如何在Android中获得所有分辨率图像的同一坐标?

7

我遇到了获取不同分辨率下图像的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坐标吗? - Dave Morrissey
是的。但是所有分辨率都应该转换x / y值。例如:在600x1024 = 300,400中,这些值必须在1080x1920中计算出来=? - malavika
我假设您正在使用普通的 ImageView,如果是这样,请问您能贴出布局 XML 吗? - Dave Morrissey
这是我的layout.xml文件 http://pastie.org/9280088 - malavika
2
你好,请解释一下你需要什么。我不理解你的问题。你是想获取触摸位置还是想知道如何适应不同分辨率设备屏幕中的图像。例如,你的设备尺寸为600X1024,图像尺寸为300、400或者它们的触摸x、y位置,请简要说明。 - Vijay Rajput
3个回答

3
这是一个比你的特定布局需要更通用的解决方案。这假设您知道源图像的尺寸,并想要知道在其上点击的坐标,忽略屏幕大小和分辨率。虽然您的布局定义了宽度和高度,看起来与源图像匹配,但如果您使用了将整个图像适配到视图边界并保持纵横比的缩放类型之一(例如fitXY),则可以处理宽度和高度设置为wrap_contentmatch_parent的布局。
我尚未尝试在像您一样的ScrollView内部使用此功能,但理论上应该可以正常工作。
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

终于我找到了答案。我是这样做的。

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

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

}

}

它应该在设备的所有分辨率下放置在同一位置。您可以滚动图像并查看该点。对我来说,它的工作非常好。


0
请设置显示密度。
    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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接