Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a bitmap using canvas clickable?

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();
    }

}
like image 531
Prashanth Avatar asked Sep 16 '13 11:09

Prashanth


People also ask

How do I create a bitmap in canvas?

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.

Is canvas a bitmap?

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.

How do you draw a bitmap?

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.


1 Answers

@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;
    }
like image 164
adhi Avatar answered Oct 18 '22 07:10

adhi