Android使用Canvas對象實現(xiàn)刮刮樂效果
在淘寶、京東等電商舉辦活動的時候,經(jīng)常可以看到在移動客戶端推出的各種刮獎活動,而這種活動也受到了很多人的喜愛。從客戶端的體驗來說,這種效果應(yīng)該是通過網(wǎng)頁來實現(xiàn)的,那么,我們使用Android的自帶控件能不能實現(xiàn)這種刮刮樂的效果呢?當(dāng)然可以,本篇文章將介紹使用Canvas這個對象,如何實現(xiàn)“刮刮樂”的效果。
先看效果圖

下面我們看一下如何使用代碼實現(xiàn)
布局文件
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/after"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/a" />
<ImageView
android:id="@+id/before"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/b" />
</FrameLayout>
Activity代碼
public class MainActivity extends Activity implements OnTouchListener {
private ImageView imgafter;
private ImageView imgbefore;
private Canvas canvas;
private Paint paint;
private Bitmap bitmap;
private Bitmap before;
private Bitmap after;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgafter = (ImageView) findViewById(R.id.after);
imgbefore = (ImageView) findViewById(R.id.before);
// 獲得圖片
after = BitmapFactory.decodeResource(getResources(), R.drawable.a);
before = BitmapFactory.decodeResource(getResources(), R.drawable.b);
imgafter.setImageBitmap(after);
imgbefore.setImageBitmap(before);
// 創(chuàng)建可以修改的空白的bitmap
bitmap = Bitmap.createBitmap(before.getWidth(), before.getHeight(),
before.getConfig());
imgbefore.setOnTouchListener(this);
paint = new Paint();
paint.setStrokeWidth(5);
paint.setColor(Color.BLACK);
// 創(chuàng)建畫布
canvas = new Canvas(bitmap);
canvas.drawBitmap(before, new Matrix(), paint);
}
@Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
int newX = (int) event.getX();
int newY = (int) event.getY();
// 將滑過的地方變?yōu)橥该?
for (int i = -10; i < 10; i++) {
for (int j = -10; j < 10; j++) {
if ((i + newX) >= before.getWidth()
|| j + newY >= before.getHeight() || i + newX < 0
|| j + newY < 0) {
return false;
}
bitmap.setPixel(i + newX, j + newY, Color.TRANSPARENT);
}
}
imgbefore.setImageBitmap(bitmap);
break;
}
return true;
}
}
可以看到,代碼很簡單,幾十行代碼就實現(xiàn)了簡單的“刮刮樂”的效果。
原理是這樣的,一開始兩張圖片重疊,顯示的還沒有刮開的效果。
在Activity的onTouch方法中,我們對滑動事件進(jìn)行監(jiān)聽,當(dāng)用戶用手指滑動屏幕的時候,我們將滑過的畫布部分的顏色設(shè)置為透明,同時,把改變之后的bitmap對象設(shè)置為ImageView的背景,這樣,隱藏在后面的圖片就顯示出來了,也就實現(xiàn)了刮刮樂的效果。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程之SQLite數(shù)據(jù)庫操作方法詳解
這篇文章主要介紹了Android編程之SQLite數(shù)據(jù)庫操作方法,簡單介紹了SQLite數(shù)據(jù)庫及Android操作SQLite數(shù)據(jù)庫的步驟與相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-08-08
android-使用環(huán)信SDK開發(fā)即時通信功能(附源碼下載)
本篇文章主要介紹了android-使用環(huán)信SDK開發(fā)即時通信功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。2016-12-12
在Android系統(tǒng)源碼中預(yù)置APK的方法
今天小編就為大家分享一篇關(guān)于在Android系統(tǒng)源碼中預(yù)置APK的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android?自定義view中根據(jù)狀態(tài)修改drawable圖片
這篇文章主要介紹了Android?自定義view中根據(jù)狀態(tài)修改drawable圖片的相關(guān)資料,需要的朋友可以參考下2023-07-07
詳解flutter如何實現(xiàn)局部導(dǎo)航管理
這篇文章主要為大家介紹了詳解flutter如何實現(xiàn)局部導(dǎo)航管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01

