添加动画到Android应用程序

时间:2016-04-09 12:31:53

标签: animation customization

我目前正在开发一款新应用,我想为其添加自定义动画(不是谈论活动转换)。 为了准确地告诉你我的意思,请在2:30-2:33看一下这个视频: https://www.youtube.com/watch?v=XBMdjX5bbvk&nohtml5=False

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

谢谢!

1 个答案:

答案 0 :(得分:1)

对于你的问题:

  

你看到胸部跳到屏幕上并顺利打开   漂亮的动画?我真的很想知道如何添加到   Android应用程序,它是一个帧动画吗?

我不认为这是一个帧动画。我想这已经使用OpenGL实现了。您可以找到官方教程here

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

// 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();

注意:您不应该在活动的AnimationDrawable.start()方法中调用onCreate()。观点尚未准备好。你应该在onWindowFocusChanged()方法上使用回调并在那里开始动画:

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