Translate screen position to image pixel coordinate with android - android

I have a ImageView they use a TouchListener with a GestureDetector to handle a SingleTap event, I'm able to get the touch (x,y) position.
Now, I would like to get the pixel coordinates of the touch position. I have play without success with the ImageView matrix..

Getting color from pixcel using bitmap like this:
ImageView imageView =((ImageView)v);
Bitmap bitmap =((BitmapDrawable)imageView.getDrawable()).getBitmap();
int pixel = bitmap.getPixel(x,y);

Related

Converting imageview pixels to bitmap pixels after gesture

I have an imageview, I want to convert the touch event on the image to its corresponding pixel on the orginal bitmap (even after the image has been gesture scaled or flinged).
How can this be achieved?
Thanks

Rotate image using matrix around center of 'imageview'

This is my code
//originBitmap is bitmap in imageview
setMatrix(originBitmap.getWidth()/2,originBitmap.getHeight()/2);
void setMatrix(float x,float y){
matrix.setTranslate(matrixX,matrixY);
matrix.preRotate(matrixR,x,y);
matrix.preScale(matrixS,matrixS,x,y);
imageView.setImageMatrix(matrix);
}
My code is work currectly in this situation. pic1(blue=imageview, red=bitmap image)
But when this situation, pic2 image rotate around it's center, so users see strange image what moves around strange position.
How can I rotate image around imageview's center position?

Draw image in activity android

I have a activity where I have two imageViews. In onTouch I recognize which imageView was clicked. Now I have a question. How I can draw new very small image in place where I clicked. I know that onTouch i have getX() and getY() and I save this position when I touch the screen, but I don't know how I can draw image in place where I clicked. My image is in drawable resources. I need to use canvas or is better way. I need draw this picture and after that I need run animation this image.
edit1: How can I set positionX and positionY my image. I have position where I click but I don't know how I can set image in this coordinates.
you should have a image view with visible attribute of FALSE and after touch (or any event you want) set three attribute : visible = true; x = getX() and y = getY();
is it enough or explain more?
I see the following approach.
Replace the ImageView by a custom view derived from ImageView.
Override onDraw.
call super.onDraw to draw the image
paint your image using onDraw's canvas at the last touch position.
each time you get a touch, you need to invalidate the custom view

PNG transparency and touches in android

I have a png, when I save it it is a square image, but inside the square png is a circle (sometimes its other shapes). How can I check when the user is over what is inside the png and a graphic rather than when a user is inside a png but is over transparency?
Yea, you can do this, you just need to manually check the color of the pixel where the touch occurred.
int color = Bitmap.getPixel(x,y); // x and y are the location of the touch event in Bitmap space
int alpha = Color.getAlpha(color);
boolean isTransparent = (alpha==0);
Note: Depending on how you implement your touch listener will might need to convert the x,y location of the touch event to the x,y coordinates of the image view.

Is there a way to find the on-screen coordinates of an ImageView's drawable?

I have an activity which consists of just an ImageView. The drawable contained in the ImageView is a Bitmap that was captured from the camera. The image is automatically scaled (maintaining the Bitmap's aspect ratio) so that the scaled width matches the width of the ImageView, and the image is centered on the screen vertically.
I need to figure out the coordinates of the top-left pixel of the actual drawable (not of the ImageView itself), but the ImageView class doesn't seem to give me a way of doing that.
I feel like I could potentially calculate it based on the dimensions of the original bitmap and the dimensions of the ImageView, but that would involve a lot of math that should be unnecessary, and would be prone to floating point errors.
Does anyone know of a way to find the coordinates of the top-left pixel of the Drawable relative to the ImageView?
Thanks.
Looking at the ImageView source code, it translates a Matrix halfway the vertical size (i.e., center it vertically), so you could retrieve it with the ImageView getImageMatrix() method and check the vertical translation by Matrix#getValues(float[]). If I read it right, it will be the sixth value.
I can't test it right now, but it would be something like:
Matrix matrix = iv.getImageMatrix ();
float[] values = new float[];
matrix.getValues(values);
float startY = values[5];

Categories

Resources