• 使用PorterDuffXfermode画出刮刮奖效果p146-p148


    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);
        }
    }
  • 相关阅读:
    Entity Framework 学习高级篇1—改善EF代码的方法(上)
    Entity Framework 学习高级篇2—改善EF代码的方法(下)
    Entity Framework 学习中级篇5—使EF支持Oracle9i
    Window API 之 PostMessage (消息投递)
    从image/xpng谈ContentType(s)
    SQL中使用 N 前缀的原因
    Delphi初浅入门笔记之一 :ObjectPascal基础
    写入DLL文件
    SQL Server 2005和SQL Server 2000中bit类型的区别
    HTML <a> 标签的 target 属性说明
  • 原文地址:https://www.cnblogs.com/zzw1994/p/5663901.html
Copyright © 2020-2023  润新知