How to get the same coordinates for the image for all resolutions in android?

I came across getting the x and y coordinates of the image for all resolutions.

  • 320, for devices with a screen configuration, such as:
    • 240x320 ldpi (QVGA phone)
    • 320x480 mdpi (handset)
    • 480x800 hdpi (high density telephone)
  • 480, for screens such as 480x800 mdpi (tablet / phone).
  • 600, for screens such as 600x1024 mdpi (7-inch tablet).
  • 720, for screens such as 720x1280 mdpi (10-inch tablet).

I created an image with the original image size, for example, 1500 x 1119. When I touched the image, I get the coordinates, using onTouch (). My problem is if the coordinates change in all devices. I get different x and y values ​​in different cheats. I cannot get the same x and y values ​​for all resolutions. How can i solve this?

+7
android coordinates imageview
source share
3 answers

This requires a slightly more general solution than your specific layout. This assumes that you know the size of the original image and want to know the coordinates on it that were involved, ignoring the size and resolution of the screen. Although your layout determines the width and height, which seem to match the original image, it will handle layouts with the width and height specified by either wrap_content or match_parent if you use one of the scale types that matches the whole image inside the view and maintains aspect ratio (e.g. fitXY ).

I have not tried this inside ScrollView , as you have it, but in theory this should be fine.

 int sourceWidth = 1500; int sourceHeight = 1119; // Find view dimensions int width = view.getWidth(); int height = view.getHeight(); // Assuming image has been scaled to the smaller dimension to fit, calculate the scale float wScale = (float)width/(float)sourceWidth; float hScale = (float)height/(float)sourceHeight; float scale = Math.min(wScale, hScale); // Find the offset in the direction the image doesn't fill the screen // For ImageViews that wrap the content, both offsets will be 0 so this is redundant int offsetX = 0; int offsetY = 0; if (wScale <= hScale) { offsetY = (int)((height/2) - ((scale * sourceHeight)/2)); } else { offsetX = (int)((width/2) - ((scale * sourceWidth)/2)); } // Convert event coordinates to image coordinates int sourceX = (int)((event.getX() - offsetX) / scale); int sourceY = (int)((event.getY() - offsetY) / scale); 
0
source share
Finally, I found the answer. I liked it.

First I created my layout like this.

new_lay.xml

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <ScrollView android:id="@+id/sv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <HorizontalScrollView android:id="@+id/hv" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" > <FrameLayout android:id="@+id/ground_gry_fl1" android:layout_width="3193dp" android:layout_height="1815dp" android:background="#FFFFFF" > <FrameLayout android:id="@+id/zoom_lay1" android:layout_width="3193dp" android:layout_height="1815dp" android:background="@drawable/mall_map" > <ImageView android:id="@+id/marker_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:visibility="invisible" > </ImageView> </FrameLayout> </FrameLayout> </HorizontalScrollView> </ScrollView> / zoom_lay1" <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <ScrollView android:id="@+id/sv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <HorizontalScrollView android:id="@+id/hv" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" > <FrameLayout android:id="@+id/ground_gry_fl1" android:layout_width="3193dp" android:layout_height="1815dp" android:background="#FFFFFF" > <FrameLayout android:id="@+id/zoom_lay1" android:layout_width="3193dp" android:layout_height="1815dp" android:background="@drawable/mall_map" > <ImageView android:id="@+id/marker_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:visibility="invisible" > </ImageView> </FrameLayout> </FrameLayout> </HorizontalScrollView> </ScrollView> / marker_image" <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <ScrollView android:id="@+id/sv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <HorizontalScrollView android:id="@+id/hv" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FFFFFF" > <FrameLayout android:id="@+id/ground_gry_fl1" android:layout_width="3193dp" android:layout_height="1815dp" android:background="#FFFFFF" > <FrameLayout android:id="@+id/zoom_lay1" android:layout_width="3193dp" android:layout_height="1815dp" android:background="@drawable/mall_map" > <ImageView android:id="@+id/marker_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" android:visibility="invisible" > </ImageView> </FrameLayout> </FrameLayout> </HorizontalScrollView> </ScrollView> 

NewClass.java

  public class NewClass extends Activity { ImageView mark; FrameLayout ground_gry_fl,zoom_lay; ScrollView sv; float val1 = 0.0f; float val2 = 0.0f; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_lay); sv=(ScrollView) findViewById(R.id.sv); mark = (ImageView) findViewById(R.id.marker_image); ground_gry_fl = (FrameLayout)findViewById(R.id.ground_gry_fl1); zoom_lay = (FrameLayout)findViewById(R.id.zoom_lay1); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int height = metrics.heightPixels; int width = metrics.widthPixels; System.out.println("width"+width); System.out.println("height"+height); float densityDpi = metrics.density; System.out.println("densityDpi" + densityDpi); val1 = densityDpi * 1064; // here give random x value (co-ordinate) val2 = densityDpi * 1253; // here give random y value (co-ordinate) System.out.println("val1" + val1); System.out.println("val2" + val2); mark.setX(val1); mark.setY(val2); mark.setVisibility(View.VISIBLE); } } ;  public class NewClass extends Activity { ImageView mark; FrameLayout ground_gry_fl,zoom_lay; ScrollView sv; float val1 = 0.0f; float val2 = 0.0f; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_lay); sv=(ScrollView) findViewById(R.id.sv); mark = (ImageView) findViewById(R.id.marker_image); ground_gry_fl = (FrameLayout)findViewById(R.id.ground_gry_fl1); zoom_lay = (FrameLayout)findViewById(R.id.zoom_lay1); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int height = metrics.heightPixels; int width = metrics.widthPixels; System.out.println("width"+width); System.out.println("height"+height); float densityDpi = metrics.density; System.out.println("densityDpi" + densityDpi); val1 = densityDpi * 1064; // here give random x value (co-ordinate) val2 = densityDpi * 1253; // here give random y value (co-ordinate) System.out.println("val1" + val1); System.out.println("val2" + val2); mark.setX(val1); mark.setY(val2); mark.setVisibility(View.VISIBLE); } } ;  public class NewClass extends Activity { ImageView mark; FrameLayout ground_gry_fl,zoom_lay; ScrollView sv; float val1 = 0.0f; float val2 = 0.0f; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_lay); sv=(ScrollView) findViewById(R.id.sv); mark = (ImageView) findViewById(R.id.marker_image); ground_gry_fl = (FrameLayout)findViewById(R.id.ground_gry_fl1); zoom_lay = (FrameLayout)findViewById(R.id.zoom_lay1); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int height = metrics.heightPixels; int width = metrics.widthPixels; System.out.println("width"+width); System.out.println("height"+height); float densityDpi = metrics.density; System.out.println("densityDpi" + densityDpi); val1 = densityDpi * 1064; // here give random x value (co-ordinate) val2 = densityDpi * 1253; // here give random y value (co-ordinate) System.out.println("val1" + val1); System.out.println("val2" + val2); mark.setX(val1); mark.setY(val2); mark.setVisibility(View.VISIBLE); } } ;  public class NewClass extends Activity { ImageView mark; FrameLayout ground_gry_fl,zoom_lay; ScrollView sv; float val1 = 0.0f; float val2 = 0.0f; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_lay); sv=(ScrollView) findViewById(R.id.sv); mark = (ImageView) findViewById(R.id.marker_image); ground_gry_fl = (FrameLayout)findViewById(R.id.ground_gry_fl1); zoom_lay = (FrameLayout)findViewById(R.id.zoom_lay1); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int height = metrics.heightPixels; int width = metrics.widthPixels; System.out.println("width"+width); System.out.println("height"+height); float densityDpi = metrics.density; System.out.println("densityDpi" + densityDpi); val1 = densityDpi * 1064; // here give random x value (co-ordinate) val2 = densityDpi * 1253; // here give random y value (co-ordinate) System.out.println("val1" + val1); System.out.println("val2" + val2); mark.setX(val1); mark.setY(val2); mark.setVisibility(View.VISIBLE); } } ;  public class NewClass extends Activity { ImageView mark; FrameLayout ground_gry_fl,zoom_lay; ScrollView sv; float val1 = 0.0f; float val2 = 0.0f; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.new_lay); sv=(ScrollView) findViewById(R.id.sv); mark = (ImageView) findViewById(R.id.marker_image); ground_gry_fl = (FrameLayout)findViewById(R.id.ground_gry_fl1); zoom_lay = (FrameLayout)findViewById(R.id.zoom_lay1); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int height = metrics.heightPixels; int width = metrics.widthPixels; System.out.println("width"+width); System.out.println("height"+height); float densityDpi = metrics.density; System.out.println("densityDpi" + densityDpi); val1 = densityDpi * 1064; // here give random x value (co-ordinate) val2 = densityDpi * 1253; // here give random y value (co-ordinate) System.out.println("val1" + val1); System.out.println("val2" + val2); mark.setX(val1); mark.setY(val2); mark.setVisibility(View.VISIBLE); } } 

It must be placed at the same place in every device resolutions. You can scroll the image and see the point. His work fine for me.

0
source share

set display density

  float density = getActivity().getResources().getDisplayMetrics().density; if (density == 4.0) { displayMetrixDensity = 640; } if (density == 3.0) { displayMetrixDensity = 480; } if (density == 2.0) { displayMetrixDensity = 320; } if (density == 1.5) { displayMetrixDensity = 160; } if (density == 1.0) { displayMetrixDensity = 160; } 
0
source share

All Articles