心得感悟
長這么大兄一,就只中過瓜子再來一包的小獎睛驳,玩游戲從來都是非洲人盐欺,但是今天這個Demo可以讓自己體驗一把歐洲人的快樂泊柬![微笑中帶著淚水.jpg]但是這個Demo是跟著老師寫的蜜唾,自已還不是很明白,有不對的地方后面會更改庶艾。
8.26
寫完這篇文章的早上我真的抽到了閃卡,感動哭了擎勘!
內(nèi)容簡概
- 一咱揍、前期圖片準備
- 二、編寫代碼
- 三棚饵、運行效果
- 四煤裙、程序運行結(jié)果分析
具體內(nèi)容
一、前期圖片準備
(1)找一張
幸運彩票噪漾,或者游戲高配卡的圖片
(2)再復(fù)制
一份進行二次編輯
硼砰,涂上刮刮樂專屬顏色——灰色
(3)最后將其復(fù)制到app>res>drawable目錄下
?大功告成????!!!
二、編寫代碼
public class MainActivity extends AppCompatActivity {
ImageView foreground;
Bitmap orgBitmap;
Bitmap copyBitmap;
Canvas canvas;
Paint paint;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 配置界面
setContentView(R.layout.activity_main);
// 找到容器視圖里面的圖片視圖控件
//findViewById
foreground = findViewById(R.id.iv_foreground);
// 將需要操作的圖片讀取出來 Bitmap
// BitmapFactory 管理位圖
// decodeResource 從工程資源路徑去生成一張位圖
// getResources獲取工程資源
// R.drawable.xiugai 訪問資源路徑下 drawable 里面的一個文件
orgBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.xiugai);
// 操作這張圖片 用透明色替換
// 不能操作原圖 只能拷貝一份
copyBitmap = Bitmap.createBitmap(orgBitmap.getWidth(),orgBitmap.getHeight(),orgBitmap.getConfig());
// 創(chuàng)建一個Canvas 畫布
canvas = new Canvas(copyBitmap);
// 創(chuàng)建一個畫筆
paint = new Paint();
// 創(chuàng)建一個矩陣
Matrix matrix = new Matrix();
// 旋轉(zhuǎn)圖片
// matrix.setRotate(90,240,400);
// 平移
// matrix.setTranslate(100,0);
// 翻轉(zhuǎn) set作用一次 post作用多次
// matrix.setScale(0.5f,1f);
// matrix.postTranslate(orgBitmap.getWidth(),0);
// 畫一幅圖
canvas.drawBitmap(orgBitmap,matrix,paint);
// 顯示圖片
foreground.setImageBitmap(copyBitmap);
// 給前景圖片添加touch事件
// 當有觸摸事件發(fā)生 系統(tǒng)會監(jiān)聽并回調(diào)該事件
foreground.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// 獲取當前事件
int action = motionEvent.getAction();
// 判斷狀態(tài)
if (action == MotionEvent.ACTION_MOVE){
// 獲取觸摸點的坐標
int x = (int) motionEvent.getX();
int y = (int) motionEvent.getY();
for(int i = 0; i < 100;i++) {
for (int j = -20; j < 200; j++) {
copyBitmap.setPixel(x + i, y + j, Color.TRANSPARENT);
}
}
foreground.setImageBitmap(copyBitmap);
}
return true;
}
});
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:id="@+id/fl_main">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/yuantu">
</ImageView>
<ImageView
android:id="@+id/iv_foreground"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/yuantu2"
>
</ImageView>
</FrameLayout>
三欣硼、運行效果
四题翰、程序運行結(jié)果分析
1. 刮除不連貫
這是因為我們將motionEvent設(shè)置為int類型
,導(dǎo)致其只能一個像素一個像素地刮除诈胜。
2. 手觸摸屏幕的位置和刮除位置不一致
這是因為圖片大小與手機界面不匹配
豹障,只要找一張與手機界面大小一致的圖片就可以解決。
3. 中間平移等代碼的理解
提供參考
焦匈,有興趣的朋友可以取消注釋血公,自己感受一下這些功能。
4. 其他bug缓熟,比如有些圖片用這個代碼不能運行成功
具體原因我還不了解累魔,水平有限,日后弄懂了定回來改正
!!!