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

Android studio實(shí)現(xiàn)刮刮樂的方法

 更新時間:2017年10月23日 10:41:55   作者:飛鳥96  
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)刮刮樂的兩種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android studio實(shí)現(xiàn)刮刮樂的具體代碼,供大家參考,具體內(nèi)容如下

MainActivity

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }
}


第一種方法:

GuaTwo

public class GuaTwo extends View {
  /*第一種方法*/
  private Path mPath;//手刮動的path,過程
  private Paint mOutterPaint;//繪制mPath的畫筆
  private Canvas mCanvas;//臨時畫布
  private Bitmap mBitmap;//臨時圖片

  //記錄用戶path每次的開始坐標(biāo)值
  private int mLastX;
  private int mLastY;

  private Bitmap mOutterBitmap;//圖片遮罩,就是手刮動,要擦掉的那張圖

  public GuaTwo(Context context) {
    this(context, null);
  }

  public GuaTwo(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public GuaTwo(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }


  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //獲得控件的寬高
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    //初始化bitmap
    mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);

    //設(shè)置畫筆屬性
    setupOutPaint();
    mCanvas.drawColor(Color.parseColor("#c0c0c0"));
  }

  @Override
  protected void onDraw(Canvas canvas) {
    mOutterPaint.setStyle(Paint.Style.STROKE);
    mOutterPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));//Mode.DST_OUT改模式就類似橡皮檫,這個屬性設(shè)置是關(guān)鍵
    canvas.drawBitmap(mOutterBitmap, 0, 0, null);
    canvas.drawBitmap(mBitmap, 0, 0, null);
    mCanvas.drawPath(mPath, mOutterPaint);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (action) {
      case MotionEvent.ACTION_DOWN://按下
        //記錄按下的時候的X和Y值,以便于之后移動的時候繪制
        mLastX = x;
        mLastY = y;
        mPath.moveTo(mLastX, mLastY);
        break;
      case MotionEvent.ACTION_MOVE://移動
        //拿到用戶移動的X絕對值,Y軸絕對值
        int dx = Math.abs(x - mLastX);
        int dy = Math.abs(y - mLastY);
        //用戶滑動超過3像素才會改變,這個可以不做,做只是為了避免很頻繁的響應(yīng)而已。
        if (dx > 3 || dy > 3) {
          mPath.lineTo(x, y);
        }
        mLastX = x;
        mLastY = y;
        break;
    }
    invalidate();//刷新UI
    return true;
  }

  /**
   * 繪制path(也就是手刮動的path來繪制) 的畫筆屬性
   * 類似橡皮擦
   */
  private void setupOutPaint() {
    mOutterPaint.setColor(Color.RED);
    mOutterPaint.setAntiAlias(true);
    mOutterPaint.setDither(true);
    mOutterPaint.setStrokeJoin(Paint.Join.ROUND);//設(shè)置圓角
    mOutterPaint.setStrokeCap(Paint.Cap.ROUND);
    mOutterPaint.setStyle(Paint.Style.FILL);
    mOutterPaint.setStrokeWidth(60);//設(shè)置畫筆寬度
  }
  /**
   * 初始化信息
   */
  private void init() {
    mOutterPaint = new Paint();
    mPath = new Path();
    mOutterBitmap = BitmapFactory.decodeResource(getResources(),
        R.drawable.mein);
  }


第二種方法:

GuaTwo

private Path mPath;//手刮動的path,過程
  private Paint mOutterPaint;//繪制mPath的畫筆
  private Canvas mCanvas;
  private Bitmap mBitmap;

  //記錄用戶path每次的開始坐標(biāo)值
  private int mLastX;
  private int mLastY;

  private Bitmap mOutterBitmap;//圖片遮罩,就是手刮動,要擦掉的那張圖
  private String mText;//刮獎文本信息
  private Rect mTextBound;
  private Paint mBackPaint;//刮獎信息的畫筆

  public GuaTwo(Context context) {
    this(context, null);
  }

  public GuaTwo(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public GuaTwo(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }


  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //獲得控件的寬高
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    //初始化bitmap
    mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);//用指定的位圖構(gòu)造一個畫布來繪制。

    //設(shè)置畫筆屬性
    setupOutPaint();
    setUpBackPaint();

