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

分享Android仿刮奖效果控件

程序员文章站 2024-03-06 11:14:55
本文实例为大家分享了android刮刮卡效果控件,供大家参考,具体内容如下 刮刮卡类: package com.reyo.view; impor...

本文实例为大家分享了android刮刮卡效果控件,供大家参考,具体内容如下

分享Android仿刮奖效果控件

刮刮卡类:

package com.reyo.view;

import android.content.context;
import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.canvas;
import android.graphics.paint;
import android.graphics.path;
import android.graphics.porterduff;
import android.graphics.porterduffxfermode;
import android.util.attributeset;
import android.view.motionevent;
import android.widget.textview;

/**
 * 刮刮卡类
 * @author reyo
 *
 */
public class scratchtextview extends textview{

 private float touch_tolerance;
 private bitmap mbitmap;
 private canvas mcanvas;
 private paint mpaint;
 private path mpath;
 private float mx,my;

 private boolean isdraw = false;

 public scratchtextview(context context) {
 super(context);

 }
 public scratchtextview(context context, attributeset attrs, int defstyle) {
 super(context, attrs, defstyle);
 }

 public scratchtextview(context context, attributeset attrs) {
 super(context, attrs);
 }

 @override
 protected void ondraw(canvas canvas) {
 super.ondraw(canvas);
 if (isdraw) {
  mcanvas.drawpath(mpath, mpaint);
//  mcanvas.drawpoint(mx, my, mpaint);
  canvas.drawbitmap(mbitmap, 0, 0, null);
 }
 }

 /**
 * 初始化刮刮卡
 * @param bgcolor 刮刮卡背景色
 * @param paintstrokewidth 擦除线宽
 * @param touchtolerance 画线容差
 */
 public void initscratchcard(final int bgcolor,final int paintstrokewidth,float touchtolerance) {
 touch_tolerance = touchtolerance;
 mpaint = new paint();
// mpaint.setalpha(0);
// mpaint.setcolor(color.black);
// mpaint.setxfermode(new porterduffxfermode(porterduff.mode.dst_out));
 
 mpaint.setalpha(0);
 mpaint.setxfermode(new porterduffxfermode(porterduff.mode.dst_in));

 mpaint.setantialias(true);
 mpaint.setdither(true);
 mpaint.setstyle(paint.style.stroke);
 mpaint.setstrokejoin(paint.join.round);
 mpaint.setstrokecap(paint.cap.round);
 mpaint.setstrokewidth(paintstrokewidth);

 mpath = new path();
 
 mbitmap = bitmap.createbitmap(getlayoutparams().width, getlayoutparams().height, config.argb_8888);
 mcanvas = new canvas(mbitmap);

 mcanvas.drawcolor(bgcolor);
 isdraw = true;
 }

 @override
 public boolean ontouchevent(motionevent event) {
 if (!isdraw) {
  return true;
 }
 switch (event.getaction()) {
 case motionevent.action_down:
  touchdown(event.getx(), event.gety());
  invalidate();
  break;
 case motionevent.action_move:
  touchmove(event.getx(), event.gety());
  invalidate();
  break;
 case motionevent.action_up:
  touchup(event.getx(), event.gety());
  invalidate();
  break;
 default:
  break;
 }
 return true;
 }


 private void touchdown(float x,float y){
 mpath.reset();
 mpath.moveto(x, y);
 mx = x;
 my = y;
 }

 private void touchmove(float x,float y){
 float dx = math.abs(x - mx);
 float dy = math.abs(y - my);
 if (dx >= touch_tolerance || dy >= touch_tolerance) {
  mpath.quadto(mx, my, (x + mx)/2, (y + my)/2);
  mx = x;
  my = y;
 }

 }

 private void touchup(float x,float y){
 mpath.lineto(x, y);
 mcanvas.drawpath(mpath, mpaint);
 mpath.reset();
 }

}

使用:

package com.reyo.app;

import com.reyo.view.scratchtextview;

import android.app.activity;
import android.content.context;
import android.graphics.bitmap;
import android.graphics.bitmap.config;
import android.graphics.canvas;
import android.graphics.color;
import android.graphics.paint;
import android.graphics.path;
import android.graphics.porterduff;
import android.graphics.porterduffxfermode;
import android.graphics.rect;
import android.graphics.drawable.bitmapdrawable;
import android.graphics.drawable.drawable;
import android.os.bundle;
import android.util.displaymetrics;
import android.view.motionevent;
import android.view.view;

public class rubberactivity extends activity {

