




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android中如何自定义刮刮卡
/upload/information/20200623/125/127721.gif/upload/information/20200623/125/127722.gif所涉及的知识点:实现思路://背景图
mBackGroundBitmap
=
BitmapFactory.decodeResource(getResources(),
R.mipmap.background);
@Override
protected
void
onDraw(Canvas
canvas)
{
//绘制背景层
canvas.drawBitmap(mBackGroundBitmap,
0,
0,
null);
}//背景图
mBackGroundBitmap
=
BitmapFactory.decodeResource(getResources(),
R.mipmap.background);
//创建一个和背景图大小一致的Bitmap对象作为装载画布
mForeGroundBitmap
=
Bitmap.createBitmap(mBackGroundBitmap.getWidth(),
mBackGroundBitmap.getHeight(),
Config.ARGB_8888);
//与Canvas进行绑定
mCanvas
=
new
Canvas(mForeGroundBitmap);
//涂成灰色
mCanvas.drawColor(Color.GRAY);
@Override
protected
void
onDraw(Canvas
canvas)
{
//绘制背景层
canvas.drawBitmap(mBackGroundBitmap,
0,
0,
null);
//绘制前景层
canvas.drawBitmap(mForeGroundBitmap,
0,
0,
null);
}/upload/information/20200623/125/127723.jpgmPaint
=
new
Paint();
mPaint.setAlpha(0);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeWidth(80);
mPaint.setXfermode(new
PorterDuffXfermode(PorterDuff.Mode.DST_IN));
@Override
public
boolean
onTouchEvent(MotionEvent
event)
{
switch
(event.getAction())
{
case
MotionEvent.ACTION_DOWN:
mLastX
=
(int)
event.getX();
mLastY
=
(int)
event.getY();
mPath.moveTo(mLastX,
mLastY);
break;
case
MotionEvent.ACTION_MOVE:
mLastX
=
(int)
event.getX();
mLastY
=
(int)
event.getY();
mPath.lineTo(mLastX,
mLastY);
break;
case
MotionEvent.ACTION_UP:
break;
default:
break;
}
mCanvas.drawPath(mPath,
mPaint);
invalidate();
return
true;
}/upload/information/20200623/125/127724.png//文字画笔
mTextPaint
=
new
Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setColor(Color.GREEN);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setTextSize(30);
mTextPaint.getTextBounds(mText,
0,
mText.length(),
mRect);@Override
protected
void
onDraw(Canvas
canvas)
{
canvas.drawText(mText,
mBitmap.getWidth()
/
2
-
mRect.width()
/
2,
mBitmap.getHeight()
/
2
+
mRect.height()
/
2,
mTextPaint);
}//通过资源文件创建Bitmap对象
mBitmap
=
BitmapFactory.decodeResource(getResources(),
R.mipmap.background);
//新建同等大小的Bitmap对象
mForeBitmap
=
Bitmap.createBitmap(mBitmap.getWidth(),
mBitmap.getHeight(),
Bitmap.Config.ARGB_8888);
//双缓冲,装载画布
mForeCanvas
=
new
Canvas(mForeBitmap);
mForeCanvas.drawBitmap(mBitmap,
0,
0,
null);private
Runnable
mRunnable
=
new
Runnable()
{
int[]
pixels;
@Override
public
void
run()
{
int
w
=
mForeBitmap.getWidth();
int
h
=
mForeBitmap.getHeight();
float
wipeArea
=
0;
float
totalArea
=
w
*
h;
pixels
=
new
int[w
*
h];
/**
*
pixels
接收位图颜色值的数组
*
offset
写入到pixels[]中的第一个像素索引值
*
stride
pixels[]中的行间距个数值(必须大于等于位图宽度)。可以为负数
*
x
从位图中读取的第一个像素的x坐标值。
*
y
从位图中读取的第一个像素的y坐标值
*
width
从每一行中读取的像素宽度
*
height
读取的行数
*/
mForeBitmap.getPixels(pixels,
0,
w,
0,
0,
w,
h);
for
(int
i
=
0;
i
<
w;
i++)
{
for
(int
j
=
0;
j
<
h;
j++)
{
int
index
=
i
+
j
*
w;
if
(pixels[index]
==
0)
{
wipeArea++;
}
}
}
if
(wipeArea
>
0
&&
totalArea
>
0)
{
int
percent
=
(int)
(wipeArea
*
100
/
totalArea);
if
(percent
>
50)
{
isClear
=
true;
postInvalidate();
}
}
}
};/upload/information/20200623/125/127725.png
@Override
protected
void
onDraw(Canvas
canvas)
{
canvas.drawText(mText,
mForeBitmap.getWidth()
/
2
-
mRect.width()
/
2,
mForeBitmap.getHeight()
/
2
+
mRect.height()
/
2,
mTextPaint);
if
(!isClear)
{
canvas.drawBitmap(mForeBitmap,
0,
0,
null);
}
}package
com.lcw.view;
import
android.content.Context;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
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.util.AttributeSet;
import
android.view.MotionEvent;
import
android.view.View;
/**
*
刮刮卡(完善版)
*
Create
by:
chenwei.li
*
Date:
2017/7/22
*
Time:
下午7:25
*/
public
class
ScratchCardView2
extends
View
{
//处理文字
private
String
mText
=
"恭喜您中奖啦!!";
private
Paint
mTextPaint;
private
Rect
mRect;
//处理图层
private
Paint
mForePaint;
private
Path
mPath;
private
Bitmap
mBitmap;//加载资源文件
private
Canvas
mForeCanvas;//前景图Canvas
private
Bitmap
mForeBitmap;//前景图Bitmap
//记录位置
private
int
mLastX;
private
int
mLastY;
private
volatile
boolean
isClear;//标志是否被清除
public
ScratchCardView2(Context
context)
{
this(context,
null);
}
public
ScratchCardView2(Context
context,
AttributeSet
attrs)
{
this(context,
attrs,
0);
}
public
ScratchCardView2(Context
context,
AttributeSet
attrs,
int
defStyleAttr)
{
super(context,
attrs,
defStyleAttr);
init();
}
private
void
init()
{
mRect
=
new
Rect();
mPath
=
new
Path();
//文字画笔
mTextPaint
=
new
Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setColor(Color.GREEN);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setTextSize(30);
mTextPaint.getTextBounds(mText,
0,
mText.length(),
mRect);
//擦除画笔
mForePaint
=
new
Paint();
mForePaint.setAntiAlias(true);
mForePaint.setAlpha(0);
mForePaint.setStrokeCap(Paint.Cap.ROUND);
mForePaint.setStrokeJoin(Paint.Join.ROUND);
mForePaint.setStyle(Paint.Style.STROKE);
mForePaint.setStrokeWidth(30);
mForePaint.setXfermode(new
PorterDuffXfermode(PorterDuff.Mode.DST_IN));
//通过资源文件创建Bitmap对象
mBitmap
=
BitmapFactory.decodeResource(getResources(),
R.mipmap.background);
mForeBitmap
=
Bitmap.createBitmap(mBitmap.getWidth(),
mBitmap.getHeight(),
Bitmap.Config.ARGB_8888);
//双缓冲,装载画布
mForeCanvas
=
new
Canvas(mForeBitmap);
mForeCanvas.drawBitmap(mBitmap,
0,
0,
null);
}
@Override
protected
void
onDraw(Canvas
canvas)
{
canvas.drawText(mText,
mForeBitmap.getWidth()
/
2
-
mRect.width()
/
2,
mForeBitmap.getHeight()
/
2
+
mRect.height()
/
2,
mTextPaint);
if
(!isClear)
{
canvas.drawBitmap(mForeBitmap,
0,
0,
null);
}
}
@Override
public
boolean
onTouchEvent(MotionEvent
event)
{
switch
(event.getAction())
{
case
MotionEvent.ACTION_DOWN:
mLastX
=
(int)
event.getX();
mLastY
=
(int)
event.getY();
mPath.moveTo(mLastX,
mLastY);
break;
case
MotionEvent.ACTION_MOVE:
mLastX
=
(int)
event.getX();
mLastY
=
(int)
event.getY();
mPath.lineTo(mLastX,
mLastY);
break;
case
MotionEvent.ACTION_UP:
new
Thread(mRunnable).start();
break;
default:
break;
}
mForeCanvas.drawPath(mPath,
mForePaint);
invalidate();
return
true;
}
/**
*
开启子线程计算被擦除的像素点
*/
private
Runnable
mRunnable
=
new
Runnable()
{
int[]
pixels;
@Override
public
void
run()
{
int
w
=
mForeBitmap.getWidth();
int
h
=
mForeBitmap.getHeight();
flo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 海口市二模中考数学试卷
- 城市空间文化映射-洞察及研究
- 呼吸道肿瘤免疫特征-洞察及研究
- 员工健康促进方案设计-洞察及研究
- 广东省1月学考数学试卷
- 实时视觉场景理解-洞察及研究
- 合肥市初中生数学试卷
- 心脏功能评估-洞察及研究
- 广西桂林临桂数学试卷
- 贵州六盘水高一数学试卷
- 碳化硅培训课件
- 2025年三门峡卢氏县事业单位(联考)招聘81人笔试模拟试题及答案
- 2025年公需科目考试试卷(含答案)
- 暑假教研活动方案
- 2025年广西中考物理试题及答案
- 2024年北京市海淀区招聘社区工作者考试真题
- 2025年 四川省港航投资集团有限责任公司招聘考试笔试试卷附答案
- 干眼的药物治疗讲课件
- 2024年武汉市汉阳区招聘社区干事笔试真题
- 国企往来款管理制度
- 【漳州片仔癀人力资源管理现状、问题及对策9000字】
评论
0/150
提交评论