动画的使用1——Drawable Animation
来源:互联网 发布:家庭用网络交换机 编辑:程序博客网 时间:2024/05/24 06:49
Drawable Animation可以称为帧动画,因为它是通过每次播放一帧Drawable资源实现的。
Drawable Animation算不上真正意义上的动画,因为它的内部实现是通过定时发送消息更新一个Drawable,
例如一个背景。所以使用这个动画的时候更像是使用一个背景资源,只不过更新背景的动作不用我们自己进行。
也许正是因为这个原因,android官方建议我们将这个动画资源放在drawable目录。
/**
* @description android中的逐帧动画.
* 逐帧动画的原理很简单,跟电影的播放一样,一张张类似的图片不停的切换,当切换速度达到一定值时,
* 我们的视觉就会出现残影,残影的出现保证了视觉上变化的连续性,这时候图片的切换看在我们眼中就跟真实的一样了。
- 想使用逐帧动画:
- 第一步:需要在res/drawable文件夹下新建一个xml文件,该文件详细定义了动画播放时所用的图片、切换每张图片
- 所用的时间、是否为连续播放等等。(有些文章说,在res/anim文件夹下放置该文件,事实证明,会出错哦)
- 第二步:在代码中,将该动画布局文件,赋值给特定的图片展示控件,如本例子中的ImageView。
- 第三步:通过imageView.getBackGround()获取相应的AnimationDrawable对象,然后通过该对象的方法进行控制动画
-
*/
使用帧动画非常之简单,只需要在drawable目录定义个xml文件,使用animation-list标签包裹所有组成这个动画的图片文件,设置播放速率。
然后在java代码中使用。
如图:
<?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/a" android:duration="200"/> <item android:drawable="@drawable/b" android:duration="200"/> <item android:drawable="@drawable/c" android:duration="200"/> <item android:drawable="@drawable/d" android:duration="200"/></animation-list>
android:oneshot=”false”可以让动画循环播放
android:oneshot=”true”动画只能播放一次
里面的命名不规范自己改正一下
然后再layout写代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:text="点击开始,长按终止" android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
这样就定义好了我们的动画资源,之后就是在activity中调用
package com.example.drawableanimation;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity { AnimationDrawable ad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imageview=(ImageView) findViewById(R.id.image); ad = (AnimationDrawable) getResources().getDrawable(R.drawable.animation_list); //获得动画资源 imageview.setBackgroundDrawable(ad); // imageview.setBackgroundResource(R.drawable.animation_list); // ad=(AnimationDrawable) imageview.getBackground(); // ad.setAlpha(80); //设置图片透明度 Button button=(Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { //点击按钮 @Override public void onClick(View arg0) { // TODO Auto-generated method stub ad.start(); } }); button.setOnLongClickListener(new View.OnLongClickListener() { //长点击按钮 @Override public boolean onLongClick(View arg0) { // TODO Auto-generated method stub ad.stop(); return true; //return false; } }); }
上面的代码就实现了简单的动画效果了
除此之外:在AnimationDrawable中,我们还可以看到如下的几个重要方法:
setOneShot(boolean flag) 和在配置文件中进行配置一样,可以设置动画是否播放一次,false为连续播放;
addFrame (Drawable frame, int duration) 动态的添加一个图片进入该动画中
- 动画的使用1——Drawable Animation
- 动画——帧动画(Drawable Animation)
- 动画——View Animation,Drawable Animation,Property Animation,LayoutTransition
- Android Drawable系列——Animation Drawable(动画实现)
- Android帧动画(Drawable Animation)的使用总结
- Android应用开发——Animation Drawable ImageView显示动画
- Android动画-Drawable Animation
- Drawable Animation(Android动画)
- Android 的动画 I-基础知识和View Animation&Drawable Animation
- Android当中的动画1—Drawable动画
- Animation特效——Drawable Animation
- Android Animation——drawable animation
- Android 动画之一 Drawable Animation —— 逐帧(Frame)动画
- Drawable Animation -- 使用多幅图片实现动画
- 【实战】使用animation drawable来实现动画效果
- Android绘图动画---Drawable Animation
- Android动画之Drawable Animation
- Android动画之一:Drawable Animation
- sql注入之宽字节注入
- linux Signal信号详解
- 打包jar包时文件读取和第三方jar包的问题
- 各种开源协议以及如何选择
- 装箱问题
- 动画的使用1——Drawable Animation
- LeetCode Blog for course "Algorithms" -- Problem 1 & 2
- spring bean加载对象简介
- 【JAVASE_学习笔记】基础知识
- Cgroup和Namespace在测试中的使用(上)
- Keras学习笔记---一
- Oreacle数据库安装
- Cgroup和Namespace在测试中的使用(下)
- homework