Android studio实现刮刮乐的方法

发布时间:2020-09-23 12:20:27 作者:飞鸟96
来源:脚本之家 阅读:164

本文实例为大家分享了Android studio实现刮刮乐的具体代码,供大家参考,具体内容如下

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每次的开始坐标值
  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);

    //设置画笔属性
    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改模式就类似橡皮檫,这个属性设置是关键
    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像素才会改变,这个可以不做,做只是为了避免很频繁的响应而已。
        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);//设置圆角
    mOutterPaint.setStrokeCap(Paint.Cap.ROUND);
    mOutterPaint.setStyle(Paint.Style.FILL);
    mOutterPaint.setStrokeWidth(60);//设置画笔宽度
  }
  /**
   * 初始化信息
   */
  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每次的开始坐标值
  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);//用指定的位图构造一个画布来绘制。

    //设置画笔属性
    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图片,让图片大小和圆角矩形大小相关联
  }

  @Override
  protected void onDraw(Canvas canvas) {
    mOutterPaint.setStyle(Paint.Style.STROKE);
    mOutterPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));//Mode.DST_OUT改模式就类似橡皮檫,这个属性设置是关键
    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像素才会改变,这个可以不做,做只是为了避免很频繁的相应而已。
        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);
    //获得当前画笔绘制文本的宽和高
    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);//设置圆角
    mOutterPaint.setStrokeCap(Paint.Cap.ROUND);
    mOutterPaint.setStyle(Paint.Style.FILL);
    mOutterPaint.setStrokeWidth(60);//设置画笔宽度
  }
  *//**
   * 初始化信息
   *//*
  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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. Android studio 实现圆形ImageView的方法步骤
  2. Android Studio如何查看源码并调试

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android studio 刮刮乐

上一篇:python中import如何使用

下一篇:pip如何安装python库

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》