欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用Canvas對象實現(xiàn)刮刮樂效果

 更新時間:2021年09月26日 15:02:59   作者:趙凱強  
這篇文章主要介紹了Android使用Canvas對象實現(xiàn)刮刮樂效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

在淘寶、京東等電商舉辦活動的時候,經(jīng)??梢钥吹皆谝苿涌蛻舳送瞥龅母鞣N刮獎活動,而這種活動也受到了很多人的喜愛。從客戶端的體驗來說,這種效果應(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ù)庫操作方法詳解

    這篇文章主要介紹了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ā)即時通信功能(附源碼下載)

    本篇文章主要介紹了android-使用環(huán)信SDK開發(fā)即時通信功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。
    2016-12-12
  • adb通過wifi連接android設(shè)備流程解析

    adb通過wifi連接android設(shè)備流程解析

    這篇文章主要介紹了adb通過wifi連接android設(shè)備流程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-12-12
  • 詳解Android 獲取手機中微信聊天記錄方法

    詳解Android 獲取手機中微信聊天記錄方法

    本篇文章給大家分享了Android 獲取手機中微信聊天記錄的詳細(xì)步驟和操作方法,有需要的朋友可以學(xué)習(xí)參考下。
    2018-05-05
  • Flutter路由的幾種用法小結(jié)

    Flutter路由的幾種用法小結(jié)

    這篇文章主要介紹了Flutter路由的幾種用法,包括基本路由跳轉(zhuǎn)和路由跳轉(zhuǎn)傳參方法,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • Android Jni的簡單使用詳解

    Android Jni的簡單使用詳解

    這篇文章主要介紹了Android Jni的簡單使用詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • 在Android系統(tǒng)源碼中預(yù)置APK的方法

    在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圖片

    這篇文章主要介紹了Android?自定義view中根據(jù)狀態(tài)修改drawable圖片的相關(guān)資料,需要的朋友可以參考下
    2023-07-07
  • 詳解flutter如何實現(xiàn)局部導(dǎo)航管理

    詳解flutter如何實現(xiàn)局部導(dǎo)航管理

    這篇文章主要為大家介紹了詳解flutter如何實現(xiàn)局部導(dǎo)航管理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Kotlin協(xié)程之Flow異常示例處理

    Kotlin協(xié)程之Flow異常示例處理

    這篇文章主要為大家介紹了Kotlin協(xié)程之Flow異常處理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09

最新評論