怎么在Android中利用Canvas對(duì)象實(shí)現(xiàn)一個(gè)刮刮樂(lè)效果-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在Android中利用Canvas對(duì)象實(shí)現(xiàn)一個(gè)刮刮樂(lè)效果,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),富裕企業(yè)網(wǎng)站建設(shè),富裕品牌網(wǎng)站建設(shè),網(wǎng)站定制,富裕網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,富裕網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

布局文件

<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(); 
      // 將滑過(guò)的地方變?yōu)橥该?nbsp;
      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; 
  } 
}

關(guān)于怎么在Android中利用Canvas對(duì)象實(shí)現(xiàn)一個(gè)刮刮樂(lè)效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前名稱:怎么在Android中利用Canvas對(duì)象實(shí)現(xiàn)一個(gè)刮刮樂(lè)效果-創(chuàng)新互聯(lián)
分享地址:http://muchs.cn/article12/dcpcgc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、定制開(kāi)發(fā)網(wǎng)站改版、營(yíng)銷型網(wǎng)站建設(shè)電子商務(wù)標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)