Android刮刮卡功能具體實現代碼_Android

來源:互聯網
上載者:User

今天整理之前的代碼,忽然看到之前自己寫的一個刮刮卡,整理下以便以後使用,同時分享給需要的朋友,如有錯誤,還請多多指正。

實現的步驟,其實就是徒手畫三個圖層疊加在一起,最上層是繪製需要的問題,就是以上所述的“騷年,刮我吧”,第二層就是覆蓋寬高的灰層,第三層是結果層,多的不囉嗦了,具體實現如下,附上詳細注釋。

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

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

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.