自定义圆角跟圆形组件

自定义圆角跟圆形组件,需要继承ImageView类,重写里面的方法


绘图类

package com.trkj.customImageview;

/**
 * Created by LiZanhong on 2015/06/18.
 */

import java.lang.ref.WeakReference;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.widget.ImageView;

import com.example.zhu.R;

/**
 * 圆角和圆形图片组件
 */
public class RoundImageViewByXfermode extends ImageView {

	private Paint mPaint;
	private Xfermode mXfermode = new PorterDuffXfermode(Mode.DST_IN);
	private Bitmap mMaskBitmap;

	private WeakReference<Bitmap> mWeakBitmap;

	/**
	 * 图片的类型,圆形or圆角
	 */
	private int type;
	public static final int TYPE_CIRCLE = 0;
	public static final int TYPE_ROUND = 1;
	/**
	 * 圆角大小的默认值
	 */
	private static final int BODER_RADIUS_DEFAULT = 10;
	/**
	 * 圆角的大小
	 */
	private int mBorderRadius;

	public RoundImageViewByXfermode(Context context) {
		this(context, null);

		mPaint = new Paint();
		mPaint.setAntiAlias(true);
	}

	public RoundImageViewByXfermode(Context context, AttributeSet attrs) {
		super(context, attrs);

		mPaint = new Paint();
		mPaint.setAntiAlias(true);

		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.RoundImageViewByXfermode);

		mBorderRadius = a.getDimensionPixelSize(
				R.styleable.RoundImageViewByXfermode_borderRadius,
				(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
						BODER_RADIUS_DEFAULT, getResources()
								.getDisplayMetrics()));// 默认为10dp
		Log.e("TAG", mBorderRadius + "");
		type = a.getInt(R.styleable.RoundImageViewByXfermode_type, TYPE_CIRCLE);// 默认为Circle

		a.recycle();
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		/**
		 * 如果类型是圆形,则强制改变view的宽高一致,以小值为准
		 */
		if (type == TYPE_CIRCLE) {
			int width = Math.min(getMeasuredWidth(), getMeasuredHeight());
			setMeasuredDimension(width, width);
		}

	}

	@Override
	public void invalidate() {
		mWeakBitmap = null;
		if (mMaskBitmap != null) {
			mMaskBitmap.recycle();
			mMaskBitmap = null;
		}
		super.invalidate();
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		// 在缓存中取出bitmap
		Bitmap bitmap = mWeakBitmap == null ? null : mWeakBitmap.get();

		if (null == bitmap || bitmap.isRecycled()) {
			// 拿到Drawable
			Drawable drawable = getDrawable();
			// 获取drawable的宽和高
			int dWidth = drawable.getIntrinsicWidth();
			int dHeight = drawable.getIntrinsicHeight();

			if (drawable != null) {
				// 创建bitmap
				bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
						Config.ARGB_8888);
				float scale = 1.0f;
				// 创建画布
				Canvas drawCanvas = new Canvas(bitmap);
				// 按照bitmap的宽高,以及view的宽高,计算缩放比例;因为设置的src宽高比例可能和imageview的宽高比例不同,这里我们不希望图片失真;
				if (type == TYPE_ROUND) {
					// 如果图片的宽或者高与view的宽高不匹配,计算出需要缩放的比例;缩放后的图片的宽高,一定要大于我们view的宽高;所以我们这里取大值;
					scale = Math.max(getWidth() * 1.0f / dWidth, getHeight()
							* 1.0f / dHeight);
				} else {
					scale = getWidth() * 1.0F / Math.min(dWidth, dHeight);
				}
				// 根据缩放比例,设置bounds,相当于缩放图片了
				drawable.setBounds(0, 0, (int) (scale * dWidth),
						(int) (scale * dHeight));
				drawable.draw(drawCanvas);
				if (mMaskBitmap == null || mMaskBitmap.isRecycled()) {
					mMaskBitmap = getBitmap();
				}
				// Draw Bitmap.
				mPaint.reset();
				mPaint.setFilterBitmap(false);
				mPaint.setXfermode(mXfermode);
				// 绘制形状
				drawCanvas.drawBitmap(mMaskBitmap, 0, 0, mPaint);
				mPaint.setXfermode(null);
				// 将准备好的bitmap绘制出来
				canvas.drawBitmap(bitmap, 0, 0, null);
				// bitmap缓存起来,避免每次调用onDraw,分配内存
				mWeakBitmap = new WeakReference<Bitmap>(bitmap);
			}
		}
		// 如果bitmap还存在,则直接绘制即可
		if (bitmap != null) {
			mPaint.setXfermode(null);
			canvas.drawBitmap(bitmap, 0.0f, 0.0f, mPaint);
			return;
		}

	}

	/**
	 * 绘制形状
	 * 
	 * @return
	 */
	public Bitmap getBitmap() {
		Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
				Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.BLACK);

		if (type == TYPE_ROUND) {
			canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()),
					mBorderRadius, mBorderRadius, paint);
		} else {
			canvas.drawCircle(getWidth() / 2, getWidth() / 2, getWidth() / 2,
					paint);
		}

		return bitmap;
	}

	public void setType(int type) {
		this.type = type;
		this.invalidate();
	}

	public void setImageBitmap(Bundle bundle) {
		// TODO Auto-generated method stub

	}
}



values文件下的XML样式


<?xml version="1.0" encoding="utf-8"?>
<resources>

    <attr name="rightPadding" format="dimension" />

    <declare-styleable name="SlidingMenu">
        <attr name="rightPadding" />
    </declare-styleable>

    <attr name="borderRadius" format="dimension" />
    <attr name="type">
        <enum name="circle" value="0" />
        <enum name="round" value="1" />
    </attr>

    <declare-styleable name="RoundImageViewByXfermode">
        <attr name="borderRadius" />
        <attr name="type" />
    </declare-styleable>

</resources>



布局文件


                    <com.trkj.customImageview.RoundImageViewByXfermode
                        android:id="@+id/gerenziliao_touxiang"
                        android:layout_width="38dp"
                        android:layout_height="38dp"
                        android:layout_marginRight="10dp"
                        android:scaleType="centerCrop"
                        android:src="@drawable/touxiang" >
                    </com.trkj.customImageview.RoundImageViewByXfermode>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值