-2

I have two questions:

I have an imageview that when I touch it I send it to an (X,Y) position. I have an S4 and a Galaxy Note 3 both with resolution (1920x1080) and density xxhdpi. When i test them, i notice the imageView in different devices not send to same position. In Galaxy note the imageView is more to the right and above... why is this happening? have the same resolution and belongs to same density (xxhdpi)! And how can i fix this for this example and all the screens?

Here is my XML code

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

And this is the method that move the card:

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

Another question I realized is why point1 have to set (-1460, 20) to be in the top left corner and not for example (20,20). I think is because it tooks the (0,0) from the views position. Its a posibility to change that?

Greets

user3240604
  • 407
  • 1
  • 8
  • 22
  • Can you post your XML view code? My first suspicion would be the layout of the menu/status bar is different between the devices resulting in different X,Y positions. If they are off by <100 dpi, this is likely the culprit. – Cookster May 04 '15 at 18:34
  • If you don't show us your layout xml we cannot diagnose the problem. – Xaver Kapeller May 04 '15 at 18:37

1 Answers1

0

To solve this problem I calculated the X and Y co-ordinates programatically by using DisplayMetrics class like this:

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

And then You can make the image view appear at that coordinate.

Abdul Rahman K
  • 664
  • 5
  • 16
  • The problem is that if a set an x y position, the (0,0) is not the left top corner, its where the imageview is! So if i set for example x = 30, y = 30 its not going to be near the top left corner, its going to go (30,30) from the position of the view. You understand? – user3240604 May 05 '15 at 14:39
  • You didn't mention it before. Anyways, in the layout you have placed one image relatively with respect to another img by setting android:layout_alignTop attribute and similar attributes hence changing coordinates doesn't change those attributes. – Abdul Rahman K May 05 '15 at 19:14