欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android studio实现刮刮乐的方法

程序员文章站 2023-12-13 22:53:16
本文实例为大家分享了android studio实现刮刮乐的具体代码,供大家参考,具体内容如下 mainactivity public class main...

本文实例为大家分享了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>

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

上一篇:

下一篇: