【问题标题】:How to get same co-ordinates for image for all resolutions in android?如何为android中的所有分辨率获取相同的图像坐标?
【发布时间】:2015-08-12 23:25:32
【问题描述】:

我坚持为所有分辨率获取图像的 x 和 y 坐标。

  1. 320,适用于具有以下屏幕配置的设备:
    • 240x320 ldpi(QVGA 手机)
    • 320x480 mdpi(手机)
    • 480x800 hdpi(高密度手机)
  2. 480,适用于 480x800 mdpi(平板电脑/手机)等屏幕。
  3. 600,适用于 600x1024 mdpi(7 英寸平板电脑)等屏幕。
  4. 720,适用于 720x1280 mdpi(10 英寸平板电脑)等屏幕。

我创建了我的图像视图,图像的原始大小为 1500 x 1119。当​​我触摸图像时,我可以使用 onTouch() 获取坐标。如果在所有设备中更改坐标,我的问题。我在不同的欺骗中得到不同的 x & y 值。我无法为所有分辨率获得相同的 x & y 值。我该如何解决这个问题?

【问题讨论】:

  • 你想要点击图像上点的 x/y 坐标吗?
  • 是的。但是应该为所有分辨率转换 x/y 值。例如:在 600x1024 = 300,400 中,值必须在 1080x1920 = ? 中计算
  • 我假设您使用的是普通的ImageView,如果是这样,您可以发布布局 XML 吗?
  • 这是我的 layout.xml pastie.org/9280088
  • 嗨,请解释一下你想要什么。我不明白你的问题。您是想要触摸位置还是想要如何在不同分辨率的设备屏幕中适合图像。 ex 600X1024 是你的设备尺寸和图像尺寸是 300,400 0r 它是触摸 x,y 位置请在 brif 中解释

标签: android imageview coordinates


【解决方案1】:

这里有一个比您的特定布局所需的更通用的解决方案。这假设您知道源图像的尺寸并且想知道其上被点击的坐标,忽略屏幕尺寸和分辨率。尽管您的布局定义了与源图像匹配的宽度和高度,但这将处理宽度和高度设置为wrap_contentmatch_parent 的布局,前提是您使用适合整个图像的比例类型之一在视图边界内并保留纵横比(例如fitXY)。

我没有在ScrollViews 内部尝试过,但理论上应该没问题。

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);

【讨论】:

    【解决方案2】:

    我终于找到了答案。我确实喜欢这个。

    最初我是这样创建布局的。

    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>
    

    新类.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);
    
    }
    
    }
    

    它应该在设备的所有分辨率中放置相同的位置。您可以滚动图像并查看要点。它对我来说很好用。

    【讨论】:

      【解决方案3】:

      请设置显示密度

          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;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-18
        • 1970-01-01
        相关资源
        最近更新 更多