Android学习笔记进阶十一图片动画播放(AnimationDrawable)
2024-09-05 00:54:04
大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它。它的使用更加简单,只需要创建一个
AnimationDrawabledF对象来表示Frame动画,然后通过addFrame 方法把每一帧要显示的内容添加进去,并设置播放间隔时间,本例子中间隔时间为5S,
最后通过start 方法就可。
以播放这个动画了,同时还可以通过 setOneShot方法设置是否重复播放。
- package xiaosi.bu;
- import android.app.Activity;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class TupianActivity extends Activity {
- /** Called when the activity is first created. */
- private Button start = null;
- private Button stop = null;
- private ImageView image = null;
- private AnimationDrawable animationDrawable = null;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- start = (Button)findViewById(R.id.start);
- start.setOnClickListener(new StartListener());
- stop = (Button)findViewById(R.id.stop);
- stop.setOnClickListener(new StopListener());
- image = (ImageView)findViewById(R.id.imageview);
- animationDrawable = new AnimationDrawable();
- for(int i =0;i<8;i++){
- //第一个 就是我们的资源名称(图片名)
- //第二个 就是我们存放图片的文件夹drawable
- //第三个 包名也可以用Context的getPackageName返回应用程序的包名
- int id = getResources().getIdentifier( "a"+i, "drawable", "xiaosi.bu");
- System.out.println("ID:" + id);
- animationDrawable.addFrame(getResources().getDrawable(id), 2000);
- }
- //设置手否重复播放,false为重复
- animationDrawable.setOneShot(false);
- image.setImageDrawable(animationDrawable);
- }
- private class StartListener implements OnClickListener{
- public void onClick(View v)
- {
- animationDrawable.start();
- }
- }
- private class StopListener implements OnClickListener{
- public void onClick(View v)
- {
- animationDrawable.stop();
- }
- }
- }
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <Button android:id="@+id/start"
- android:text="Start"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <Button android:id="@+id/stop"
- android:text="End"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <ImageView android:id="@+id/imageview"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scaleType="fitXY"
- android:background="#ffffff" />
- </LinearLayout>
源代码:点击打开链接
最新文章
- 转 用JS实现PHP的sprintf函数
- [转]10个学习Android开发的网站推荐
- Java 装饰模式 (Decorator)
- SVN Cleanup failed的解决办法
- Attributes(2): Displaying attributes for a class.(显示类属性)
- (总结)Nginx配置文件nginx.conf中文详解 <;转>;
- ajax创建对象
- spring 运行时属性值注入
- Python内置函数(5)——pow
- kettle 在javascrip代码组件中使用fireToDB()函数实现自定义数据库查询
- 自学Python4.3-装饰器固定格式
- Codeforces | CF1041F 【Ray in the tube】
- javaweb开发.页面中文乱码问题
- The logback manual #02# Architecture
- ZooKeeper 集群的安装部署
- django-模板初探
- 号称简明实用的django上手教程
- SignalR实现在线聊天室功能
- C# 多维数组
- 【转载】移动开发中的上下左右滑动插件jquery.swipe.js
热门文章
- 【UVA 437】The Tower of Babylon(拓扑排序+DP,做法)
- Word 操作组件介绍 - Spire.Doc
- Flutter入门
- intellij idea 13
- 变量对象、作用域链和This
- 紫书 例题 9-12 UVa 12186 (树形dp)
- qt获取磁盘序列号
- ArcGIS Engine 9.3启动程序报错
- 【LeetCode-面试算法经典-Java实现】【05-Longest Palindromic Substring(最大回文字符串)】
- java.lang.IllegalArgumentException: The observer is null.终于解决方式