向 Android 应用程序添加动画

Adding animation to Android application

我目前正在开发一个新应用,我想向其中添加自定义动画(不讨论 activity 过渡)。 为了准确地向您展示我的意思,请在 2:30-2:33 观看此视频: https://www.youtube.com/watch?v=XBMdjX5bbvk&nohtml5=False

你看到宝箱跳到屏幕上并以漂亮的动画顺利打开了吗? 我很想知道如何将它添加到 Android 应用程序中,它是帧动画吗? 我的意思是,我可以在 2D 中制作这个动画,我只是想知道如何添加它(我正在使用 Android Studio)而不会导致内存溢出。

谢谢!

你的问题:

You see the chest jumping to the screen and opens smoothly with beautiful animation? I'd really like to know how can it be added to an Android app, is it a frame animation?

我觉得不是帧动画。我想这是使用 OpenGL 实现的。可以找官方教程here.

如果想制作简单的二维动画,可以使用android提供的AnimationDrawableapi。您基本上需要动画序列的帧,然后您可以使用以下代码创建动画:

// you would need an `ImageView` object as a placeholder for the animation
ImageView mMascotView = findViewById(...);

// prepare the animation object ..
AnimationDrawable mMascotAnimation = new AnimationDrawable();

final int frameTime = 250; // time in milliseconds

// adding the frames to the animation object. You can specify different
// times for each of these in milliseconds
mMascotAnimation.addFrame(getResources().getDrawable(R.drawable.frame1),frameTime);
mMascotAnimation.addFrame(getResources().getDrawable(R.drawable.frame2),frameTime);
mMascotAnimation.addFrame(getResources().getDrawable(R.drawable.frame3),frameTime);


// make it loop infinitely ..
mMascotAnimation.setOneShot(false);

// set the background of the `ImageView` as the `AnimationDrawable`object ..
mMascotView.setBackground(mMascotAnimation);

// start the animation ..
mMascotAnimation.start();

注意:你不应该在activity的onCreate()方法中调用AnimationDrawable.start()。意见还没有准备好。您应该使用 onWindowFocusChanged() 方法的回调并在那里启动动画:

@Override
public void onWindowFocusChanged (boolean hasFocus)
{
      //Start animation here
      if(hasFocus) {
           mMascotAnimation.start();
      }
}