簡體   English   中英

翻譯動畫無效

[英]Translate animation has no effect

我希望視圖從布局的右下角移至中心。 我在RelativeLayout內只有一個textView,並編寫了一個簡單的代碼對其進行測試,但沒有用。

這是我的動畫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" />

這是我的布局文件;

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

這是活動的相關部分;

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

問題是,textView“ x”不會移動。 我已經讀過,在XML文件中指定%p會使它相對於父級。 由於我希望視圖從其原始位置移動到屏幕中心,因此將“ from”和“ to”值設置為0%50%p 我觀察到,當我刪除%p屬性並將其從0%移到-100% ,它可以正確移動。 因此,相對定位在這里似乎不起作用。 我可能做錯了什么?

問題解決了。 相對值應為負,如下所示;

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

因為正值會將視圖移出布局。 因此, %0p值對應於布局的右下角。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM