Draw a bitmap in a custom ImageView and get the coordinates of the ImageView regardless of device

I want to get the coordinates of ImageView , regardless of the size of the device.

Is there any possible way!

I tried to create a specific size for ImageView , even a specific size for Parent View , but it doesnโ€™t work.

I tried the following possible ways.

 int[] posXY = new int[2]; imageview.getLocationOnScreen(posXY); int MoveX = posXY[0]; int MoveY = posXY[1]; 

I also tried Matrix, but did not work.

 Matrix m = imageview.getImageMatrix(); 

Tried the code below, but it also doesn't work. !!

I need to get the same {x, y} coordinates for all devices for the same point (Location).

 final float[] getPointerCoords(ImageView view, MotionEvent e) { final int index = e.getActionIndex(); final float[] coords = new float[] { e.getX(index), e.getY(index) }; Matrix matrix = new Matrix(); view.getImageMatrix().invert(matrix); matrix.mapPoints(coords); return coords; } 

Here is the draw method:

if I set the bitmap in the drawing, it is not suitable for the screen for all devices. If I set the image with the width and height of the display, I get different coordinates.

  @Override public void draw(Canvas canvas) { // TODO Auto-generated method stub super.draw(canvas); Resources res = getResources(); Drawable drawable = res.getDrawable(R.drawable.subimage); mBitmap = ((BitmapDrawable) drawable).getBitmap(); canvas.drawBitmap(mBitmap, 0, 0, null); } 

Any idea or help would be really helpful.

+7
android canvas coordinates android-custom-view draw
source share
3 answers

Finally, some relative solution was found, the same coordinates for all devices. Using Activity, not Custom ImageView.

In ImageView OnTouch ..

  public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); final int index = e.getActionIndex(); final float[] coords = new float[] { e.getX(index), e.getY(index) }; Matrix matrix = new Matrix(); choosenImageView.getImageMatrix().invert(matrix); matrix.mapPoints(coords); return true; } 

Canvas drawing

  Resources res = getResources(); Drawable drawable = res.getDrawable(R.drawable.image); Bitmap bmp= ((BitmapDrawable) drawable).getBitmap(); alteredBitmap = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig()); canvas = new Canvas(alteredBitmap); paint = new Paint(); paint.setColor(Color.GREEN); paint.setStrokeWidth(5); matrix = new Matrix(); canvas.drawBitmap(bmp, matrix, paint); 
+1
source share

First you need to calculate the screen coefficient of the device,

 deviceWidth / screenHeight = ratio 

then you need to multiply it by the desired coordinate:

 ratio * xCoord = newXcoord; // and same for Y coord 

By doing this, you save the equivalent coordinates for all device sizes.

You can also declare sizes and coordinates relative to% of screen sizes, for example:

 x = 0.2 * deviceWidth // %20 of device width from left 
+1
source share

You can try this

 imageview.post(new Runnable() { @Override public void run() { int[] posXY = new int[2]; imageview.getLocationOnScreen(posXY); int MoveX = posXY[0]; int MoveY = posXY[1]; Log.i("xy co-ordinate", "x "+MoveX, "y"+MoveY); } }); 
0
source share

All Articles