这篇博客这不是讲解如何自定义控件,是怎么使用造好的轮子(好烦啊,博客竟然不能上传小附件,只能通过上传资源的方式)点我下载
资源文件中有两个类和一个xml文件,将其放入项目相对位置,然后就是在布局中引入CircleImageView这个类。
设置属性时,设置不同宽高(实际上确定椭圆的四个顶点),可以得到不同的椭圆,如图:
不想下载的话,直接复制下面代码
如果res/values没有attrs.xml 这个文件,就先创建,再复制下面的代码进去
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircleImageView">
<attr name="shape" format="integer">
<enum name="circle" value="1" />
<enum name="rectangle" value="2" />
<enum name="svg" value="3" />
</attr>
<attr name="svg_raw_resource" format="reference" />
</declare-styleable>
</resources>
下面两个类复制到java包相应位置下
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import java.lang.ref.WeakReference;
public abstract class BaseImageView extends ImageView {
protected Context mContext;
private static final Xfermode sXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
private Bitmap mMaskBitmap;
private Paint mPaint;
private WeakReference<Bitmap> mWeakBitmap;
public BaseImageView(Context context) {
super(context);
sharedConstructor(context);
}
public BaseImageView(Context context, AttributeSet attrs) {
super(context, attrs);
sharedConstructor(context);
}
public BaseImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
sharedConstructor(context);
}
private void sharedConstructor(Context context) {
mContext = context;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
public void invalidate() {
mWeakBitmap = null;
if (mMaskBitmap != null) { mMaskBitmap.recycle(); }
super.invalidate();
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
if (!isInEditMode()) {
int i = canvas.saveLayer(0.0f, 0.0f, getWidth(), getHeight(),
null, Canvas.ALL_SAVE_FLAG);
try {
Bitmap bitmap = mWeakBitmap != null ? mWeakBitmap.get() : null;
if (bitmap == null || bitmap.isRecycled()) {
Drawable drawable = getDrawable();
if (drawable != null) {
bitmap = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_8888);
Canvas bitmapCanvas = new Canvas(bitmap);
drawable.setBounds(0, 0, getWidth(), getHeight());
drawable.draw(bitmapCanvas);
if (mMaskBitmap == null || mMaskBitmap.isRecycled()) {
mMaskBitmap = getBitmap();
}
mPaint.reset();
mPaint.setFilterBitmap(false);
mPaint.setXfermode(sXfermode);
bitmapCanvas.drawBitmap(mMaskBitmap, 0.0f, 0.0f, mPaint);
mWeakBitmap = new WeakReference<Bitmap>(bitmap);
}
}
if (bitmap != null) {
mPaint.setXfermode(null);
canvas.drawBitmap(bitmap, 0.0f, 0.0f, mPaint);
return;
}
} catch (Exception e) {
System.gc();
} finally {
canvas.restoreToCount(i);
}
} else {
super.onDraw(canvas);
}
}
public abstract Bitmap getBitmap();
}
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
public class CircleImageView extends BaseImageView {
public static class Shape {
public static final int CIRCLE = 1;
public static final int RECTANGLE = 2;
public static final int SVG = 3;
}
private int mShape = Shape.CIRCLE;
public CircleImageView(Context context) {
super(context);
}
public CircleImageView(Context context, int resourceId, int shape, int svgRawResourceId) {
this(context);
setImageResource(resourceId);
mShape = shape;
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
sharedConstructor(context, attrs);
}
public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
sharedConstructor(context, attrs);
}
private void sharedConstructor(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView);
mShape = a.getInt(R.styleable.CircleImageView_shape, Shape.CIRCLE);
a.recycle();
}
public Bitmap getBitmap(int width, int height) {
Bitmap bitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.BLACK);
canvas.drawOval(new RectF(0.0f, 0.0f, width, height), paint);
return bitmap;
}
@Override
public Bitmap getBitmap() {
switch (mShape) {
case Shape.CIRCLE:
return getBitmap(getWidth(), getHeight());
}
return null;
}
}