首页 > 解决方案 > 通过重启活动继续动画(Android)

问题描述

我有这个代码:

            Refresh.startAnimation(
                    AnimationUtils.loadAnimation(getActivity(),R.anim.rotate));

            Intent intent = getActivity().getIntent();
            getActivity().finish();
            getActivity().overridePendingTransition(0, 0);
            startActivity(intent);

我想要它,所以在活动重置后,“刷新”静止图像上的动画会继续。我该怎么做呢?现在它只是在重置时停止。

标签: androidanimationandroid-intentandroid-activityandroid-animation

解决方案


//code to restart you activity

public static void restartActivity(Activity activity){
        Intent intent = new Intent();
        intent.setClass(activity, activity.getClass());
        activity.startActivity(intent);
        activity.overridePendingTransition(R.anim.slideup, R.anim.keep_activity_static);
        activity.finish();
}

//now this is to animate the exit of activity

@Override
public void finish() {
    super.finish();
    overridePendingTransition(R.anim.keep_activity_static, R.anim.slidedown);
}

//now the anim file keep_activity_static

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <translate
       android:fromYDelta="0%p"
       android:toYDelta="0%p"
       android:fillAfter="true"
       android:duration="1000" />
</set>

//anim file slidedown
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <translate
       android:fromYDelta="0%p"
       android:toYDelta="100%p"
       android:fillAfter="true"
       android:duration="200"/>
</set>

//anim file slideup

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <translate
       android:fromYDelta="100%p"
       android:toYDelta="0%p"
       android:fillAfter="true"
       android:duration="200"/>
</set>

推荐阅读