欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-05 16:40:49
今天整理之前的代码,忽然看到之前自己写的一个刮刮卡,整理下以便以后使用,同时分享给需要的朋友,如有错误,还请多多指正。 实现的步骤,其实就是徒手画三个图层叠加在一...

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

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();
        }
      }
    }
  };
}

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。