Detect Touch on Bitmap

Detect touch on bitmap

You should work with something like so:

public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();
int x = event.getX() // or getRawX();
int y = event.getY();

switch(action){
case MotionEvent.ACTION_DOWN:
if (x >= xOfYourBitmap && x < (xOfYourBitmap + yourBitmap.getWidth())
&& y >= yOfYourBitmap && y < (yOfYourBitmap + yourBitmap.getHeight())) {
//tada, if this is true, you've started your click inside your bitmap
}
break;
}
}

That's a start, but you need to handle case MotionEvent.ACTION_MOVE and case MotionEvent.ACTION_UP to make sure you properly deal with the user input. The onTouchEvent method gets called every time the user: puts a finger down, moves a finger already on the screen or lifts a finger. Each time the event carries the X and Y coordinates of where the finger is. For example if you want to check for someone tapping inside your bitmap, you should do something like set a boolean that the touch started inside the bitmap on ACTION_DOWN, and then check on ACTION_UP that you're still inside the bitmap.

Adding touch event to a bitmap in android

As you are using SurfaceView,then the touch event is just about the point(x,y) that you touched on the whole surface.You need to calculate if this point is on one of your bitmap.

See this: Detect touch on bitmap

detecting touch for bitmap that's been drawn with Rect transformations

It seems to me that the same method applies as for simple x,y coordinates. You simply need to use the coordinates and size of Rect dest to calculate whether the bitmap has been touched.

In other words, you need to do something like this:

public boolean picked(Rect dest, int touchX, int touchY) {
if(touchX > dest.left && touchX < dest.left + dest.width() &&
touchY > dest.top && touchY < dest.top + dest.height())
return true;
return false;
}

Canvas Android, how can i do an OnTouchEvent when a bitmap is touched instead of the full screen?

float x = event1.getX();
float y = event1.getY();

if(rect.contains((int)x, (int)y))
{
// touch in image
}
//where rect is rectangle of bitmap

How to check touch on a bitmap

image without background, like a circle, a triangle and so on

then you should probably override View's onTouch, retrieve x and y from the touch event you got as parameter and compare it with the coords of the shape you drawn. The easiest way is to use Rect.contains(int x, int y)

Detect touch on color spots at image(bitmap)

The simplest solution I see is:

1- Get the imageView size as it might change depending on device screen size and/or resolution.

int IVwidth = imageView.getWidth();
int IVHeight = imageView.getHeight();

2- Get the Bitmap width/height and then find the mathmatical relation (ratio) between bitmap width and imageView width,
Simply:

float HeightRatio = (float)bm.getHeight() / (float)iv.getHeight();
float WidthRatio = (float)bm.getWidth() / (float)iv.getWidth();

lets say we have a bitmap of (800*600) size and an imageView of (400*300) size
this would give you a ratio of (2.0) for height and width.

3- Now when the event ImageViewTouched is raised, you can get the touch coordinates quite easy as the event will deliver them to you. Notice that catching the touch event is quite easy and simply can be implemented by adding this to the view;

    ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {

if(arg1.getAction() == MotionEvent.ACTION_UP)
{
int x = arg1.getX();
int y = arg1.getY();
int XonImage = x * WidthRatio;
int YonImage = Y * HeightRatio;
if(bm.getPixel(XonImage, YonImage) == Color.RED)
{
// BINGOOOOOOO, do your magic here
}
}

return false;
}
});

For anymore info please ask without hesitation.
Cheers

How to get Coordinates on touch Event on bitmap not of Screen

You can't get the coordinates of the bitmap directly. You need to calculate to yourself.
Use the position of the ImageView, and with that you can handle it.

Of course, when you are after Activity onCreate you can acces to any inflated (active) views parameter.
Exemple.
ImageView a;
a.getPaddingBottom();
Like all coordinats (left right etc...)
After this you need the Height and Width of the ImageView.
Nah, when you know the views position, hegiht and width you can calculate.

Example:

final ImageView iv_YourImage = (ImageView) findViewById(R.id.iv_imageview);
public boolean onTouch(View v, MotionEvent event){
int topParam = iv_YourImage.getPaddingTop();
int rightParam = iv_YourImage.getPaddingRight();
int maxTopParam = topParam+iv_YourImage.getMaxHeight();
int maxRightParam = rightParam + iv_YourImage.getMaxWidth();
if(event.getX>topParam&&event.getX<maxTopParam){
//the x coordinate is in your image... do the same to Y
}
return true;
}


Related Topics



Leave a reply



Submit