当前位置:主页 > 移动开发 > Android代码 >

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

时间:2022-01-10 15:18:34 | 栏目: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();
        }
      }
    }
  };
}

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

您可能感兴趣的文章:

相关文章