 @override
 public void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 // setcontentview(new rubberview(this,"title",new rect(100, 200,300,250),5,1f,18));
 setcontentview(r.layout.main);
 ((scratchtextview) findviewbyid(r.id.rubbler)).initscratchcard(0xffffffff, 5, 1f);
  
 }

 class rubberview extends view {

 private final int paint_stroke_width;
 private final float touch_tolerance;
 private final int text_size;

 private bitmap mbitmap;
 private canvas mcanvas;
 private paint mpaint;
 private path mpath;
 private float mx, my;
 private final int x, y, w, h;

 private final rect touchrect;

 public rubberview(context context, string bgtext, rect rect,
  int paintstrokewidth, float touchtolerance, int textsize) {
  super(context);
  setfocusable(true);
  touchrect = rect;
  w = rect.right - rect.left;
  h = rect.bottom - rect.top;
  x = rect.left;
  y = rect.top;
  text_size = textsize;
  paint_stroke_width = paintstrokewidth;
  touch_tolerance = touchtolerance;
  setbackground(touchrect, bgtext);
  initdrowtools();
 }

 private void setbackground(rect rect, string bgtext) {
  displaymetrics dm = new displaymetrics();
  dm = this.getresources().getdisplaymetrics();

  bitmap bitmap = bitmap.createbitmap(dm.widthpixels,
   dm.heightpixels, config.argb_8888);
  canvas canvas = new canvas(bitmap);

  paint paint = new paint();
  paint.setcolor(0x88000000);
  // paint.setstyle(style.stroke);
  // paint.settextalign(align.center);
  paint.settextsize(text_size);

  // paint.settextscalex(1.5f);
  canvas.drawcolor(color.white);
  int x = rect.left + (rect.right - rect.left - bgtext.length() * text_size) / 2;
  int y = rect.top + (rect.bottom - rect.top - text_size) / 2;
  // int y = 218+25;
  canvas.drawtext(bgtext, x, y, paint);
  drawable drawable = new bitmapdrawable(bitmap);
  setbackgrounddrawable(drawable);
 }

 private void initdrowtools() {
  mpaint = new paint();
  // mpaint.setalpha(0);
//  mpaint.setcolor(color.black); 
//  mpaint.setxfermode(new porterduffxfermode(porterduff.mode.dst_out));
  
  mpaint.setalpha(0);
  mpaint.setxfermode(new porterduffxfermode(porterduff.mode.dst_in));

  mpaint.setantialias(true);
  mpaint.setdither(true);
  mpaint.setstyle(paint.style.stroke);
  mpaint.setstrokejoin(paint.join.round); 
  mpaint.setstrokecap(paint.cap.round); 
  mpaint.setstrokewidth(paint_stroke_width); 

  mpath = new path();
  
  mbitmap = bitmap.createbitmap(w, h, config.argb_8888);
  mcanvas = new canvas(mbitmap);
  mcanvas.drawcolor(0x88000000);

 }

 @override
 protected void ondraw(canvas canvas) {
  super.ondraw(canvas);
  mcanvas.drawpath(mpath, mpaint);
  // mcanvas.drawpoint(mx, my, mpaint);
  canvas.drawbitmap(mbitmap, x, y, null);
 }

 @override
 public boolean ontouchevent(motionevent event) {
  if (!touchrect.contains((int) event.getx(), (int) event.gety())) {
  return false;
  }

  switch (event.getaction()) {
  case motionevent.action_down: 
  touchdown(event.getrawx(), event.getrawy());
  touchdown(event.getx() - touchrect.left, event.gety()
   - touchrect.top);
  invalidate();
  break;
  case motionevent.action_move: 
  touchmove(event.getx() - touchrect.left, event.gety()
   - touchrect.top);

  invalidate();
  break;
  case motionevent.action_up: 
  touchup(event.getx() - touchrect.left, event.gety()
   - touchrect.top);
  invalidate();
  break;
  default:
  break;
  }
  return true;
 }

 private void touchdown(float x, float y) {
  mpath.reset();
  mpath.moveto(x, y);
  mx = x;
  my = y;
 }

 private void touchmove(float x, float y) {
  float dx = math.abs(x - mx);
  float dy = math.abs(y - my);
  if (dx >= touch_tolerance || dy >= touch_tolerance) {
  mpath.quadto(mx, my, (x + mx) / 2, (y + my) / 2);
  mx = x;
  my = y;
  }

 }

 private void touchup(float x, float y) {
  mpath.lineto(x, y);
  mcanvas.drawpath(mpath, mpaint);
  mpath.reset();
 }

 }
}

布局xml代码:

<?xml version="1.0" encoding="utf-8"?>
<absolutelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" 
>
<!--
 必须设置
 android:layout_width
 android:layout_height 
 的值为常量
-->
<com.reyo.view.scratchtextview
 android:id="@+id/rubbler"
 android:layout_y="100dip"
 android:layout_x="100dip"
  android:layout_width="100dip" 
  android:layout_height="50dip" 
  android:text="谢谢您"
  android:textsize="18sp"
  android:gravity="center"
  />
</absolutelayout>

仿刮奖效果的控件:https://github.com/winsontan520/android-wscratchview

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