Skip to content Skip to sidebar Skip to footer

Make Bitmaps Listen To Touch Events

How can I add touch capabilities to a simple bitmap? I've tried to create a wrapper class for a bitmap, which implemented OnGestureListener, but it didn't work. I want to avoid

Solution 1:

A Bitmap per se is just a representation of an image... thus, in order to show it you will have to draw it somewhere (a View, in fact you always draw it on a View). Then, there's no way to avoid using the View class since all user interface widgets extend it.

In conclusion, if you want to simply set touch listeners to a single Bitmap you can, for instance, draw it on a ImageView and set the appropriate listeners. On the other hand, if you have a set of bitmaps drawn somewhere (for instance, on a SurfaceView), you should locate the bitmap by its coordinates (in that case, the View which would receive the events will be the SurfaceView).

Solution 2:

Do you have your OnGestureListener implementation wired to a GestureDetector? The GestureDetector analyzes a MotionEvent and invokes the appropriate callback on the listener based on the type of movement it found.

publicclassMyActivityextendsActivity {
    privateGestureDetector detector;

    publicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ...
        detector = newGestureDetector(newMyGestureListener());
        ...
    }

    @OverridepublicbooleanonTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);
    }
}

Post a Comment for "Make Bitmaps Listen To Touch Events"