1. Check out our companion app, Forums for Android! Download from Google Play

touch event on image made on canvas

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

  1. focode

    focode Member
    Thread Starter
    5

    Jun 17, 2010
    5
    0
    5
    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
    36

    Oct 2, 2010
    71
    8
    36
    Computer Programmer
    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
    36

    Oct 2, 2010
    71
    8
    36
    Computer Programmer
  4. focode

    focode Member
    Thread Starter
    5

    Jun 17, 2010
    5
    0
    5
    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
    36

    Oct 2, 2010
    71
    8
    36
    Computer Programmer
    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

    26

    Jan 13, 2016
    5
    1
    26
    Male
    Hello sir i want to create a bitmap with every single touch by the user. with the existing one....
     

Share This Page

Loading...