分享Android仿刮奖效果控件

 更新时间:2016年09月05日 16:03:11   投稿:lijiao  
这篇文章主要为大家分享了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虚拟机Dalvik和ART科普

    Android虚拟机Dalvik和ART科普

    这篇文章主要为大家介绍了Android虚拟机Dalvik和ART科普详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-01-01
  • Android项目迁移到AndroidX的方法步骤

    Android项目迁移到AndroidX的方法步骤

    这篇文章主要介绍了Android项目迁移到AndroidX的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • 分析Android Choreographer源码

    分析Android Choreographer源码

    Choreographer的作用主要是配合Vsync,给上层App的渲染提供一个稳定的Message处理的时机,也就是Vsync到来的时候,系统通过对Vsync信号周期的调整,来控制每一帧绘制操作的时机
    2021-06-06
  • Android setTag方法的key问题解决办法

    Android setTag方法的key问题解决办法

    这篇文章主要介绍了Android setTag方法的key问题解决办法的相关资料,需要的朋友可以参考下
    2016-09-09
  • Android:下拉刷新+加载更多+滑动删除实例讲解

    Android:下拉刷新+加载更多+滑动删除实例讲解

    本文主要讲解 Android下拉刷新+加载更多+滑动删除的示例,这里整理了相关资料并附示例代码供大家学习参考,有需要的小伙伴可以参考下
    2016-08-08
  • Android实现地理定位功能

    Android实现地理定位功能

    这篇文章主要为大家详细介绍了Android实现地理定位功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-10-10
  • Android  LayoutInflater.inflate()详解及分析

    Android LayoutInflater.inflate()详解及分析

    这篇文章主要介绍了Android LayoutInflater.inflate()详解及分析的相关资料,需要的朋友可以参考下
    2017-01-01
  • Android中dataBinding使用的简单封装

    Android中dataBinding使用的简单封装

    前面一段时间学习了一下Android中的DataBinding,但是只是很简单地实现了一下,DataBinding中最强大的地方还没有认真地学习过,有很多地方还不理解,下面这篇文章主要给大家介绍了关于Android中dataBinding使用的简单封装,需要的朋友可以参考下
    2023-06-06
  • Android7.0自动更新适配 包解析异常

    Android7.0自动更新适配 包解析异常

    这篇文章主要为大家详细介绍了Android7.0自动更新适配,包解析异常的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Kotlin中的扩展函数与属性示例详解

    Kotlin中的扩展函数与属性示例详解

    理论上来说,扩展函数很简单,它就是一个类的成员函数,不过定义在类的外面。下面这篇文章主要给大家介绍了关于Kotlin中扩展函数与属性的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下
    2018-06-06

最新评论