
视图动画由两部分组成,补间动画和逐帧动画,下面讲解逐帧动画。
frame-by-frame Animation主要作用于view,可以利用xml或者代码生成动画,如果使用xml方式生成动画需要在res/drawable 目录下创建动画xml文件(animation-list)。
逐帧动画的原理是一张一张的播放图片资源(drawable资源),然后出现动画效果。
逐帧动画对应的类是AnimationDrawable,在android.graphics.drawable.Drawable包名下。
1.构造函数:逐帧动画使用方式:把逐帧动画作为view的背景,然后获取动画,开启动画。
AnimationDrawable()
2.属性说明:oneshot:是否只播放一次,取值true,false,默认为false,用于animation-listduration:每个item(每一帧动画)播放时长drawable: 每一帧的drawable资源visible:drawable资源是否可见,默认不可见 3.AnimationDrawable的主要函数:
addframe(Drawable frame, int duration)添加drawableframe:每一帧的图片资源duration:每一帧的持续动画start():开始动画stop(): 结束动画isRunning():是否正在执行setoneShot(boolean oneShot):设置是否只播放一次getNumberOfframes() :获取帧的动画数 二、使用方式 1.xml使用方式
首先定义animation-list 类型的drawable frameanimation.xml
// drawable= 图片资源;duration = 一帧持续时间(ms)
设置动画资源的三种使用方式
第一种:
// 设置动画 imageView.setImageResource(R.drawable.frameanimation); // 获取动画对象 animationDrawable = (AnimationDrawable)imageView.getDrawable(); animationDrawable.start();
第二种
设置背景:
imageView.setBackgroundResource(R.drawable.frameanimation); animationDrawable = (AnimationDrawable) imageView.getBackground()
第三种
直接获取然后设置:
animationDrawable = (AnimationDrawable) getResources().getDrawable( R.drawable.frameanimation); imageView.setBackground(animationDrawable)2代码使用方式
animationDrawable = new AnimationDrawable(); // 为AnimationDrawable添加动画帧 animationDrawable .addframe(getResources().getDrawable(R.drawable.d1), 50); imageView.setBackground(animationDrawable );三、代码示例 1.xml实现方式
imageView = findViewById(R.id.imageview); imageView.setImageResource(R.drawable.animationlist); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); animationDrawable.start();2.代码实现方式
AnimationDrawable animationDrawable = new AnimationDrawable(); animationDrawable.setOneShot(false); animationDrawable.addframe(getResources().getDrawable(R.drawable.compositedst1),1000); animationDrawable.addframe(getResources().getDrawable(R.drawable.compositedst2),1000); animationDrawable.addframe(getResources().getDrawable(R.drawable.compositedst3),1000); animationDrawable.addframe(getResources().getDrawable(R.drawable.compositedst4),1000); imageView.setImageDrawable(animationDrawable); animationDrawable.start();
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)