Androidワイパーカードの機能は具体的にコードを実現します。


今日は前のコードを整理して、突然前に自分で書いたワイパーカードを見ました。後で使うために整理して、必要な友達に共有します。


実现のステップは、つまり素手で3つのレイヤーを重ねて、一番上の段阶は絵を描く上で必要な问题です。以上述べた「騒年、私を剃りましょう」です。2段目は、幅の高い灰层を覆い、3段目は结果层です。

/**
 * 
 * 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();
        }
      }
    }
  };
}
会社に必要な効果です。以上のコードは一つの実現だけです。いろいろな様式は自分で実現する必要があります。
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。