圆形 圆角ImageView

本文介绍了一种自定义Android View的方法,用于创建圆形或圆角图片效果。通过使用PorterDuff.Mode.SRC_IN模式,结合Bitmap绘制技巧,实现了不同形状的图片展示效果。

自定义了一个实现圆形,圆角的View,和大家共享;

效果图:


核心代码分析:

</pre><pre name="code" class="java">	/**
	 * 绘制圆
	 * 
	 * @param Bitmap
	 *            source
	 * @param int min 长宽中短的一边
	 * @return Bitmap
	 */
	private Bitmap createCircleImage(Bitmap source, int min) {
		final Paint paint = new Paint();
		paint.setAntiAlias(true); // 去锯齿
		Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);
		Canvas canvas = new Canvas(target);
		canvas.drawCircle(min>>1, min>> 1, min>>1, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//设置 SRC_IN 叠加模式;
		canvas.drawBitmap(source, 0, 0, paint);
		return target;
	}


其实主要靠:paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));这行代码,为什么呢,我给大家解释下,SRC_IN这种模式,两个绘制的效果叠加后取交集展现后图,怎么说呢,咱们第一个绘制的是个圆形,第二个绘制的是个Bitmap,于是交集为圆形,展现的是BItmap,就实现了圆形图片效果。圆角,其实就是先绘制圆角矩形,是不是很简单,以后别人再说实现圆角,你就把这一行代码给他就行了。

从Android的示例中,给大家证明一下:

下面有一张PorterDuff.Mode的16中效果图,咱们的只是其一:



下面解析源码:

自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="UICircleImageView">
        <attr name="borderRadius" format="dimension"/>
        <attr name="type">
            <enum name="circle" value="0" />
            <enum name="round"  value="1" />
        </attr>
        <attr name="src" format="reference" />
    </declare-styleable>
    
</resources>

获取自定义属性

public class UICircleImageView extends View {

	private final static int TYPE_CIRCLE = 0; // 定义View类型 圆
	private final static int TYPE_ROUND = 1; // 定义View类型 圆角
	private int type; // 初始化View类型
	/** 图片 */
	private Bitmap mSrc;
	/** 圆角大小 */
	private int mRadius;
	/** 控件的宽度 */
	private int mWidth;
	/** 控件的高度 */
	private int mHeight;

	public UICircleImageView(Context context) {
		this(context, null);
		// TODO Auto-generated constructor stub
	}

	public UICircleImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public UICircleImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		TypedArray typedArray = context.obtainStyledAttributes(attrs,
				R.styleable.UICircleImageView);
		int n = typedArray.getIndexCount();
		for (int i = 0; i < n; i++) {
			int attr = typedArray.getIndex(i);
			switch (attr) {
			case R.styleable.UICircleImageView_borderRadius:
				mRadius = typedArray.getDimensionPixelSize(attr,
						(int) TypedValue.applyDimension(
								TypedValue.COMPLEX_UNIT_DIP, 10f,
								getResources().getDisplayMetrics()));
				break;
			case R.styleable.UICircleImageView_src:
				mSrc = BitmapFactory.decodeResource(getResources(),
						typedArray.getResourceId(attr, 0));
				break;
			case R.styleable.UICircleImageView_type:
				type = typedArray.getInt(attr, 0);
				break;

			default:
				break;
			}

		}
		typedArray.recycle();
	}

设置View  大小

@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// 设置宽度
		int specMode = MeasureSpec.getMode(widthMeasureSpec);
		int specSize = MeasureSpec.getSize(widthMeasureSpec);
		if (specMode == MeasureSpec.EXACTLY) 
		{
			mWidth = specSize;
			mSrc = Bitmap.createScaledBitmap(mSrc, mWidth, mSrc.getHeight(), true);
		} else {
			int desireByImg = getPaddingLeft() + getPaddingRight()
					+ mSrc.getWidth();
			if (specMode == MeasureSpec.AT_MOST) {              //相当于wrap_content
				mWidth = Math.min(desireByImg, specSize);
			}
		}

		// 设置高度
		specMode = MeasureSpec.getMode(heightMeasureSpec);
		specSize = MeasureSpec.getSize(heightMeasureSpec);
		if (specMode == MeasureSpec.EXACTLY) // match_parent
		{
			mHeight = specSize;
			mSrc = Bitmap.createScaledBitmap(mSrc, mSrc.getWidth(),mHeight, true);
		} else {
			int desireByImg = getPaddingTop() + getPaddingBottom()
					+ mSrc.getHeight();
			if (specMode == MeasureSpec.AT_MOST) {       //相当于wrap_content
				mHeight = Math.min(desireByImg, specSize);
			}
		}
		setMeasuredDimension(mWidth, mHeight);
	}

绘制VIew

@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		switch (type) {

		case TYPE_CIRCLE:// 绘制圆
			/**
			 * 长和宽不一致去,较小的值
			 */
			int min = Math.min(mWidth, mHeight);
			// 压缩图片
			mSrc = Bitmap.createScaledBitmap(mSrc, min, min, true);
			canvas.drawBitmap(createCircleImage(mSrc, min), 0, 0, null);
			break;
		case TYPE_ROUND: // 圆角
			canvas.drawBitmap(createRoundeConerImage(mSrc), 0, 0, null);
			break;

		}
	}

	/**
	 * 绘制圆
	 * 
	 * @param Bitmap
	 *            source
	 * @param int min 长宽中短的一边
	 * @return Bitmap
	 */
	private Bitmap createCircleImage(Bitmap source, int min) {
		final Paint paint = new Paint();
		paint.setAntiAlias(true); // 去锯齿
		Bitmap target = Bitmap.createBitmap(min, min, Config.ARGB_8888);
		Canvas canvas = new Canvas(target);
		canvas.drawCircle(min>>1, min>> 1, min>>1, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//设置 SRC_IN 叠加模式;
		canvas.drawBitmap(source, 0, 0, paint);
		return target;
	}

	private Bitmap createRoundeConerImage(Bitmap source) {
		final Paint paint = new Paint();
		paint.setAntiAlias(true); // 去锯齿
		Bitmap target = Bitmap.createBitmap(mWidth, mHeight, Config.ARGB_8888);
		Canvas canvas = new Canvas(target);
		RectF rect = new RectF(0, 0, source.getWidth(), source.getHeight());// 画矩形
		canvas.drawRoundRect(rect, mRadius, mRadius, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));//设置
		canvas.drawBitmap(source, 0, 0, paint);
		return target;
	}


点击源码下载


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值