這篇文章將為大家詳細(xì)講解有關(guān)怎么在Android中利用Canvas對象實(shí)現(xiàn)一個(gè)刮刮樂效果,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。
布局文件
<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)橥该?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對象實(shí)現(xiàn)一個(gè)刮刮樂效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前名稱:怎么在Android中利用Canvas對象實(shí)現(xiàn)一個(gè)刮刮樂效果-創(chuàng)新互聯(lián)
分享地址:http://chinadenli.net/article12/dcpcgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、定制開發(fā)、網(wǎng)站改版、營銷型網(wǎng)站建設(shè)、電子商務(wù)、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容