Abdalrhman Alkhulaqi
Abdalrhman Alkhulaqi

Reputation: 333

How can I get ImageView position with support Right To Left direction

I want to get the position of my ImageView programmatically. That position is for imageView in pixels relative to screen NOT to parent. actually I found some solutions when searching they working While the Layout Direction is left-to-right, but when I change direction to right-to-left it gives me strange values( is this isseu). How can i get the position when the activity is rtl supporting.

some solution I have found:

1) private int getRelativeTop(View myView) {
if (myView.getParent() == myView.getRootView())
    return myView.getTop();
else
    return myView.getTop() + getRelativeTop((View) myView.getParent());} 


2) image.getLocationOnScreen(int[] locaiton);

UPDATE

In my activity i have three imageviews , i move (translte animation) image3 from image1 to image2 . start moving from position image1 to position image2, when i use ltr it is animate correctlly but when i change supportrtl="true" i do not see the animation at all.

this is xml file

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/rootParent"
    android:orientation="vertical"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/piece_FLOAT"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/piece1"
        android:translationZ="@dimen/activity_horizontal_margin" />


    <ImageView
        android:id="@+id/piece_1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="8dp"
        android:src="@drawable/piece1" />


    <ImageView
        android:id="@+id/piece_2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="8dp"
        android:src="@drawable/piece1" />



</LinearLayout>

this is java class

public class AnimateActivity extends AppCompatActivity {


    ImageView  imageViewfrom, imageViewto;
    ImageView  imageViewFLOAT;
    LinearLayout L_33;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_animate);

        imageViewfrom = (ImageView)findViewById(R.id.piece_1);
        imageViewto = (ImageView)findViewById(R.id.piece_2);


        imageViewFLOAT = (ImageView)findViewById(R.id.piece_FLOAT);

        assert imageViewfrom != null;
        assert imageViewto != null;
        imageViewfrom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int[] ToLocation = new int[2];
                imageViewto.getLocationOnScreen(ToLocation);

                float xTO =  ToLocation[0];//imageViewto.getX(); //ToLocation[0];
                float yTO = ToLocation[1];//imageViewto.getY();//ToLocation[1];

                int[] FromLocation = new int[2];
                imageViewfrom.getLocationOnScreen(FromLocation);
                float xFROM = FromLocation[0];//imageViewfrom.getX();///FromLocation[0];
                float yFROM = FromLocation[1]; //imageViewfrom.getY();//FromLocation[1];

                Log.e("xFrom =" + xFROM, "xTo =" + xTO );
                Log.e("yFrom =" + yFROM, "yTo =" + yTO );
              //  Log.e("offset =" + topOffset, "xTo =" + 0);
                ValueAnimator animatorX = ObjectAnimator.ofFloat(imageViewFLOAT, "x", xFROM, xTO).setDuration(1500);
                ValueAnimator animatorY = ObjectAnimator.ofFloat(imageViewFLOAT, "y", yFROM, yTO).setDuration(1500);
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.playTogether(animatorX, animatorY);
                animatorSet.start();
                //  Animation an = new TranslateAnimation(xFROM, xTO, yFROM , yTO);
               // an.setDuration(1500);

  //              an.setFillAfter(true);// to keep the state after animation is finished
//                imageViewFLOAT.startAnimation(an);// to start animation obviously
            }
        });
    }
}

when I use ltr it work very will but add some pixels. when I use rtl the animation did not seen. why this happen?

thanks for any help .

Upvotes: 1

Views: 1612

Answers (1)

Mike M.
Mike M.

Reputation: 39191

The issue here isn't really in finding the View's location, as that shouldn't be affected by the layout direction. The real problem is that View animations - especially those involving translations - apparently don't work well with RTL layouts. This is not surprising, since View animations have been around since the first versions of Android, but RTL support was only added in API level 17.

The solution is to use Property Animations instead.

For example, in your snippet, you're actually only translating that ImageView along the x-axis, so we can replace your entire TranslateAnimation setup with one line:

ObjectAnimator.ofFloat(imageViewFLOAT, "x", xFROM, xTO).setDuration(1500).start();

This creates an ObjectAnimator that modifies imageViewFLOAT's x-coordinate - by calling its setX() method - in the range [xFROM...xTO] over a duration of 1500 milliseconds, and immediately starts it.

Using an AnimatorSet, we can combine multiple animations to play together, so you can simultaneously perform a y-translation, as well. For example:

AnimatorSet as = new AnimatorSet();
as.playTogether(ObjectAnimator.ofFloat(imageViewFLOAT, "x", xFROM, xTO), 
                ObjectAnimator.ofFloat(imageViewFLOAT, "y", yFROM, yTO));
as.setDuration(1500).start();

Property animations and their classes are rather straightforward, and can do pretty much anything that the old View animations could. Updating your code to use these should be a simple fix for your RTL animation issues.

Upvotes: 2

Related Questions