Android第一次动画不流畅 [英] Android first time animation is not smooth

查看:221
本文介绍了Android第一次动画不流畅的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一个动画将imageView从屏幕底部向上滑动到屏幕中心,但是当我第一次执行此动画时,它并不平滑,但是当我第二次执行动画时,它是正常且平滑的.我已经尝试了几乎所有内容,但无法解决问题.

I am trying one animation to slide up imageView from bottom of screen to center of screen but when I do this animation for very first time it's not smooth but when do animation for second time it's normal and smooth. I have tried almost everything but I couldn't solve my issue.

这是我的动画文件

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="700"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5"/>
    <translate
        android:fromXDelta="0%"
        android:toXDelta="0%"
        android:fromYDelta="300%"
        android:toYDelta="0%"
        android:duration="2000"
        android:zAdjustment="top" />
</set>

这就是我在片段上显示的方式

and this how I am displaying on my fragment

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    private Animation anim;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startAnimations();
    }

    private void startAnimations() {
        anim = AnimationUtils.loadAnimation(this, R.anim.slide_up_animation);
        ImageView imageView = (ImageView) findViewById(R.id.splash);
        imageView.startAnimation(anim);
    }
}

推荐答案

首先要确保您在主线程上没有做任何繁重的工作.如果需要,可以将其移至后台线程

The first thing to make sure is that you are not doing any heavy work on your main thread.If something is required, you can move it to the background thread.

由于您已经提到要在片段中显示,因此应将动画代码放入以下方法中,以确保仅在片段处于活动状态时才发生动画.另外,您应该始终在onDestroy()中停止动画.

Since you have mentioned displaying in a fragment, you should put your animation code inside the following method.It will make sure that the animation happens only if the fragment is active. Also, you should always stop your animation in onDestroy().

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
  super.setUserVisibleHint(isVisibleToUser);
}

这篇关于Android第一次动画不流畅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