Android 圆角 ImageView类可设置弧度(代码简单)

作者:jerrylsxu 时间:2022-09-06 13:13:36 

废话不多说了,直接给大家贴代码了,具体代码如下所示:


public class RoundImageView extends ImageView {
private Paint paint;
private int roundWidth = 50;
private int roundHeight = 50;
private Paint paint2;
public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public RoundImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public void SetRoundValue(float roundValue) {
roundWidth = (int) roundValue;
roundHeight = (int) roundValue;
}
public RoundImageView(Context context) {
super(context);
init(context, null);
}
@SuppressLint("Recycle")
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundAngleImageView);
roundWidth = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth, roundWidth);
roundHeight = a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight, roundHeight);
} else {
float density = context.getResources().getDisplayMetrics().density;
roundWidth = (int) (roundWidth * density);
roundHeight = (int) (roundHeight * density);
}
paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
paint2 = new Paint();
paint2.setXfermode(null);
}
@Override
public void draw(Canvas canvas) {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
Canvas canvas2 = new Canvas(bitmap);
super.draw(canvas2);
drawLiftUp(canvas2);
drawRightUp(canvas2);
drawLiftDown(canvas2);
drawRightDown(canvas2);
canvas.drawBitmap(bitmap, 0, 0, paint2);
bitmap.recycle();
bitmap = null;
}
private void drawLiftUp(Canvas canvas) {
Path path = new Path();
path.moveTo(0, roundHeight);
path.lineTo(0, 0);
path.lineTo(roundWidth, 0);
path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90);
path.close();
canvas.drawPath(path, paint);
}
private void drawLiftDown(Canvas canvas) {
Path path = new Path();
path.moveTo(0, getHeight() - roundHeight);
path.lineTo(0, getHeight());
path.lineTo(roundWidth, getHeight());
path.arcTo(new RectF(0, getHeight() - roundHeight * 2, 0 + roundWidth * 2, getWidth()), 90, 90);
path.close();
canvas.drawPath(path, paint);
}
private void drawRightDown(Canvas canvas) {
Path path = new Path();
path.moveTo(getWidth() - roundWidth, getHeight());
path.lineTo(getWidth(), getHeight());
path.lineTo(getWidth(), getHeight() - roundHeight);
path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), 0,
90);
path.close();
canvas.drawPath(path, paint);
}
private void drawRightUp(Canvas canvas) {
Path path = new Path();
path.moveTo(getWidth(), roundHeight);
path.lineTo(getWidth(), 0);
path.lineTo(getWidth() - roundWidth, 0);
path.arcTo(new RectF(getWidth() - roundWidth * 2, 0, getWidth(), 0 + roundHeight * 2), -90, 90);
path.close();
canvas.drawPath(path, paint);
}
}

好了,有关Android 圆角 ImageView类可设置弧度的内容小编就给大家介绍到这里,希望对大家有所帮助!

标签:android,圆角,imageview
0
投稿

猜你喜欢

  • Java中保留两位小数的四种方法实现实例

    2022-07-15 20:12:23
  • C#抽象类与抽象方法详解

    2022-05-20 18:08:03
  • Jackson 反序列化时实现大小写不敏感设置

    2021-11-18 06:17:18
  • springmvc+shiro自定义过滤器的实现代码

    2021-08-11 21:23:11
  • Springboot+SpringSecurity+JWT实现用户登录和权限认证示例

    2021-11-14 11:06:11
  • C#实现文件上传与下载功能实例

    2022-11-18 07:59:03
  • java 的Collection接口实例详解

    2021-09-22 15:35:00
  • Android下拉列表spinner的实例代码

    2023-07-31 20:39:47
  • 一篇文章教你如何用多种迭代写法实现二叉树遍历

    2023-12-23 04:03:29
  • C#中数组段用法实例分析

    2022-06-11 23:08:05
  • java通过Idea远程一键部署springboot到Docker详解

    2022-03-26 09:31:27
  • Java BigDecimal中divide方法案例详解

    2021-12-31 07:00:29
  • c# 实现MD5,SHA1,SHA256,SHA512等常用加密算法源代码

    2021-11-22 22:18:24
  • Swift编程中的泛型解析

    2022-04-19 05:37:24
  • java在网页上面抓取邮件地址的方法

    2023-10-01 19:18:21
  • Java ==,equals()与hashcode()的使用

    2022-02-24 07:06:28
  • 详解Mybatis框架SQL防注入指南

    2023-09-16 02:49:02
  • Java并发包线程池ThreadPoolExecutor的实现

    2022-11-10 09:52:41
  • 基于Android AppWidgetProvider的使用介绍

    2021-09-27 08:48:19
  • 手动添加jar包进Maven本地库内的方法

    2023-08-03 03:10:09
  • asp之家 软件编程 m.aspxhome.com