美文网首页
Android PorterDuff.Mode案例-刮刮卡

Android PorterDuff.Mode案例-刮刮卡

作者: 刘小厨 | 来源:发表于2020-03-31 18:07 被阅读0次

    本文主要承接上篇文章Android 图层混合模式PorterDuff.Mode
    简单实现了一个刮刮卡的小案例,相当于api应用案例,不是完整的demo,效果图如下:

    刮刮卡.gif

    代码比较简单,不做过多讲解,有疑问可以看上文Android 图层混合模式PorterDuff.Mode
    贴下代码:

    public class XfermodeEraserView extends View {
    
        private Paint  mPaint;
        private Bitmap mDstBmp, mSrcBmp, mTxtBmp;
        private Path mPath;
    
        public XfermodeEraserView(Context context) {
            this(context, null);
        }
    
        public XfermodeEraserView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public XfermodeEraserView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            //初始化画笔
            mPaint = new Paint();
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(80);
    
            //禁用硬件加速
            setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    
            //初始化图片对象
            mTxtBmp = BitmapFactory.decodeResource(getResources(), R.drawable.result);
            mSrcBmp = BitmapFactory.decodeResource(getResources(), R.drawable.eraser);
            mDstBmp = Bitmap.createBitmap(mSrcBmp.getWidth(), mSrcBmp.getHeight(), Bitmap.Config.ARGB_8888);
    
            //路径(贝塞尔曲线)
            mPath = new Path();
        }
    
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            //绘制刮奖结果
            canvas.drawBitmap(mTxtBmp, 0, 0, mPaint);
    
            //使用离屏绘制
            int layerID = canvas.saveLayer(0, 0, getWidth(), getHeight(), mPaint, Canvas.ALL_SAVE_FLAG);
    
            //先将路径绘制到 bitmap上
            Canvas dstCanvas = new Canvas(mDstBmp);
            dstCanvas.drawPath(mPath, mPaint);
    
            //绘制 目标图像
            canvas.drawBitmap(mDstBmp, 0, 0, mPaint);
            //设置 模式 为 SRC_OUT, 擦橡皮区域为交集区域需要清掉像素
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
            //绘制源图像
            canvas.drawBitmap(mSrcBmp, 0, 0, mPaint);
    
            mPaint.setXfermode(null);
    
            canvas.restoreToCount(layerID);
        }
    
        private float mEventX, mEventY;
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            super.onTouchEvent(event);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mEventX = event.getX();
                    mEventY = event.getY();
                    mPath.moveTo(mEventX, mEventY);
                    break;
                case MotionEvent.ACTION_MOVE:
                    float endX = (event.getX() - mEventX) / 2 + mEventX;
                    float endY = (event.getY() - mEventY) / 2 + mEventY;
                    //画二阶贝塞尔曲线
                    mPath.quadTo(mEventX, mEventY, endX, endY);
                    mEventX = event.getX();
                    mEventY = event.getY();
                    break;
            }
            invalidate();
            return true; //消费事件
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Android PorterDuff.Mode案例-刮刮卡

          本文链接:https://www.haomeiwen.com/subject/sxrguhtx.html