Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android刮刮卡功能具體實現代碼

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

發的是公司需要的效果,以上代碼只是一個實現,各種樣式還需要自己去實現。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved