Android 完美实现图片圆角和圆形(对实现进行分析)

文章详细描述了一个自定义的AndroidImageView类,它允许用户设置图片类型(圆形或圆角)、边框半径,并在onMeasure和onDraw方法中动态计算和绘制图形。着重展示了如何根据给定的类型调整图片尺寸和绘制不同形状。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

private static final int TYPE_CIRCLE = 0;

private static final int TYPE_ROUND = 1;

/**

  • 图片

*/

private Bitmap mSrc;

/**

  • 圆角的大小

*/

private int mRadius;

/**

  • 控件的宽度

*/

private int mWidth;

/**

  • 控件的高度

*/

private int mHeight;

public CustomImageView(Context context, AttributeSet attrs)

{

this(context, attrs, 0);

}

public CustomImageView(Context context)

{

this(context, null);

}

/**

  • 初始化一些自定义的参数

  • @param context

  • @param attrs

  • @param defStyle

*/

public CustomImageView(Context context, AttributeSet attrs, int defStyle)

{

super(context, attrs, defStyle);

TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomImageView, defStyle, 0);

int n = a.getIndexCount();

for (int i = 0; i < n; i++)

{

int attr = a.getIndex(i);

switch (attr)

{

case R.styleable.CustomImageView_src:

mSrc = BitmapFactory.decodeResource(getResources(), a.getResourceId(attr, 0));

break;

case R.styleable.CustomImageView_type:

type = a.getInt(attr, 0);// 默认为Circle

break;

case R.styleable.CustomImageView_borderRadius:

mRadius= a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.C

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值