`

刮刮卡效果

 
阅读更多


刮刮卡类:
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 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
  • 大小: 25 KB
分享到:
评论
1 楼 zjc198805 2014-07-07  
lz,如何监听当前已经挂了70%之类的?

相关推荐

    html5 刮刮卡效果

    在刮刮卡效果中,当用户在Canvas上移动鼠标或手指时,我们需要监听`mousedown`、`mousemove`和`mouseup`事件,以实现刮开的效果。 3. **图形状态管理**: `save()`和`restore()`方法用于保存和恢复Canvas的状态。在...

    html5手机刮刮卡效果_手指滑动刮刮卡特效

    HTML5手机刮刮卡效果是一种基于现代Web技术实现的互动元素,主要应用于移动设备上的网页设计,为用户提供一种类似于实物刮刮卡的游戏体验。这种效果是通过JavaScript、CSS3以及HTML5的新特性来构建的,使得用户可以...

    Android 刮刮卡效果 自定义控件实现

    在Android开发中,有时我们需要为应用添加一些独特的交互体验,比如刮刮卡效果。这个刮刮卡效果可以用于各种场景,比如抽奖、解锁隐藏内容等。本教程将介绍如何通过自定义控件来实现这一效果。 首先,我们需要理解...

    android刮刮卡效果实现

    在Android开发中,实现刮刮卡效果是一种常见的交互设计,常用于抽奖、解锁等功能,给用户带来趣味性。本文将详细讲解如何在Android平台上创建一个刮刮卡效果,包括使用圆形图片的实现。 首先,刮刮卡效果的核心是...

    Unity插件 实现 刮刮卡/刮刮乐 效果

    7. **示例资源**:提供的"1704942063332.jpg"可能是一个展示刮刮卡效果的截图,而"Scratch Card 1.6.unitypackage"则是包含插件源代码和资源的包文件。开发者可以通过导入这个包来快速开始使用刮刮卡功能。 总结来...

    2种使用html5实现刮刮卡效果

    刮刮卡效果的核心部分就是在Canvas上进行绘制。通过JavaScript,我们可以获取Canvas的2D渲染上下文,并用它来画出刮刮卡的背景、覆盖层以及刮开的效果。 1. **基础刮刮卡实现**: - 使用HTML创建一个Canvas元素,...

    android仿微信界面+刮刮卡效果

    本项目旨在帮助开发者学习如何实现这样的界面,并结合刮刮卡效果,为用户提供更具趣味性的交互体验。接下来,我们将详细探讨这两个主要知识点。 一、仿微信界面 1. 主界面布局:微信的主界面通常包括底部导航栏,...

    HTML5实现刮刮卡效果

    刮刮卡效果是一种常见的互动元素,常见于促销活动或抽奖游戏中,用户通过鼠标或触控操作刮开一层覆盖层,显示隐藏的内容。以下是如何使用HTML5和canvas实现这一效果的步骤: 1. **创建canvas元素**: 首先,在HTML...

    移动端H5刮刮卡效果

    移动端刮刮卡效果 使用了canvas,奖品可以自己用 canvas 生成一下。朋友推荐的一个插件写的,挺好用的

    html5刮刮卡效果

    在刮刮卡效果中,Canvas是核心元素,用来创建刮刮区域。通过设置Canvas的宽高和位置,可以将它放置在网页的任何地方。 2. **绘图函数**:Canvas API提供了丰富的绘图方法,如`fillRect()`用于填充矩形,`clearRect...

Global site tag (gtag.js) - Google Analytics