欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android刮刮卡功能具體實(shí)現(xiàn)代碼

 更新時(shí)間:2016年09月13日 11:47:38   作者:pangpang123654  
這篇文章主要介紹了Android刮刮卡功能具體實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

今天整理之前的代碼,忽然看到之前自己寫的一個(gè)刮刮卡,整理下以便以后使用,同時(shí)分享給需要的朋友,如有錯(cuò)誤,還請(qǐng)多多指正。

實(shí)現(xiàn)的步驟,其實(shí)就是徒手畫三個(gè)圖層疊加在一起,最上層是繪制需要的問(wèn)題,就是以上所述的“騷年,刮我吧”,第二層就是覆蓋寬高的灰層,第三層是結(jié)果層,多的不啰嗦了,具體實(shí)現(xiàn)如下,附上詳細(xì)注釋。

/**
 * 
 * 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);

    // 對(duì)mPaint的設(shè)置
    mPaint = new Paint();
    mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mPaint.setAntiAlias(true);
    mCanvas = new Canvas();
    mPaint.setDither(true);
    // 設(shè)置畫筆為空心
    mPaint.setStyle(Style.STROKE);
    // 設(shè)置線寬,即每次擦除的寬度
    mPaint.setStrokeWidth(10);
    mPaint.setStrokeCap(Cap.ROUND);
    mPaint.setStrokeJoin(Join.ROUND);
    // 設(shè)置圖形重疊時(shí)的處理方式,一共有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("一等獎(jiǎng)");
  }

  // 接收后臺(tái)傳來(lái)的文字,即中獎(jiǎng)或者未中獎(jiǎng)的文字
  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);
      /**
       * 遍歷統(tǒng)計(jì)擦除的區(qū)域
       */
      for (int i = 0; i < screenWidth; i++)
      {
        for (int j = 0; j < screenHeight; j++)
        {
          int index = i + j * screenWidth;
          if (mPixels[index] == 0)
          {
            wipeArea++;
          }
        }
      }

      /**
       * 根據(jù)所占百分比,進(jìn)行一些操作
       */
      if (wipeArea > 0 && totalArea > 0)
      {
        int percent = (int) (wipeArea * 100 / totalArea);
        /**
         * 設(shè)置達(dá)到多少百分比的時(shí)候,彈窗提醒是否中獎(jiǎng)此處設(shè)置為20
         */
        if (percent > 20)
        {
          /**
           * 刮開獎(jiǎng)以后的操作,此處在子線程toast,可能會(huì)發(fā)生線程阻塞,只為測(cè)試使用
           */
          Looper.prepare();
          Toast.makeText(getContext(), "已刮開" + percent + "%",
              Toast.LENGTH_LONG).show();
          Looper.loop();
        }
      }
    }
  };
}

發(fā)的是公司需要的效果,以上代碼只是一個(gè)實(shí)現(xiàn),各種樣式還需要自己去實(shí)現(xiàn)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論