我制作了这个Custom Drawable,它应该将任何Drawable剪辑成圆形.但是在我的实现中,drawable传递的是原始形式的输出而不是循环形式.
public class CircularDrawable extends Drawable { Paint mPaint,xfermodePaint; Drawable mDrawable; int[] vinylCenter = new int[2]; int radius; Bitmap src; PorterDuffXfermode xfermode; Rect rect; Canvas testCanvas; public CircularDrawable(Drawable drawable) { mDrawable = drawable; mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mPaint.setColor(0xffffffff); mPaint.setStyle(Paint.Style.FILL); xfermodePaint = new Paint(Paint.ANTI_ALIAS_FLAG); xfermode=new PorterDuffXfermode(PorterDuff.Mode.SRC_IN); xfermodePaint.setXfermode(xfermode); testCanvas=new Canvas(); } @Override public void setBounds(Rect bounds) { super.setBounds(bounds); mDrawable.setBounds(bounds); } @Override public void setBounds(int left, int top, int right, int bottom) { super.setBounds(left, top, right, bottom); mDrawable.setBounds(left, top, right, bottom); } @Override protected void onBoundsChange(Rect bounds) { super.onBoundsChange(bounds); vinylCenter[0] = bounds.width() / 2; vinylCenter[1] = bounds.height() / 2; radius = (bounds.right - bounds.left) / 2; src = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888); testCanvas.setBitmap(src); } @Override public void draw(Canvas canvas) { canvas.save(); canvas.drawARGB(0, 0, 0, 0); canvas.drawCircle(vinylCenter[0],vinylCenter[1],radius,mPaint); mDrawable.draw(testCanvas); canvas.drawBitmap(src,0f,0f,xfermodePaint); } @Override public void setAlpha(int alpha) {/*ignored*/} @Override public void setColorFilter(ColorFilter colorFilter) {/*ignored*/} @Override public int getOpacity() { /*ignored*/ return 0; } }
我的错是什么?
此外,我正在使用SquareImageview来显示这个drawable,它只是使onMeasure中的视图正方形.此问题不适用于圆形imageview.