1. Introducing Channels - a new way to chat with other Android users!
    Dismiss Notice

touch event on image made on canvas


Last Updated:

  1. focode

    focode Member This Topic's Starter

    Joined:
    Jun 17, 2010
    Messages:
    5
    Likes Received:
    0
    i have have made an image on canvas in the following way

    PHP:
    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
     

    Advertisement
  2. AlbertPucciani

    AlbertPucciani Well-Known Member

    Joined:
    Oct 2, 2010
    Messages:
    71
    Likes Received:
    8
    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)"


    @Override
    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
    }

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


    Hope that helps,

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

    AlbertPucciani Well-Known Member

    Joined:
    Oct 2, 2010
    Messages:
    71
    Likes Received:
    8
  4. focode

    focode Member This Topic's Starter

    Joined:
    Jun 17, 2010
    Messages:
    5
    Likes Received:
    0
    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
     
  5. AlbertPucciani

    AlbertPucciani Well-Known Member

    Joined:
    Oct 2, 2010
    Messages:
    71
    Likes Received:
    8
    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.
     
  6. Kishan kumar sahu

    Kishan kumar sahu Member

    Joined:
    Jan 13, 2016
    Messages:
    5
    Likes Received:
    1
    Hello sir i want to create a bitmap with every single touch by the user. with the existing one....
     

Share This Page

Loading...