-2

我有两个问题:

我有一个图像视图,当我触摸它时,我会将它发送到(X,Y)位置。我有一个分辨率(1920x1080)和密度为 xxhdpi 的 S4 和 Galaxy Note 3。当我测试它们时,我注意到不同设备中的 imageView 没有发送到相同的位置。在 Galaxy note 中,imageView 更多地位于右侧和上方……为什么会发生这种情况?具有相同的分辨率并属于相同的密度(xxhdpi)!对于这个例子和所有的屏幕,我该如何解决这个问题?

这是我的 XML 代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/fondo_jugar"
tools:context="com.trucouruguayo.JugarActivity" >


<ImageView
    android:id="@+id/imageViewCard1"
    android:layout_width="101dp"
    android:layout_height="142.50dp"
    android:layout_alignLeft="@+id/imageViewMazo"
    android:layout_alignTop="@+id/imageViewMazo"
    android:layout_marginRight="-50dp"
    android:contentDescription="@string/imageViewDescriptionCartd1"
    android:src="@drawable/reverso"
    android:scaleType="fitXY" />

<ImageView
    android:id="@+id/imageViewCard2"
    android:layout_width="101dp"
    android:layout_height="142.50dp"
    android:layout_alignLeft="@+id/imageViewMazo"
    android:layout_alignTop="@+id/imageViewMazo"
    android:layout_marginRight="-50dp"
    android:contentDescription="@string/imageViewDescriptionCard2"
    android:src="@drawable/reverso"
    android:tag="imageViewCarta1"
    android:scaleType="fitXY" />

    ...

</<RelativeLayout >

这是移动卡片的方法:

Point point1 = new Point(-1460, 20);

    /*Tira la carta al a mesa (sea de la maquina o del jugador)*/
private void tirarCartaALaMesa(View view, List<Carta> cartas, boolean tiraMaquina) {

    cartas.remove(obtenerCartaFromView(view, cartas));
    listaImageViewsTiradas.add(view);
    indiceOrdenCartas ++;

    animSetXY = new AnimatorSet();
    ObjectAnimator scaleDownX = ObjectAnimator.ofFloat(view, "scaleX", 0.8f);
    ObjectAnimator scaleDownY = ObjectAnimator.ofFloat(view, "scaleY", 0.8f);
    ObjectAnimator rotation;

    objectX = ObjectAnimator.ofFloat(view, "translationX", point1.x);
    objectY = ObjectAnimator.ofFloat(view, "translationY", point1.y);

    animSetXY.setInterpolator(new LinearInterpolator());
    animSetXY.setDuration(500);
    animSetXY.start();
}

我意识到的另一个问题是为什么 point1 必须设置 (-1460, 20) 位于左上角,而不是例如 (20,20)。我认为是因为它从视图位置获取了 (0,0) 。它有可能改变它吗?

问候

4

1 回答 1

0

为了解决这个问题,我使用 DisplayMetrics 类以编程方式计算 X 和 Y 坐标,如下所示:

DisplayMetrics metrics= new DisplayMetrics();
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
display.getMetrics(metrics);

int x=metrics.widthPixels/2; //2 can be changed according to the need
int y=metrics.heightPixels/2; //2 can be changed according to the need

然后您可以使图像视图出现在该坐标处。

于 2015-05-05T05:20:54.027 回答