📜  Android 动画示例

📅  最后修改于: 2020-10-11 03:59:00             🧑  作者: Mango

Android动画示例

Android为动画开发提供了大量的类和接口。大多数类和接口在android.animation包中提供。

Android动画使您可以在运行时更改对象的属性和行为。有多种方法可以在android中制作动画。

AnimationDrawable类提供了开始和结束动画的方法。甚至,您也可以使用基于时间的动画。

让我们看一下android动画的简单示例。

activity_main.xml

您只需要查看。



    


仅具有图像视图。





MainActivity类

package com.javatpoint.animation;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.widget.ImageView;

public class MainActivity extends Activity {

ImageView anm;
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.logo);
        anm = (ImageView)findViewById(R.id.anm);
        
        anm.setBackgroundResource(R.drawable.animation);
// the frame-by-frame animation defined as a xml file within the drawable folder
        
        /*
         * NOTE: It's not possible to start the animation during the onCreate.
         */
    }
 public void onWindowFocusChanged (boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
AnimationDrawable frameAnimation = 
(AnimationDrawable) anm.getBackground();
if(hasFocus) {
frameAnimation.start();
} else {
frameAnimation.stop();
}
}

}

您需要在res / drawable-hdpi目录中创建animation.xml文件。

您需要有很多图像。在这里,我们使用14张图像,所有14张图像都位于res / drawable-mdpi目录中。




































输出: