1. Are you ready for the Galaxy S20? Here is everything we know so far!

touch event on image made on canvas

Discussion in 'Android Apps & Games' started by focode, Nov 1, 2010.

  1. focode

    focode Lurker
    Thread Starter

    i have have made an image on canvas in the following way

    1. Bitmap bmp = BitmapFactory.decodeResource(getResources(),R.drawable.pushpin);
    2.             canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
    now i want to listen touch event on the image , how can i do that

    thanks and regards

    1. Download the Forums for Android™ app!


  2. Hi,
    This is the wrong forum for that request, but I'll answer it:

    If you are using a canvas from a SurfaceView, for example, you'll need to override the method "onTouchEvent(TouchEvent event)"

    public boolean onTouchEvent(final MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN: {
    // Code on finger down
    float posX = ev.getX();
    float posY = ev.getY()'

    // 80x80 pixel square located 20 pixels left, 20 pixels top.
    float x1 = 20, x2 = 100, y1 = 20, y2 = 100;

    if ((posX >= x1 && posX <= x2) && (posY >= y1 && posY <= y2)) {
    // we are in the square
    } else {
    // we are somewhere else on the canvas

    case MotionEvent.ACTION_UP: {
    // Code on finger up
    case MotionEvent.ACTION_MOVE: {
    // Code on finger move

    Hope that helps,

    @@Moderator, could you move this?
  3. focode

    focode Lurker
    Thread Starter

    is it not possible to get touch event on the image itself , it will be difficult to get the area of the image and then use the above code
  4. You can't get a touch event on the bitmap because the bitmap doesn't support any kind of touch handlers, it is simply a picture. The canvas is also another bitmap, it just supports an API that allows you to draw onto it with ease. The canvas and bitmap do not support being touched because that is out of the scope of what they do.

    Your other alternative is to make a layout and add an ImageButton or ImageView to it, then you "setContentView(R.id.my_layout_file_name)" on the Activity. Then you can register a touch handler by calling "setOnClickListener()" on the ImageButton or ImageView.

    Start with the Android documentation: User Interface | Android Developers

    If you're still having problems, let me know.
  5. Hello sir i want to create a bitmap with every single touch by the user. with the existing one....

Share This Page