//    mCanvas.drawColor(Color.parseColor("#c0c0c0"));
    mCanvas.drawRoundRect(new RectF(0, 0, width, height), 30, 30,
        mOutterPaint);//用mOutterPaint畫圓角矩形
    mCanvas.drawBitmap(mOutterBitmap, null, new Rect(0, 0, width, height),
        null);//在剛剛畫的圓角矩形上面再畫一個bitmap圖片,讓圖片大小和圓角矩形大小相關(guān)聯(lián)
  }

  @Override
  protected void onDraw(Canvas canvas) {
    mOutterPaint.setStyle(Paint.Style.STROKE);
    mOutterPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));//Mode.DST_OUT改模式就類似橡皮檫,這個屬性設(shè)置是關(guān)鍵
    canvas.drawText(mText, (getWidth() - mTextBound.width()) / 2, getHeight() / 2 - mTextBound.height() / 2, mBackPaint);//把獲獎信息放在正中間
    mCanvas.drawPath(mPath, mOutterPaint);
    canvas.drawBitmap(mBitmap, 0, 0, null);
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    int action = event.getAction();
    int x = (int) event.getX();
    int y = (int) event.getY();

    switch (action) {
      case MotionEvent.ACTION_DOWN://按下
        //記錄按下的時候的X和Y值,以便于之后移動的時候繪制
        mLastX = x;
        mLastY = y;
        mPath.moveTo(mLastX, mLastY);
        break;
      case MotionEvent.ACTION_MOVE://移動
        //拿到用戶移動的X絕對值,Y軸絕對值
        int dx = Math.abs(x - mLastX);
        int dy = Math.abs(y - mLastY);
        //用戶滑動超過3像素才會改變,這個可以不做,做只是為了避免很頻繁的相應(yīng)而已。
        if (dx > 3 || dy > 3) {
          mPath.lineTo(x, y);
        }
        mLastX = x;
        mLastY = y;
        break;
    }
    invalidate();//刷新UI
    return true;
  }
  private void setUpBackPaint() {
    mBackPaint.setColor(Color.RED);
    mBackPaint.setStyle(Paint.Style.FILL);
    mBackPaint.setTextSize(60);
    //獲得當(dāng)前畫筆繪制文本的寬和高
    mBackPaint.getTextBounds(mText, 0, mText.length(), mTextBound);
  }
  *//**
   * 繪制path(也就是手刮動的path來繪制) 的畫筆屬性
   * 類似橡皮擦
   *//*
  private void setupOutPaint() {
    mOutterPaint.setColor(Color.RED);
    mOutterPaint.setAntiAlias(true);
    mOutterPaint.setDither(true);
    mOutterPaint.setStrokeJoin(Paint.Join.ROUND);//設(shè)置圓角
    mOutterPaint.setStrokeCap(Paint.Cap.ROUND);
    mOutterPaint.setStyle(Paint.Style.FILL);
    mOutterPaint.setStrokeWidth(60);//設(shè)置畫筆寬度
  }
  *//**
   * 初始化信息
   *//*
  private void init() {
    mOutterPaint = new Paint();
    mPath = new Path();
    mOutterBitmap = BitmapFactory.decodeResource(getResources(),
        R.drawable.huahua);
    mText = "您中獎了!";
    mTextBound = new Rect();
    mBackPaint = new Paint();
  }


布局文件

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context="com.bwie.test.guaguale.MainActivity">

  <com.bwie.test.guaguale.GuaTwo
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>

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

相關(guān)文章

最新評論