
圆角图片.jpg
首先准备两张图片

xyjy6.png
查看原图才能看清

shade.png
Xfermode中SRC_IN模式是利用目标图片的透明度对原图片的透明度进行处理,目标图片透明度越低,相应的原图的透明度也越低
private Bitmap bitmapSrc;
private Bitmap bitmapDst;
private Paint mPaint = new Paint();
初始化参数
public RoundImageview(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//使用Xfermode时,关闭硬件加速
setLayerType(LAYER_TYPE_SOFTWARE, null);
bitmapSrc = BitmapFactory.decodeResource(getResources(), R.mipmap.xyjy6);
bitmapDst = BitmapFactory.decodeResource(getResources(), R.mipmap.shade);
mPaint.setAntiAlias(true);
mPaint.setDither(true);
}
onDraw方法中处理
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmapDst,0,0,mPaint);
//后画SRC的图片
Xfermode xfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
mPaint.setXfermode(xfermode);
canvas.drawBitmap(bitmapSrc,0,0,mPaint);
mPaint.setXfermode(null);
}
网友评论