I have a bitmap:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R开发者_如何学运维.drawable.myimage);
I want it so when a user presses on the bmp, I get a trace message (I will add in what I need it to do)
Anyone have any ideas?
Actually, Bitmap
is a picture that can't exist on your layout without any container. A container for a picture in Android is ImageView
. So to make a clickable ImageView
with your picture inside you should use:
ImageView imgView = (ImageView)findViewById(R.id.img);
imgView.setImageBitmap(bmp);
imgView.setOnClickListener(new View.OnClickListener());
Hope this helps.
It may be more appropriate to use an ImageButton
rather than an ImageView
. Although, you could implement the onTouchEvent
and register a touch event listener on the main element that the Bitmap relies in. Or, register an onTouchListener
for the ImageView
.
You will have to set an ImageView's background Bitmap as bmp, and then set a click listener on the ImageView.
Take a look at ImageView's setImageBitmap method, and View.OnClickListener
精彩评论