分享Android仿刮奖效果控件

作者:lijiao 时间:2021-12-15 16:00:16 

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

标签:Android,刮奖
0
投稿

猜你喜欢

  • Java实现SSL双向认证的方法

    2023-09-22 10:34:35
  • 很详细的android序列化过程Parcelable

    2021-09-15 20:03:51
  • SpringBoot + FFmpeg实现一个简单的M3U8切片转码系统

    2023-11-24 19:52:38
  • Springboot2.1.6集成activiti7出现登录验证的实现

    2022-08-03 10:44:48
  • 解决IDEA无法下载maven依赖的问题

    2023-12-05 17:06:59
  • Android studio 3.0安装配置方法图文教程

    2021-05-26 14:48:42
  • 详解JAVA 时间处理相关类

    2022-11-07 03:12:36
  • Java字符串编码解码性能提升的技巧分享

    2022-01-30 08:45:13
  • 详细分析Java Lambda表达式

    2021-06-20 04:32:19
  • Java压缩解压zip技术_动力节点Java学院整理

    2022-08-29 22:50:42
  • SpringBoot停止启动时测试检查rabbitmq操作

    2023-04-06 10:05:05
  • C#8 的模式匹配实现

    2023-02-11 16:22:16
  • C#实现快捷键的几种常用方法汇总

    2021-08-23 15:01:54
  • C# 通过反射获取类型的字段值及给字段赋值的操作

    2021-11-14 13:30:23
  • 启用springboot security后登录web页面需要用户名和密码的解决方法

    2022-01-29 06:12:07
  • Android中基于XMPP协议实现IM聊天程序与多人聊天室

    2022-05-06 07:37:51
  • Java线程并发中常见的锁机制详细介绍

    2023-07-04 05:33:33
  • android之计时器(Chronometer)的使用以及常用的方法

    2021-05-26 12:27:33
  • 详解使用Spring Security进行自动登录验证

    2021-07-28 12:31:51
  • C#实现自定义Dictionary类实例

    2022-12-19 09:23:47
  • asp之家 软件编程 m.aspxhome.com