(本篇博客只说明Canvas画笔的使用,关于案例仅供学习,禁止非法使用)
对上层资源(刮的图层)进行解析
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.scratch_card);
Bitmap alterBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig());
收集屏幕信息
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double NX = bitmap.getWidth()/dm.widthPixels;
double NY = bitmap.getHeight()/dm.heightPixels;
适应屏幕进行涂顶层图层
//创建一个canvas对象
Canvas canvas = new Canvas(alterBitmap);
//画笔
Paint paint = new Paint();
//设置颜色
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
//Matrix对象
Matrix matrix = new Matrix();
//画图
canvas.drawBitmap(bitmap,matrix,paint);
Random方法随机一个值
//随机抽奖
Random XingYun = new Random();
int kelang = XingYun.nextInt(3);
String[] deng = {"一等奖","二等奖","三等奖"};
text_bg.setText(deng[kelang]);
完成消除上层图层指定位置的Bitmap内容
bg.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
try {
int x = (int)event.getX();
int y = (int)event.getY();
for (int i = -100 ; i<100 ; i++){
for (int j = -100 ; j<100 ; j++){
if (Math.sqrt((i*i) + (j*j)) <= 100){
alterBitmap.setPixel((int)(x*NX) + i , (int) (y*NY +90)+j , Color.TRANSPARENT);
}
}
}
bg.setImageBitmap(alterBitmap);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
});
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="50dp" />
<ImageView
android:id="@+id/image_bm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/scratch_card"/>
RelativeLayout>
private void init() {
bg = findViewById(R.id.image_bm);
text_bg = findViewById(R.id.text_bg);
//随机抽奖
Random XingYun = new Random();
int kelang = XingYun.nextInt(3);
String[] deng = {"一等奖","二等奖","三等奖"};
text_bg.setText(deng[kelang]);
}
private void initImage() {
//资源解析
Bitmap bitmap =
BitmapFactory.decodeResource(getResources(),R.drawable.scratch_card);
alterBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),bitmap.getConfig());
//收集屏幕信息
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
//适应屏幕
NX = bitmap.getWidth()/dm.widthPixels;
NY = bitmap.getHeight()/dm.heightPixels;
//创建一个canvas对象
Canvas canvas = new Canvas(alterBitmap);
//画笔
Paint paint = new Paint();
//设置颜色
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
//Matrix对象
Matrix matrix = new Matrix();
//画图
canvas.drawBitmap(bitmap,matrix,paint);
//监听
bg.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
try {
int x = (int)event.getX();
int y = (int)event.getY();
for (int i = -100 ; i<100 ; i++){
for (int j = -100 ; j<100 ; j++){
if (Math.sqrt((i*i) + (j*j)) <= 100){
alterBitmap.setPixel((int)(x*NX) + i , (int) (y*NY +90)+j , Color.TRANSPARENT);
}
}
}
bg.setImageBitmap(alterBitmap);
}catch (Exception e){
e.printStackTrace();
}
return true;
}
});
}
CSDN:https://download.csdn.net/download/weixin_48916759/87920165
gitee:https://gitee.com/xu-pq/android-demo/tree/master/CanvasDemo