链接动画 - 新的位置不会在动画之间持续存在

时间:2015-08-08 22:15:40

标签: android

问题:

在下移动画完成后,文本视图将回到原始位置然后旋转。

期望效果:

textview在屏幕上向下移动,然后在其位置永远旋转

XML布局

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:gravity="center_horizontal"
    tools:context=".ActivityMain">

    <TextView
        android:id="@+id/tv_hello"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="20sp"/>

</RelativeLayout>

move.XML:

<?xml version="1.0" encoding="utf-8"?>

<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:interpolator="@android:anim/accelerate_interpolator">

    <!-- move down -->
    <translate
        android:duration="800"
        android:fillAfter="true"
        android:fromYDelta="0%p"
        android:startOffset="0"
        android:toYDelta="70%p" />

</set>

rotate.XML:

<?xml version="1.0" encoding="utf-8"?>

<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillBefore="true"
    android:fillAfter="true"
    android:interpolator="@android:anim/linear_interpolator">

    <!-- infinite rotate -->
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:duration="1200" />

</set>

Java代码:

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class ActivityMain extends AppCompatActivity {

    private TextView mTextView;
    private Animation mAnimationMove;
    private Animation mAnimationRotate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = (TextView) findViewById(R.id.tv_hello);

        mAnimationMove = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.move);
        mAnimationRotate = AnimationUtils.loadAnimation(ActivityMain.this, R.anim.rotate);

        mAnimationMove.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mTextView.startAnimation(mAnimationRotate);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {

            mTextView.startAnimation(mAnimationMove);

            return true;
        }

        return super.onOptionsItemSelected(item);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_activity_main, menu);
        return true;
    }
}

这怎么可能完成? 令人难以置信的是,看似简单的事情实际上非常困难!

1 个答案:

答案 0 :(得分:0)

改变这个:

<?xml version="1.0" encoding="utf-8"?>

<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/accelerate_interpolator">

<!-- move down -->
<translate
    android:duration="800"
    android:fillAfter="true"
    android:fromYDelta="0%p"
    android:startOffset="0"
    android:toYDelta="70%p" />

</set>

到这个

<?xml version="1.0" encoding="utf-8"?>

<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/accelerate_interpolator">

<!-- move down -->
<translate
    android:duration="800"
    android:fillEnabled="true"
    android:fromYDelta="0%p"
    android:startOffset="0"
    android:toYDelta="70%p" />

 </set>