Android刮刮卡功能具体实现代码
短信预约 -IT技能 免费直播动态提醒
今天整理之前的代码,忽然看到之前自己写的一个刮刮卡,整理下以便以后使用,同时分享给需要的朋友,如有错误,还请多多指正。
实现的步骤,其实就是徒手画三个图层叠加在一起,最上层是绘制需要的问题,就是以上所述的“骚年,刮我吧”,第二层就是覆盖宽高的灰层,第三层是结果层,多的不啰嗦了,具体实现如下,附上详细注释。
public class ScratchView extends View
{
public ScratchView(Context context)
{
super(context);
init();
}
private Canvas mCanvas = null;
private Path mPath = null;
private Paint mPaint = null;
// 定义画布的宽和高
private int screenWidth = 720;
private int screenHeight = 360;
private Bitmap bitmap = null;
private void init() {
// TODO Auto-generated method stub
mPath = new Path();
bitmap = Bitmap.createBitmap(screenWidth, screenHeight,
Config.ARGB_8888);
// 对mPaint的设置
mPaint = new Paint();
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint.setAntiAlias(true);
mCanvas = new Canvas();
mPaint.setDither(true);
// 设置画笔为空心
mPaint.setStyle(Style.STROKE);
// 设置线宽,即每次擦除的宽度
mPaint.setStrokeWidth(10);
mPaint.setStrokeCap(Cap.ROUND);
mPaint.setStrokeJoin(Join.ROUND);
// 设置图形重叠时的处理方式,一共有16种方式,有兴趣可自己查阅
mPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
mPaint.setAlpha(0);
mCanvas = new Canvas(bitmap);
mCanvas.drawColor(Color.parseColor("#c0c0c0"));
setBitmapText();
}
private void setBitmapText() {
Paint paint = new Paint();
paint.setTextSize(40);
paint.setColor(Color.parseColor("#9f9fa0"));
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.CENTER);
paint.setFakeBoldText(true);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.alpha(0));
canvas.rotate(-20);
// 遍历绘制文字
for (int i = 0; i < screenWidth + 200; i += 300)
{
for (int j = 0; j < screenHeight + 200; j += 60)
{
canvas.drawText("刮我吧,骚年!", i, j, paint);
}
}
setScratchBackground("一等奖");
}
// 接收后台传来的文字,即中奖或者未中奖的文字
public void setScratchBackground(String txt_win) {
// TODO Auto-generated method stub
Paint paint = new Paint();
Bitmap bitmap = Bitmap.createBitmap(screenWidth, screenHeight,
Config.ARGB_8888);
paint.setTextSize(40);
paint.setColor(Color.BLACK);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setAntiAlias(true);
paint.setTextAlign(Paint.Align.CENTER);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.alpha(0));
canvas.drawText(txt_win, screenWidth / 2, 60, paint);
setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
mCanvas.drawPath(mPath, mPaint);
canvas.drawBitmap(bitmap, 0, 0, null);
}
int x = 0;
int y = 0;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
int currX = (int) event.getX();
int currY = (int) event.getY();
switch (action)
{
case MotionEvent.ACTION_DOWN:
{
mPath.reset();
x = currX;
y = currY;
mPath.moveTo(x, y);
}
break;
case MotionEvent.ACTION_MOVE:
{
mPath.quadTo(x, y, currX, currY);
x = currX;
y = currY;
postInvalidate();
}
break;
case MotionEvent.ACTION_UP:
{
new Thread(mRunnable).start();
}
case MotionEvent.ACTION_CANCEL:
{
mPath.reset();
}
break;
}
return true;
}
private Runnable mRunnable = new Runnable()
{
private int[] mPixels;
@Override
public void run() {
float wipeArea = 0;
float totalArea = screenWidth * screenHeight;
Bitmap mBitmap = bitmap;
mPixels = new int[screenWidth * screenHeight];
mBitmap.getPixels(mPixels, 0, screenWidth, 0, 0, screenWidth,
screenHeight);
for (int i = 0; i < screenWidth; i++)
{
for (int j = 0; j < screenHeight; j++)
{
int index = i + j * screenWidth;
if (mPixels[index] == 0)
{
wipeArea++;
}
}
}
if (wipeArea > 0 && totalArea > 0)
{
int percent = (int) (wipeArea * 100 / totalArea);
if (percent > 20)
{
Looper.prepare();
Toast.makeText(getContext(), "已刮开" + percent + "%",
Toast.LENGTH_LONG).show();
Looper.loop();
}
}
}
};
}
发的是公司需要的效果,以上代码只是一个实现,各种样式还需要自己去实现。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341