AnimationDrawable是实现Drawable animations的基本类。 推荐用XML文件的方法实现Drawable动画,不推荐在代码中实现。 animationDrawable = (AnimationDrawable)loadingImageView.getBackground(); loadingImageView.post 对象, 代码如下: rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); start()方法不能在onCreate() 因为AnimationDrawable并未完全关联到Window, 在onCreate()方法中,View并未完成显示(同理,在此方法中测量某个View的宽高,常得到0值。 : android.graphic.drawable.AnimationDrawable //获得我们xml定义的AnimationDrawable animDrawable
animationDrawable; private Button btn_start; private Button btn_stop; private ImageView = new AnimationDrawable(); //2.为帧动画添加内容(在小人书里添加内容) animationDrawable.addFrame ); //其他操作,如,设置只执行一次 animationDrawable.setOneShot(true); : //停止动画 animationDrawable.stop(); break; } 2、布局中将 AnimationDrawable 对象直接作为背景 3.在Activity文件中进行操作(播放,停止)
获取动画对象 animationDrawable = (AnimationDrawable) iv.getDrawable(); // 获取动画对象 animationDrawable = (AnimationDrawable) iv.getDrawable(); // // 直接从drawable文件夹获取动画资源(图片) animationDrawable = new AnimationDrawable(); for (int 获取资源对象 iv.setImageDrawable(animationDrawable); // 2. 停止动画 animationDrawable.stop(); } }); 4.
imageView_2 = findViewById(R.id.image_2); AnimationDrawable animationDrawable1 = new AnimationDrawable animationDrawable1.addFrame(getResources().getDrawable(R.drawable.iron_2 ),200); animationDrawable1 animationDrawable1.addFrame(getResources().getDrawable(R.drawable.iron_8 ),200); animationDrawable1 .setOneShot(true); imageView_2.setImageDrawable(animationDrawable1); animationDrawable1 animationDrawable = (AnimationDrawable) imageView_1.getDrawable(); animationDrawable.start()
anim = (AnimationDrawable)getDrawable(); anim.start(); } /** * 带动画监听的播放 * @param resId * anim = (AnimationDrawable)getDrawable(); anim.start(); if(listener ! AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); // Start the animation AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); Drawable getDrawable( anim = (AnimationDrawable)getDrawable();//得到一个AnimationDrawable对象 anim.start();//然后启动动画 }
//将帧动画的资源文件设置为imageview的背景 iv.setBackgroundResource(R.drawable.frameanimation); c:获得AnimationDrawable 对象 //获取AnimationDrawable对象 AnimationDrawable ad = (AnimationDrawable) iv.getBackground //将帧动画的资源文件设置为imageview的背景 iv.setBackgroundResource(R.drawable.frameanimation); //获取AnimationDrawable 对象 AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); //开始播放动画 ad.start
设置动画 animationDrawable = (AnimationDrawable) iv.getDrawable(); // 2. 获取动画对象 animationDrawable.start(); // 3. 设置动画 animationDrawable = (AnimationDrawable) iv.getDrawable(); // 2. 获取动画对象 animationDrawable.stop(); // 3. (true); iv.setImageDrawable(animationDrawable); animationDrawable.stop
以imageview为载体可以在xml设置src和在类中用iamgeview.getDrawable获取AnimationDrawable,再调用start方法或者stop方法。 <? > </animation-list> 调用帧动画 写好xml文件我们就可以在类中通过以下方法来调用动画 mImageView.setImageResource(R.drawable.frame); AnimationDrawable drawable = (AnimationDrawable) mImageView.getDrawable(); drawable.start(); ```# 帧动画 下面我们来说什么是帧动画。 以imageview为载体可以在xml设置src和在类中用iamgeview.getDrawable获取AnimationDrawable,再调用start方法或者stop方法。 drawable = (AnimationDrawable) mImageView.getDrawable(); drawable.start();
image.backgroundDrawable = resources.getDrawable(R.drawable.frame_drawable_test) // getBackground() 取得 AnimationDrawable 对象 val animDrawable : AnimationDrawable = image.background as AnimationDrawable // 启动动画 animDrawable.start () // 作为 Resource image.imageResource = R.drawable.frame_drawable_test // getDrawable() 取得 AnimationDrawable 对象 val animDrawable2 : AnimationDrawable = image.drawable as AnimationDrawable animDrawable.stop() / / 停止动画 animDrawable2.start() 代码方式 val animDrawable = AnimationDrawable() animDrawable.isOneShot
变回最初的状态 repeatCount:重复的次数(不包括第一次) startOffset:距离动画开始的时间 repeatMode:1表示重新开始,2表示从最后一个状态往回逆序播放 帧动画: 实现方法 1.实例AnimationDrawable import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.drawable.AnimationDrawable rotateAnimation); scaleAnimation.setStartOffset(10000); set2.addAnimation(scaleAnimation); /** * 帧动画 */ //第一种 //AnimationDrawable 间接继承Drawable AnimationDrawable ad = new AnimationDrawable(); Drawable frame1 = getResources().getDrawable ad1 = (AnimationDrawable) tv.getBackground(); ad1.start(); } @Override public void finish() { super.finish
com.example.exmgif.util.GifImage.GifFrame; import android.app.Activity; import android.graphics.drawable.AnimationDrawable return getResources().getDrawable(id); } private void showFrameAnimation() { //帧动画需要把每帧图片加入AnimationDrawable 队列 AnimationDrawable animationList = new AnimationDrawable(); animationList.addFrame(getDraw(R.drawable.flow_p1 gifList = new AnimationDrawable(); for (int i=0; i<frameList.length; i++) { //BitmapDrawable用于把 》 3、NinePatchDrawable:详见《Android开发笔记(九)特别的.9图片》 4、TransitionDrawable:详见《Android开发笔记(十五)淡入淡出动画》 5、AnimationDrawable
Drawable子类之——InsetDrawable (嵌入) ClipDrawable Drawable子类之——ClipDrawable (裁剪图像) ScaleDrawable RotateDrawable AnimationDrawable item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list> 我们可以看到,AnimationDrawable 我们可以通过编码来加载播放动画: // Load the ImageView that will host the animation and // set its background to our AnimationDrawable AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); // Start the animation frameAnimation.start(); 注意:AnimationDrawable. start()方法不能够在Activity的onCreate()方法中调用,因为AnimationDrawable
aniImageView.setImageResource(R.drawable.pull_end_imag_frame); AnimationDrawable animationDrawable = (AnimationDrawable) aniImageView.getDrawable(); // 只执行一次动画 animationDrawable.setOneShot(true); animationDrawable.start aniImageView.setImageResource(R.drawable.refreshing_image_frame); AnimationDrawable animationDrawable = (AnimationDrawable) aniImageView.getDrawable(); animationDrawable.start
item> </animation-list> 这样图片就一层层的叠起来了 动画调用 public class MainActivity extends Activity { private AnimationDrawable findViewById(R.id.loading); loadingImg.setBackgroundResource(R.drawable.loading); loadingAnimation = (AnimationDrawable super.onWindowFocusChanged(hasFocus); if (hasFocus) loadingAnimation.start(); } } 需要注意的是,不能在onCreate()方法中调用AnimationDrawable 的start()方法,因为此时AnimationDrawable还未真正加载到界面中。
帧动画最简单,通过顺序播放一系列的图像产生动画,有点类似动画片 以tomcat案例来讲解 1、首先准备好一组图片(网上找的现成的一组图片),然后定义一个AnimationDrawable,命名为ani.xml android:layout_height="match_parent" android:background="@drawable/ani" /> </RelativeLayout> 3、通过AnimationDrawable R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.imageView1); final AnimationDrawable background = (AnimationDrawable) imageView .getBackground(); imageView.setOnClickListener
item设置好每一个帧的图片以及对应的延时; 如此便定义好了AnimationDrawable对象; 接着使用AnimationDrawable, 在某个Activity或者fragment的布局文件中 ();获得view的背景Drawable对象并向下转型为AnimationDrawable对象, 然后赋给一个AnimationDrawable的实例变量引用; 使用AnimationDrawable 并编写如下代码: 代码中通过<animation-list>来定义AnimationDrawable对象, 一个animation-list便签对应(映射为)一个AnimationDrawable对象 , // 所以可以做一个强制转换(窄化、向下转型) animationDrawable = (AnimationDrawable)view.getBackground() ()返回的是一个Drawable对象, 在这里已经明确背景是AnimationDrawable, 所以可以做一个强制转换(窄化、向下转型); 后面是简单的点击事件: 使用AnimationDrawable
第三步:在java文件中实现方法 起到主要作用的代码: animaion.setImageResource(R.drawable.animaion); AnimationDrawable drawable= (AnimationDrawable) animaion.getDrawable(); drawable.start();
Java 代码(MainActivity.java)import android.graphics.drawable.AnimationDrawable;import android.os.Bundle 对象,用于控制帧动画 private AnimationDrawable frameAnimation; @Override protected void onCreate(Bundle 从 ImageView 的背景中获取 AnimationDrawable 对象 frameAnimation = (AnimationDrawable) ivAnimation.getBackground ) ivAnimation.getBackground() 从 ImageView 的背景中提取 AnimationDrawable,该对象是帧动画的核心控制器;触发动画:在 onTouchEvent 检查这 3 点确认 AnimationDrawable 对象是否正确获取:需先设置 BackgroundResource,再获取背景,顺序不能颠倒;避免在 onCreate 中直接调用 start():
第二步: 将xml文件用和AnimationDrawable关联。(我们是xml实现也可以代码实现,demo中有此处省略。。。。) AnimationDrawable mAnimationDrawable =(AnimationDrawable) getResources().getDrawable( R.drawable.anim_frame 然后初始化Imageview 在.setBackground(animationDrawable);即可。 完毕!
<item android:drawable="@drawable/f8" android:duration="80"/> </animation-list> ② 在main.java中创建AnimationDrawable 类 private AnimationDrawable animationDrawable; //初始化, animationDrawable= (AnimationDrawable) imageView.getBackground (); //开启 animationDrawable.start(); 7 日期控件 //时间控件 private Calendar calendar; private DatePickerDialog