android 方式实现imageview圆角

private int mRadius;

/**

  • 3x3 矩阵,主要用于缩小放大

*/

private Matrix mMatrix;

/**

  • 渲染图像,使用图像为绘制图形着色

*/

private BitmapShader mBitmapShader;

/**

  • view的宽度

*/

private int mWidth;

private RectF mRoundRect;

/**

  • 构造函数:获取自定义属性

*/

public RoundImageView(Context context, AttributeSet attrs) {

super(context, attrs);

mMatrix = new Matrix();

mBitmapPaint = new Paint();

mBitmapPaint.setAntiAlias(true);

TypedArray a = context.obtainStyledAttributes(attrs,

R.styleable.RoundImageView);

mBorderRadius = a.getDimensionPixelSize(

R.styleable.RoundImageView_borderRadius, (int) TypedValue

.applyDimension(TypedValue.COMPLEX_UNIT_DIP,

BODER_RADIUS_DEFAULT, getResources()

.getDisplayMetrics()));// 默认为10dp

type = a.getInt(R.styleable.RoundImageView_type, TYPE_CIRCLE);// 默认为Circle

a.recycle();

}

/**

  • 关于view的宽高:主要用于当设置类型为圆形时,我们强制让view的宽和高一致

*/

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

Log.e(“TAG”, “onMeasure”);

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

/**

  • 如果类型是圆形,则强制改变view的宽高一致,以小值为准;如果是圆角则不用管宽高问题

*/

if (type == TYPE_CIRCLE) {

mWidth = Math.min(getMeasuredWidth(), getMeasuredHeight());

mRadius = mWidth / 2;

setMeasuredDimension(mWidth, mWidth);

}

}

/**

    1. 设置BitmapShader
    1. 绘制

*/

@Override

  • 12
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android ImageView 圆角可以通过以下两种方式实现: 1. 使用 XML 属性设置圆角ImageView 的 XML 布局文件中,可以使用以下属性设置圆角: ``` android:background="@drawable/your_image" android:scaleType="centerCrop" android:clipToOutline="true" android:outlineProvider="background" ``` 其中,`your_image` 是你要显示的图片资源。`scaleType` 属性设置图片的缩放方式,`clipToOutline` 属性设置是否裁剪视图的轮廓,`outlineProvider` 属性设置视图的轮廓提供者,这里使用 `background` 表示使用视图的背景作为轮廓。 然后,在 `res/drawable` 目录下创建一个 XML 文件,命名为 `your_image.xml`,内容如下: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="10dp" /> </shape> ``` 其中,`radius` 属性设置圆角的半径大小。 2. 使用代码设置圆角Java 代码中,可以使用以下方法设置圆角: ``` ImageView imageView = findViewById(R.id.image_view); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.your_image); RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap); roundedBitmapDrawable.setCornerRadius(10); imageView.setImageDrawable(roundedBitmapDrawable); ``` 其中,`your_image` 是你要显示的图片资源。`RoundedBitmapDrawableFactory.create()` 方法创建一个圆角位图,`setCornerRadius()` 方法设置圆角的半径大小,`setImageDrawable()` 方法设置 ImageView 的显示内容为圆角位图。 以上两种方法都可以实现 ImageView 圆角的效果,具体使用哪种方式取决于你的需求和习惯。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值