简体   繁体   中英

Translate animation has no effect

I want my view to move from bottom right corner of my layout to the center. I only have a textView inside a RelativeLayout and wrote a simple code to test it but did not work.

This is my animation XML;

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXDelta="0%" 
    android:fromYDelta="0%" 
    android:toXDelta="50%p"
    android:toYDelta="50%p" 
    android:duration="1000" 
    android:fillAfter="true" />

This is my layout file;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="x" />

</RelativeLayout>

And this is the related part of the Activity;

    x = (TextView) findViewById(R.id.textView1);
    Animation translate = AnimationUtils.loadAnimation(MainActivity.this,
            R.anim.viewanimation); 
    x.startAnimation(translate);

The problem is, the textView "x" doesn't move. I have read that specifying %p in XML file makes it relative to the parent. Since I want my view to move from its original position to center of screen, I set the "from" and "to" values as 0% and 50%p . I observed that when I remove %p attributes and make it move from something like from 0% to -100% , it moves correctly. So relative positioning does not seem to work here. What might I be doing wrong?

The problem solved. Relative values should be negative as follows;

android:toXDelta="-50%p"
android:toYDelta="-50%p" 

Because positive values move the view out of the layout. So, %0p values correspond to the right and bottom corner of the layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM