帧动画

   前面讲了三大动画的视图动画,现在就来简单介绍一下帧动画。

   现在先来看一下效果:

            

   一.使用xml布局的形式

   1.在drawable目录下新建一个xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/icon1"
        android:duration="300"></item>
    <item
        android:drawable="@drawable/icon2"
        android:duration="300"></item>
    <item
        android:drawable="@drawable/icon3"
        android:duration="300"></item>
    <item
        android:drawable="@drawable/icon4"
        android:duration="300"></item>
    <item
        android:drawable="@drawable/icon5"
        android:duration="300"></item>
    <item
        android:drawable="@drawable/icon6"
        android:duration="300"></item>
</animation-list>
   2.布局xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/main3_image"
        android:src="@drawable/icon1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
   3.Java代码

public class Main3Activity extends AppCompatActivity{

    private ImageView image;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
    }

    private void initView() {
        image= (ImageView) findViewById(R.id.main3_image);
        image.setImageResource(R.drawable.my_frame_animation);
        AnimationDrawable animationDrawable= (AnimationDrawable) image.getDrawable();
        animationDrawable.start();

    }
}
   二.使用代码的形式

public class Main3Activity extends AppCompatActivity{

    private ImageView image;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
    }

    private void initView() {
        AnimationDrawable animationDrawable=new AnimationDrawable();
        Drawable drawable=getResources().getDrawable(R.drawable.icon1);
        Drawable drawable2=getResources().getDrawable(R.drawable.icon2);
        Drawable drawable3=getResources().getDrawable(R.drawable.icon3);
        Drawable drawable4=getResources().getDrawable(R.drawable.icon4);
        Drawable drawable5=getResources().getDrawable(R.drawable.icon5);
        Drawable drawable6=getResources().getDrawable(R.drawable.icon6);
        //添加图片
        animationDrawable.addFrame(drawable,300);
        animationDrawable.addFrame(drawable2,300);
        animationDrawable.addFrame(drawable3,300);
        animationDrawable.addFrame(drawable4,300);
        animationDrawable.addFrame(drawable5,300);
        animationDrawable.addFrame(drawable6,300);

        animationDrawable.setOneShot(false);
        image.setImageDrawable(animationDrawable);
        animationDrawable.start();
    }
}



  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值