Display image in area touch with finger on the screen - android

When I touch a region on my screen, I want an ImageView to be visible at the touch area. An example of imageview is as follows.
ImageView image = (ImageView)findViewById(R.id.image);
How can I set the image at region touch on the screen using the method below. Also when I tap on a new region the previous image become invisible.
#Override
public boolean onTouchEvent(MotionEvent event, view) {
final int action = event.getAction();
final int x = (int) event.getX();
final int y = (int) event.getY();
boolean result = false;
}

Use Canvas you will get the xy coordinates and you can easily get Imageview at that coordinates

Related

Imageview not following finger touch

I want to build an app that has an imageview and it simply follows the finger touch. In other words, the imageview follows the touch of the finger. I have partially achieved this, in the sense that the imageview is following the touch of the finger but the problem is that it is at some offset. The imageview is about an inch down the actual touch.
Below is my code:
public class MainActivity extends AppCompatActivity {
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.img1);
img.setImageResource(R.drawable.ic_remove_circle_black_24dp);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
img.setX(event.getX());
img.setY(event.getY());
return false;
}
}
What could be the reason behind the offset?
Edit: Solution
Following code does what I need:
public class MainActivity extends AppCompatActivity {
ImageView img;
int[] viewCoords = new int[2];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.img1);
img.setImageResource(R.drawable.ic_remove_circle_black_24dp);
}
#Override
public boolean onTouchEvent(MotionEvent event){
img.getLocationOnScreen(viewCoords);
img.setX(event.getX()-(viewCoords[0]-img.getX()));
img.setY(event.getY()-(viewCoords[1]-img.getY()));
return true;
}
}
Thanks to Doomsknight for the direction.
From what I can see, you are using the X and Y coordinates of the touch event of the Screen.
And then drawing this coordinates on the X and Y of the ImageView.
This will cause the offset you are seeing, as they do not match up, as the imageview probably isnt at the very top of your screen.
What I think you are trying to do is get the X and Y, of the actual image.
You will therefore need to offset the coordinates accordingly.
There is a good example of this already of getting the offset, and removing it: see link below.
https://stackoverflow.com/a/11312423/940834
You can get the top left corner of your view as follows:
int[] viewCoords = new int[2];
imageView.getLocationOnScreen(viewCoords);
From this and the touch
coordinates you can calculate the point inside the ImageView:
int touchX = (int) event.getX();
int touchY = (int) event.getY();
int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate

How to check if imageview is currently zoomed or not in android?

i am zooming an image view on double tap to some certain points. on double tap event it zooms in perfectly but i want to implement the functionality of zoom out too that when user double taps the image is zoomed but when he double tap again the zoomed image must go back to original size
how this can be done? Any help will be appreciated
Here is my code
ImageView iv;
private Matrix matrix = new Matrix();
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(), R.drawable.demoimg), size.x, size.y, true);
iv.setImageBitmap(bmp);
original_matrix = iv.getImageMatrix();
iv.setOnTouchListener(new View.OnTouchListener() {
private GestureDetector gestureDetector = new GestureDetector(SecondActivity.this, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onDoubleTap(MotionEvent e) {
float scalefactor = Math.max(1.1f, Math.min(3.0f, 4.0f));
float x = e.getX();
float y = e.getY();
matrix.setScale(scalefactor, scalefactor, x, y);
iv.setImageMatrix(matrix);
return super.onDoubleTap(e);
}
});
#Override
public boolean onTouch(View v, MotionEvent event) {
/* Toast.makeText(getApplicationContext(), "On Touch Event Called",
Toast.LENGTH_LONG).show();*/
gestureDetector.onTouchEvent(event);
return true;
}
});
The best practice is to Extend the imageView and make your customImageView.
In your customImageView keep the original size of your image and the track of image current state (zoomed in/out) then in your xml use your customImageView instead of android imageView .
You can put your zoom method in your customImageView. Then you have a customImageView which know how to zoom in, get back to the original size and its current state.

Multi touch events

There is an image in my activity, on touch listener of particular coordinates I am changing the image so that the image from the drawable folder will get loaded on the current image. Now I want a loop for multiple touch so that when the touch event is occurred again, the image will get changed. Should I apply a for loop for touch listener? How? Please help. Here is the code.
final View touchView = findViewById(R.id.imageView1);
touchView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
int ax1=324;
int ay1=522;
int ax2=394;
int ay2=575;
int ax = (int) event.getX();
int ay = (int) event.getY();
if(ax>=ax1 && ax<=ax2 && ay>=ay1 && ay<=ay2){
touchView.setOnTouchListener(this);
img1.setImageResource(R.drawable.num2);
}
}
return true;
}
});
Here, the imageView1 is the main image and img1 is a part of image which resides on the main image. Now on the first touch, the image "num2" will get loaded on img1 and on the next touch "num3" will get loaded.
// get pointer index from the event object
int pointerIndex = event.getActionIndex();
// get pointer ID
int pointerId = event.getPointerId(pointerIndex);
Based on multi touch you can decide your selection. multi touch

Need to get the X and Y value of image where it clicked I am using image which larger then device in android

Hi, I know how to get the clicked position in screen .. my problem is I need to get the X and Y value of image where it clicked. It works fine if I have image size lesser then device but it goes wrong when using the large image.
I am Using horizontal and vertical scroll view to show the large image.
Using the event.getX() to get the position in the touch listener.
To get X and Y value on image put following code in OnTouchListener.
public boolean onTouch (View v, MotionEvent ev)
{
final int action = ev.getAction();
final int evX = (int) ev.getX();
final int evY = (int) ev.getY();
}

Draw circle on view

I want to draw a circle when ever user touches any place on the screen.I know to draw the circle using canvas,but how the drawn circles should be positioned to screen?
Does the surface view help for me?
Thanks in advance.
You can use the Activity.onTouchEvent event to determine where the user touched the screen. Then you can draw your circle on the canvas at that position.
Here's an example that handles a simple touch event.
#Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
// draw circle at x,y
}
}

Categories

Resources