自定义了一个实现圆形,圆角的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;
}