Android刮刮卡功能具体实现代码

作者:pangpang123654 时间:2021-08-06 04:12:24 

今天整理之前的代码,忽然看到之前自己写的一个刮刮卡,整理下以便以后使用,同时分享给需要的朋友,如有错误,还请多多指正。

Android刮刮卡功能具体实现代码

Android刮刮卡功能具体实现代码

实现的步骤,其实就是徒手画三个图层叠加在一起,最上层是绘制需要的问题,就是以上所述的“骚年,刮我吧”,第二层就是覆盖宽高的灰层,第三层是结果层,多的不啰嗦了,具体实现如下,附上详细注释。


/**
*
* created by zero on 2016-9-9
*
* 刮刮卡
*
*/
public class ScratchView extends View
{

public ScratchView(Context context)
 {
   super(context);
   init();
 }

private Canvas mCanvas = null;
 private Path mPath = null;
 private Paint mPaint = null;

// 定义画布的宽和高
 private int screenWidth = 720;
 private int screenHeight = 360;
 private Bitmap bitmap = null;

private void init() {
   // TODO Auto-generated method stub
   mPath = new Path();
   bitmap = Bitmap.createBitmap(screenWidth, screenHeight,
       Config.ARGB_8888);

// 对mPaint的设置
   mPaint = new Paint();
   mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
   mPaint.setAntiAlias(true);
   mCanvas = new Canvas();
   mPaint.setDither(true);
   // 设置画笔为空心
   mPaint.setStyle(Style.STROKE);
   // 设置线宽,即每次擦除的宽度
   mPaint.setStrokeWidth(10);
   mPaint.setStrokeCap(Cap.ROUND);
   mPaint.setStrokeJoin(Join.ROUND);
   // 设置图形重叠时的处理方式,一共有16种方式,有兴趣可自己查阅
   mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
   mPaint.setAlpha(0);

mCanvas = new Canvas(bitmap);
   mCanvas.drawColor(Color.parseColor("#c0c0c0"));
   setBitmapText();
 }

private void setBitmapText() {
   Paint paint = new Paint();
   paint.setTextSize(40);
   paint.setColor(Color.parseColor("#9f9fa0"));
   paint.setFlags(Paint.ANTI_ALIAS_FLAG);
   paint.setAntiAlias(true);
   paint.setTextAlign(Paint.Align.CENTER);
   paint.setFakeBoldText(true);

Canvas canvas = new Canvas(bitmap);
   canvas.drawColor(Color.alpha(0));
   canvas.rotate(-20);
   // 遍历绘制文字
   for (int i = 0; i < screenWidth + 200; i += 300)
   {
     for (int j = 0; j < screenHeight + 200; j += 60)
     {
       canvas.drawText("刮我吧,骚年!", i, j, paint);
     }
   }
   setScratchBackground("一等奖");
 }

// 接收后台传来的文字,即中奖或者未中奖的文字
 public void setScratchBackground(String txt_win) {
   // TODO Auto-generated method stub
   Paint paint = new Paint();
   Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight,
       Config.ARGB_8888);
   paint.setTextSize(40);
   paint.setColor(Color.BLACK);
   paint.setFlags(Paint.ANTI_ALIAS_FLAG);
   paint.setAntiAlias(true);
   paint.setTextAlign(Paint.Align.CENTER);

Canvas canvas = new Canvas(bitmap);
   canvas.drawColor(Color.alpha(0));
   canvas.drawText(txt_win, screenWidth / 2, 60, paint);
   setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
 }

@Override
 protected void onDraw(Canvas canvas) {
   // TODO Auto-generated method stub
   super.onDraw(canvas);
   mCanvas.drawPath(mPath, mPaint);
   canvas.drawBitmap(bitmap, 0, 0, null);
 }

int x = 0;
 int y = 0;

