android自定义圆角View组件

1、在res/values文件夹下创建attrs.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
    <declare-styleable name="RoundCornerView">
        <attr name="cornerRadius" format="dimension" />
        <attr name="src" format="integer" />
    </declare-styleable>

</resources> 

2、RoundCornerView类:

public class RoundCornerView extends View {

	private Bitmap src; 

	private Bitmap roundCornerBitmap;

	private float mRadius = 1; //圆角半径

	public RoundCornerView(Context context) {
		super(context);
	}

	public RoundCornerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public RoundCornerView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs);
	}

	private void init(Context context, AttributeSet attrs) {
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundCornerView);
		mRadius = ta.getDimension(R.styleable.RoundCornerView_cornerRadius, mRadius);
		int resID = ta.getResourceId(R.styleable.RoundCornerView_src, 0);
		src = BitmapFactory.decodeResource(getResources(), resID);
		ta.recycle();
	}

	private Bitmap convertToRoundCornerBitmap() {
		if (src == null)
			return null;
		int width;
		int height;
		LayoutParams lp = getLayoutParams();
		if (lp != null && lp.width == LayoutParams.WRAP_CONTENT) { //处理View大小为wrap_content的情况
			width = src.getWidth();
		} else {
			width = getWidth();
		}
		if (lp != null && lp.width == LayoutParams.WRAP_CONTENT) {
			height = src.getHeight();
		} else {
			height = getHeight();
		}
		final Paint paint = new Paint();
		paint.setAntiAlias(true);
		Bitmap target = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(target);
		RectF rect = new RectF(0, 0, width, height);
		canvas.drawRoundRect(rect, mRadius, mRadius, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //图层的交集,显示上层
//		Bitmap scaledSource = Bitmap.createScaledBitmap(source, width, height, true);
//		canvas.drawBitmap(scaledSource, 0, 0, paint);
		canvas.drawBitmap(src, null, new Rect(0, 0, width, height), paint); //该行代码和以上两句注释的代码作用相同,但是这里的好处是不用创建新的Bitmap
		return target;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (roundCornerBitmap == null) {
			roundCornerBitmap = convertToRoundCornerBitmap();
		}
		if (roundCornerBitmap != null) {
			canvas.drawBitmap(roundCornerBitmap, 0, 0, null);
		}
	}
}

3、在布局里显示该组件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:bobo="http://schemas.android.com/apk/res/com.example.bobo"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.example.bobo.RoundCornerView
        android:layout_width="100dp"
        android:layout_height="100dp"
        bobo:cornerRadius="20dp"
        bobo:src="@drawable/men_head"
        android:layout_centerInParent="true" />

</RelativeLayout>

完成。效果如下:



代码下载


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值