圆角 自动回收 ImageView styles.xml <declare-styleable name="RecycleableRoundCorneImageView"> <attr name="roundWidth" format="dimension" /> <attr name="roundHeight" format="dimension" /> </declare-styleable> //使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:lyn="http://schemas.android.com/apk/res/com.example.pa" <!-- com.example.pa 跟menifest里的package 一样 --> android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.example.pa.wedget.RecycleableRoundCorneImageView android:layout_width="100dp" android:layout_height="100dp" android:scaleType="fitXY" android:src="@drawable/z" lyn:roundHeight="10dp" lyn:roundWidth="10dp" /> </LinearLayout> //代码 public class RecycleableRoundCorneImageView extends ImageView { private Paint paint; private int roundWidth = 5; private int roundHeight = 5; private Paint paint2; private Bitmap preBitmap = null; private Bitmap mPrevBG = null; public RecycleableRoundCorneImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(context, attrs); } public RecycleableRoundCorneImageView(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); } public RecycleableRoundCorneImageView(Context context) { super(context); init(context, null); } private void init(Context context, AttributeSet attrs) { if (attrs != null) { TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RecycleableRoundCorneImageView); roundWidth = a.getDimensionPixelSize(R.styleable.RecycleableRoundCorneImageView_roundWidth, roundWidth); roundHeight = a.getDimensionPixelSize(R.styleable.RecycleableRoundCorneImageView_roundHeight, roundHeight); } else { float density = context.getResources().getDisplayMetrics().density; roundWidth = (int) (roundWidth * density); roundHeight = (int) (roundHeight * density); } paint = new Paint(); paint.setColor(Color.WHITE); paint.setAntiAlias(true); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); paint2 = new Paint(); paint2.setXfermode(null); } @Override public void draw(Canvas canvas) { Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888); Canvas canvas2 = new Canvas(bitmap); super.draw(canvas2); drawLiftUp(canvas2); drawRightUp(canvas2); drawLiftDown(canvas2); drawRightDown(canvas2); canvas.drawBitmap(bitmap, 0, 0, paint2); bitmap.recycle(); } private void drawLiftUp(Canvas canvas) { Path path = new Path(); path.moveTo(0, roundHeight); path.lineTo(0, 0); path.lineTo(roundWidth, 0); path.arcTo(new RectF(0, 0, roundWidth * 2, roundHeight * 2), -90, -90); path.close(); canvas.drawPath(path, paint); } private void drawLiftDown(Canvas canvas) { Path path = new Path(); path.moveTo(0, getHeight() - roundHeight); path.lineTo(0, getHeight()); path.lineTo(roundWidth, getHeight()); path.arcTo(new RectF(0, getHeight() - roundHeight * 2, 0 + roundWidth * 2, getWidth()), 90, 90); path.close(); canvas.drawPath(path, paint); } private void drawRightDown(Canvas canvas) { Path path = new Path(); path.moveTo(getWidth() - roundWidth, getHeight()); path.lineTo(getWidth(), getHeight()); path.lineTo(getWidth(), getHeight() - roundHeight); path.arcTo(new RectF(getWidth() - roundWidth * 2, getHeight() - roundHeight * 2, getWidth(), getHeight()), 0, 90); path.close(); canvas.drawPath(path, paint); } private void drawRightUp(Canvas canvas) { Path path = new Path(); path.moveTo(getWidth(), roundHeight); path.lineTo(getWidth(), 0); path.lineTo(getWidth() - roundWidth, 0); path.arcTo(new RectF(getWidth() - roundWidth * 2, 0, getWidth(), 0 + roundHeight * 2), -90, 90); path.close(); canvas.drawPath(path, paint); } @Override public void setImageBitmap(Bitmap bm) { if (preBitmap != null && preBitmap != bm) { if (!preBitmap.isRecycled()) { System.out.println("setImageBitmap -> recycle"); preBitmap.recycle(); } } preBitmap = bm; if (bm != null) { super.setImageDrawable(new BitmapDrawable(bm)); } else { super.setImageDrawable(new ColorDrawable(0x0)); } } @Override public void setImageDrawable(Drawable d) { if (preBitmap != null) { Bitmap curBitmap = null; if (d != null && d instanceof BitmapDrawable) { curBitmap = ((BitmapDrawable) d).getBitmap(); } if (preBitmap != curBitmap) { if (!preBitmap.isRecycled()) { System.out.println("setImageDrawable -> recycle"); preBitmap.recycle(); } preBitmap = null; } } super.setImageDrawable(d); } @Override public void setImageResource(int resId) { if (preBitmap != null) { Drawable dw = getDrawable(); if (!preBitmap.isRecycled()) { System.out.println("setImageResource-> recycle"); preBitmap.recycle(); } if (dw != null && dw instanceof BitmapDrawable) { Bitmap bm = ((BitmapDrawable) dw).getBitmap(); if (bm == preBitmap) { super.setImageDrawable(new ColorDrawable(0x0)); } } preBitmap = null; } super.setImageResource(resId); } // 背景 @Override public void setBackgroundDrawable(Drawable background) { if (mPrevBG != null) { Bitmap curBG = null; if (background != null && background instanceof BitmapDrawable) { curBG = ((BitmapDrawable) background).getBitmap(); } if (mPrevBG != curBG) { if (!mPrevBG.isRecycled()) { System.out.println("setBackgroundDrawable -> recycle"); mPrevBG.recycle(); } mPrevBG = null; } } super.setBackgroundDrawable(background); } @Override public void setBackgroundResource(int resid) { if (mPrevBG != null) { Drawable dw = getBackground(); if (!mPrevBG.isRecycled()) { System.out.println("setBackgroundResource-> recycle"); mPrevBG.recycle(); } if (dw != null && dw instanceof BitmapDrawable) { Bitmap bm = ((BitmapDrawable) dw).getBitmap(); if (bm == mPrevBG) { super.setImageDrawable(new ColorDrawable(0x0)); } } mPrevBG = null; } super.setBackgroundResource(resid); } }