[問題] 我要如何在繪製出來的bitmap上加上按鈕

看板AndroidDev作者 (舞踏linkl)時間14年前 (2011/11/30 11:00), 編輯推噓2(204)
留言6則, 5人參與, 最新討論串1/1
最近練習由程式自已繪製圖片出來的程式碼 但我想讓我的bitmap在點擊下去時可以有更換圖片或是跑出toast之類的效果 也就是想當成一般botton來用 這樣的話 我應該如何更改才好呢? 程式碼如下: import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class NewView extends View{ Bitmap myBitmap; //背景 Bitmap tableImg; //動作目標物 Bitmap itemImg1; //物件1 Bitmap itemImg2; //物件2 Paint paint; public NewView(Context context, AttributeSet attrs){ super(context, attrs); this.initBitmap(); } public void initBitmap(){ paint = new Paint(); myBitmap = BitmapFactory.decodeResource (getResources(), R.drawable.back); tableImg = BitmapFactory.decodeResource (getResources(), R.drawable.table); itemImg1 = BitmapFactory.decodeResource (getResources(), R.drawable.clock); itemImg2 = BitmapFactory.decodeResource (getResources(), R.drawable.draw); } @Override protected void onDraw(Canvas canvas){ super.onDraw(canvas); paint.setAntiAlias(true); paint.setColor(Color.BLACK); paint.setTextSize(30); canvas.drawBitmap(myBitmap, 0, 0, paint); canvas.drawText("寬:"+myBitmap.getWidth(),0 , 220, paint); canvas.drawText("高: "+myBitmap.getHeight(), 150, 220, paint); canvas.drawBitmap(tableImg, 75, 300,paint); canvas.drawBitmap(itemImg1, 10, 10,paint); canvas.drawBitmap(itemImg2, 260, 10,paint); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.134.40.161

11/30 11:14, , 1F
把你畫出來的Bitmap設定在button上
11/30 11:14, 1F

11/30 13:16, , 2F
有一種view叫Button
11/30 13:16, 2F

11/30 13:38, , 3F
Button.setBackgroundDrawable()
11/30 13:38, 3F

12/01 00:20, , 4F
ImageButton?
12/01 00:20, 4F

12/05 11:51, , 5F
其實Android的View都可以setClickable( ture ) 然後再設
12/05 11:51, 5F

12/05 11:51, , 6F
setOnClickListener就好
12/05 11:51, 6F
文章代碼(AID): #1ErPmwx9 (AndroidDev)