AnimationDrawable和逐帧动画

获取 AnimationDrawable对象之后,接下来就可用把 AnimationDrawable显示出来,—–习惯上把AnimationDrawable设为ImageView的背景即可.
需要注意的是 AnimationDrawable代表的动画默认是不播放的.必须在程序中启动动画播放才可以.AnimationDrawable 提供了两个方法() : 开始停止动画.

  1. start( ): 开始播放动画.
  2. stop( ): 停止播放动画.

package com.test.frameanimation;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 * 帧动画 AnimationDrawable
 */
public class MainActivity extends AppCompatActivity {
    Button play, stop;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        play = (Button) findViewById(R.id.btn_start);
        stop = (Button) findViewById(R.id.btn_stop);

        image = (ImageView) findViewById(R.id.id_image);

        //获取 AnimationDrawable 对象
        final AnimationDrawable anim = (AnimationDrawable) image.getBackground();

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anim.start();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anim.stop();
            }
        });

    }
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context="com.test.frameanimation.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >

        <Button
            android:id="@+id/btn_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始动画"/>

        <Button
            android:id="@+id/btn_stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止动画"/>

    </LinearLayout>

    <ImageView
        android:layout_gravity="center"
        android:id="@+id/id_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/fat_panda"
        android:scaleType="fitXY"
        />
</LinearLayout>

动画资源文件

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <!--添加多个 帧动画-->
    <item android:drawable="@drawable/fat_po_f01" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f02" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f03" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f04" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f05" android:duration="60"/>
    <item android:drawable="@drawable/fat_po_f06" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f07" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f08" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f09" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f10" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f11" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f12" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f13" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f14" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f15" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f16" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f17" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f18" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f19" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f20" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f21" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f22" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f23" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f24" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f25" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f26" android:duration="160"/>
    <item android:drawable="@drawable/fat_po_f27" android:duration="160"/>

</animation-list>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值