25 帧动画案例

创建方法一:

Flash中的概念是一样动画一张一张的组合在一起 因为人眼接受视图是有缓存时间 所以当你图片飞快切换的时候造成是动画的效果

  • 在res/drawable/创建一个一xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--第一个参数图片地址-->
    <!--第二个参数持续时间-->
<item android:drawable="@mipmap/anim1" android:duration="50"></item>
<item android:drawable="@mipmap/anim2" android:duration="50"></item>
<item android:drawable="@mipmap/anim3" android:duration="50"></item>
<item android:drawable="@mipmap/anim4" android:duration="50"></item>
<item android:drawable="@mipmap/anim5" android:duration="50"></item>
<item android:drawable="@mipmap/anim6" android:duration="50"></item>
<item android:drawable="@mipmap/anim7" android:duration="50"></item>
<item android:drawable="@mipmap/anim8" android:duration="50"></item>
<item android:drawable="@mipmap/anim9" android:duration="50"></item>
<item android:drawable="@mipmap/anim10" android:duration="50"></item>
<item android:drawable="@mipmap/anim11" android:duration="50"></item>
<item android:drawable="@mipmap/anim12" android:duration="50"></item>
</animation-list>
  • 在布局文件中引用
<?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="qf.fmy.com.myfirst.MainActivity">

    <ImageView
        android:id="@+id/iv"
        android:background="@drawable/myanimation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="start" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="end" />
</LinearLayout>
  • 在java代码中启动
package qf.fmy.com.myfirst;

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

public class MainActivity extends AppCompatActivity {


    private TextView tv;
    private ScrollView sv;
    private ImageView iv;
    private AnimationDrawable ad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = ((ImageView) findViewById(R.id.iv));
        ad = (AnimationDrawable) iv.getBackground();

    }

    public void start(View view) {
        if (!ad.isRunning()) {
            ad.start();
        }
    }

    public void end(View view) {
        if (ad.isRunning()) {
            ad.stop();
        }
    }
}

创建方式二

  • 在java代码中创建AnimationDrawable对象
package qf.fmy.com.appmy2;

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

public class MainActivity extends AppCompatActivity {

    private ImageView iv;
    private AnimationDrawable animationDrawable;

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

        iv = ((ImageView) findViewById(R.id.iv));


        animationDrawable = new AnimationDrawable();

        //设置是否执行一次  如果执行一次则为true 
        // 注意:如果为true 执行一次后animationDrawable.isRunning()依然为已启动状态
        animationDrawable.setOneShot(false);
//        第一个参数图片 第二个参数持续时间
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0001),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0002),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0003),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0004),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0005),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0006),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0007),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0008),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0009),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0010),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0011),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0012),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0013),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0014),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0015),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0016),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0017),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0018),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0019),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0020),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0021),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0022),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0023),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0024),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0025),50);
        animationDrawable.addFrame(getResources().getDrawable(R.mipmap.loading0026),50);

        iv.setBackground(animationDrawable);
    }

    public void start(View view) {
        if (!animationDrawable.isRunning()){
            animationDrawable.start();
        }
    }

    public void end(View view) {

        if (animationDrawable.isRunning()){
            animationDrawable.stop();
        }
    }
}
原文地址:https://www.cnblogs.com/muyuge/p/6152176.html