package com.zzw.Qunyinzghuan3; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * Created by zzw on 2016/7/12. * 描述: */ public class PorterDuffXfermodeView extends View { private Bitmap bgBmp, fgBmp; private Paint paint; private Canvas mCanvas; private Path path; public PorterDuffXfermodeView(Context context) { this(context, null); } public PorterDuffXfermodeView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public PorterDuffXfermodeView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); paint = new Paint(); paint.setAlpha(0);//要把透明度设置为0 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); paint.setStyle(Paint.Style.STROKE); paint.setStrokeJoin(Paint.Join.ROUND);//设置结合处的样子 Miter:结合处为锐角, Round:结合处为圆弧:BEVEL:结合处为直线。 paint.setStrokeWidth(50); paint.setStrokeCap(Paint.Cap.ROUND);// 设置画笔转弯去的连接风格 path = new Path(); bgBmp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_test); fgBmp = Bitmap.createBitmap(bgBmp.getWidth(), bgBmp.getHeight(), Bitmap.Config.ARGB_8888); mCanvas = new Canvas(fgBmp); mCanvas.drawColor(Color.GRAY); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: path.reset(); path.moveTo(event.getX(), event.getY()); break; case MotionEvent.ACTION_MOVE: path.lineTo(event.getX(), event.getY()); break; } mCanvas.drawPath(path, paint); invalidate(); return true; } @Override protected void onDraw(Canvas canvas) { canvas.drawBitmap(bgBmp, 0, 0, null); canvas.drawBitmap(fgBmp, 0, 0, null); } }