@SuppressLint("ClickableViewAccessibility")
 @Override
 public boolean onTouchEvent(MotionEvent event) {
   // TODO Auto-generated method stub
   int action = event.getAction();
   int currX = (int) event.getX();
   int currY = (int) event.getY();
   switch (action)
   {
   case MotionEvent.ACTION_DOWN:
   {
     mPath.reset();
     x = currX;
     y = currY;
     mPath.moveTo(x, y);
   }
     break;
   case MotionEvent.ACTION_MOVE:
   {
     mPath.quadTo(x, y, currX, currY);
     x = currX;
     y = currY;
     postInvalidate();
   }
     break;
   case MotionEvent.ACTION_UP:
   {
     new Thread(mRunnable).start();
   }
   case MotionEvent.ACTION_CANCEL:
   {
     mPath.reset();
   }
     break;
   }
   return true;
 }

private Runnable mRunnable = new Runnable()
 {
   private int[] mPixels;

@Override
   public void run() {
     float wipeArea = 0;
     float totalArea = screenWidth * screenHeight;
     Bitmap mBitmap = bitmap;
     mPixels = new int[screenWidth * screenHeight];
     /**
      * 拿到所有的像素信息
      */
     mBitmap.getPixels(mPixels, 0, screenWidth, 0, 0, screenWidth,
         screenHeight);
     /**
      * 遍历统计擦除的区域
      */
     for (int i = 0; i < screenWidth; i++)
     {
       for (int j = 0; j < screenHeight; j++)
       {
         int index = i + j * screenWidth;
         if (mPixels[index] == 0)
         {
           wipeArea++;
         }
       }
     }

/**
      * 根据所占百分比,进行一些操作
      */
     if (wipeArea > 0 && totalArea > 0)
     {
       int percent = (int) (wipeArea * 100 / totalArea);
       /**
        * 设置达到多少百分比的时候,弹窗提醒是否中奖此处设置为20
        */
       if (percent > 20)
       {
         /**
          * 刮开奖以后的操作,此处在子线程toast,可能会发生线程阻塞,只为测试使用
          */
         Looper.prepare();
         Toast.makeText(getContext(), "已刮开" + percent + "%",
             Toast.LENGTH_LONG).show();
         Looper.loop();
       }
     }
   }
 };
}

发的是公司需要的效果,以上代码只是一个实现,各种样式还需要自己去实现。

标签:Android,刮刮卡
0
投稿

猜你喜欢

  • 详解Java中Iterable与Iterator用法

    2022-05-14 04:25:30
  • Java8中关于Function.identity()的使用

    2021-11-16 16:05:15
  • SprintBoot深入浅出讲解场景启动器Starter

    2023-11-24 20:58:58
  • Java实现计算器设计

    2023-08-18 13:36:54
  • Spring Boot 整合JPA 数据模型关联使用操作(一对一、一对多、多对多)

    2022-11-18 23:14:29
  • idea打包java可执行jar包的实现步骤

    2022-07-30 02:57:48
  • Spring Cloud 的 Hystrix.功能及实践详解

    2023-11-19 06:40:46
  • C#资源释放方法实例分析

    2022-03-22 04:46:20
  • Java初学者常问的问题(推荐)

    2023-05-29 05:41:51
  • springboot结合websocket聊天室实现私聊+群聊

    2022-09-25 03:22:23
  • Java GZip 基于内存实现压缩和解压的方法

    2023-05-24 12:47:29
  • springboot bean循环依赖实现以及源码分析

    2022-06-05 11:50:15
  • SpringBoot整合RedisTemplate实现缓存信息监控的步骤

    2023-10-14 10:17:13
  • 使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)

    2023-04-24 07:35:45
  • IDEA SpringBoot项目配置热更新的步骤详解(无需每次手动重启服务器)

    2023-11-12 00:22:41
  • JDK集合源码之解析TreeMap(一)

    2021-08-09 07:14:02
  • springboot+log4j.yml配置日志文件的方法

    2023-08-07 11:33:17
  • spring boot集成p6spy的最佳实践

    2023-04-11 23:40:36
  • Java Condition条件变量提高线程通信效率

    2022-11-26 13:32:46
  • C# Volatile的具体使用

    2023-11-21 11:14:50
  • asp之家 软件编程 m.aspxhome.com