How do i make the bitmap created clickable? Below is the code which i have used to create a bitmap using canvas.
public class DrawView extends View implements OnClickListener
{
public DrawView(Context context)
{
super(context);
paint = new Paint();
image = BitmapFactory.decodeResource(getResources(), R.drawable.andmrktsmall);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
int canWidth = canvas.getWidth();
int canHeight = canvas.getHeight();
int width = (canWidth - 200) / 2;
int height = (canHeight - 100) / 2;
Bitmap indexcanvas = Bitmap.createScaledBitmap(image, 200, 100, true);
canvas.drawBitmap(indexcanvas, width, height, paint);
this.setBackgroundColor(Color.YELLOW);
}
@Override
public void onClick(View v)
{
Toast.makeText(context, "View is clicked", 1).show();
}
}
To create a bitmap object on a canvas C , use: id = C . create_bitmap( x , y , *options ...) which returns the integer ID number of the image object for that canvas.
Basically, the Canvas is backed by a Bitmap , so when you draw anything using the canvas, the canvas will draw into the Bitmap it was created with.
To draw on a bitmap, use the image control's canvas and attach the mouse-event handlers to the appropriate events in the image control. Typically, you would use region operations (fills, rectangles, polylines, and so on). These are fast and efficient methods of drawing.
@Override
public boolean onTouchEvent(MotionEvent event)
{
int x=(int)event.getX();
int y=(int)event.getY();
if (drawable.getBounds().contains(x,y) &&
event.getAction()==MotionEvent.ACTION_DOWN)
{
Log.e(TAG, "onTouchEvent: drawable touched ");
return true;
}
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With