Android动画详解之Android 动画属性和实现方法之帧动画(二)

一、简介

Frame AnimationAnimationDrawable对象):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。

必须以<animation-list>为根元素,以<item>表示要轮换显示的图片,duration属性表示各项显示的时间。XML文件要放在/res/anim/或者/res/animator目录下。

二、代码实现

在frame_animation.xml下

<?xml version="1.0" encoding="utf-8" ?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@mipmap/lw0" android:duration="50" />
    <item android:drawable="@mipmap/lw1" android:duration="50" />
    <item android:drawable="@mipmap/lw2" android:duration="50" />
    <item android:drawable="@mipmap/lw3" android:duration="50" />
    <item android:drawable="@mipmap/lw4" android:duration="50" />
    <item android:drawable="@mipmap/lw5" android:duration="50" />
    <item android:drawable="@mipmap/lw6" android:duration="50" />
    <item android:drawable="@mipmap/lw7" android:duration="50" />
    <item android:drawable="@mipmap/lw8" android:duration="50" />
    <item android:drawable="@mipmap/lw9" android:duration="50" />
</animation-list>

drawable是绑定图片,duration是设置每张图片持续时间

主页面的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.lw.frameanimation_demo.MainActivity">
    <ImageView
        android:id="@+id/myImageView"
        android:layout_centerInParent="true"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:src="@anim/frame_animation"/>
    <Button
        android:layout_centerHorizontal="true"
        android:id="@+id/myButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open/Stop"/>

</RelativeLayout>

MainActivity代码

public class MainActivity extends AppCompatActivity {
    private ImageView imageView;
    private Button button;
    private AnimationDrawable animationDrawable = null;
    private void initView(){
        imageView = (ImageView) findViewById(R.id.myImageView);
        button = (Button) findViewById(R.id.myButton);
        //mageView.setBackgroundResource(R.anim.frame_animation);
        animationDrawable = (AnimationDrawable) imageView.getDrawable();
    }
    private void initCtrl(){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //每次从头开始播放
                if(!animationDrawable.isRunning()){
                    //是否执行一次
                    animationDrawable.setOneShot(false);
                    animationDrawable.start();
                }else{
                    animationDrawable.setOneShot(false);
                    animationDrawable.stop();
                }
            }
        });

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initCtrl();
    }
    //进入的时候播放
    // SDK中提到,不要在onCreate()中调用start(),因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            animationDrawable.setOneShot(false);
            animationDrawable.start();
        }
    }
}
其中:

 SDK中提到,不要在onCreate()中调用start(),因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。
源码下载:源码



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值