Android学习笔记进阶十一图片动画播放(AnimationDrawable)
大家平时见到的最多的可能就是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>
源代码:点击打开链接
转载于:https://www.cnblogs.com/Free-Thinker/p/6721791.html
Android学习笔记进阶十一图片动画播放(AnimationDrawable)相关推荐
- Android学习笔记26:图片切换控件ImageSwitcher的使用
在Windows操作系统中,要查看多张图片,可以通过使用"Windows照片查看器"在"上一张"和"下一张"之间切换,进行多张图片的浏览. ...
- Android学习笔记之在图片特效
1.涂鸦(能清屏) HandWritingActivity.java [java] view plaincopy package xiaosi.handWriting; import android. ...
- Android学习笔记——用ObjectAnimator平移动画仿微信表情雨的简单实现
最近需要实现类似微信聊天界面输入文字命中关键字出现Emoji 表情雨的动画效果,自己试着实现了一下,主要就是用 ObjectAnimatior 的 平移 动画,用随机函数控制每个Emoji 的 横,纵 ...
- Android学习笔记进阶20之得到图片的缩略图
<1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在A ...
- android给图片加边框,Android学习笔记进阶19之给图片加边框
//设置颜色 public void setColour(int color){ co = color; } //设置边框宽度 public void setBorderWidth(int width ...
- Android学习笔记(十一):Activity-ListView
在手机中经常有列表方式.如果Activity中只有唯一一个List(这也是通常的情况),可以继承ListActivity来实现.我们用两个例子来学习List. List例子一:利用Android自带的 ...
- Android学习笔记进阶十三获得本地全部照片
这是Intent的一个用法. 在ActivityAction里面有一个"ACTION_GET_CONTENT"字符串常量,该常量让用户选择特定类型的数据. intent.setTy ...
- Android学习笔记19-ImageView实现图片适屏与裁剪
在Android中,要将一张图片显示在屏幕上,需要创建一个显示图片的对象,该对象就是ImageView. 1.ImageView常用属性 要对图片进行适屏裁剪操作,首先需要了解ImageView的常用 ...
- Android学习笔记(十一)——将Fragment添加到Activity中以及参数传递
将Fragment添加到Activity中 Fragment的布局文件 <LinearLayout xmlns:android="http://schemas.android.com/ ...
最新文章
- DWS和各异构数据库的差异对比
- 利用CSS定位背景图片
- Server-Side UI Automation Provider - WinForm Sample
- Webkit for android
- LeetCode【3--无重复的最长字串】 LeetCode【4--有序数组中的中位数】
- Selenium 自动化测试基础知识
- joblib多线程、多进程学习案例(一)——一步步写多进程任务
- Swift教程之基本操作符
- iOS 使用NSUserdefault 保存自定义的 对象
- android 通讯录恢复,通讯录恢复
- excel甘特图制作项目进度管理表
- Linux 运行.exe程序
- YOLO系列梳理(九)初尝新鲜出炉的YOLOv6
- Android实现随意拖动View效果
- 使用H5开发移动端app
- steam版拳皇14无法正常启动
- android水电工具softonic,Softonic
- esp8266 NVS_FLASH 应用指南
- 解决国内手机安装Google Play打开闪退问题
- Java8 Lambda表达式(三)Lambda表达式与Stream API
热门文章
- java 外部类似_[求指点] 如何用java 实现类似linux中管道调用外部程序的功能
- java 对象内存布局_Java--对象内存布局
- mysql数据库验证登陆不上_MySQL数据库连接不上、密码修改问题
- html 右边框变短,HTML / CSS:使边框右侧高度动态化
- 学校计算机机房好处,浅谈学校计算机机房维护
- π是无理数证明定积分_证明圆周率是无理数很容易?人类花了2000年!
- app获取个人信息是否合法_【关注】如何界定App违法违规收集个人信息?认定方法来了!...
- usb禁止重定向_远程桌面中的USB重定向技术解析(功能篇)
- datagrid表头与内容不对应_斜线表头竟然如此简单,花上3分钟学习,从此告别低颜值报表...
- k8s mysql operator_将 MySQL 通过 presslabs/mysql-operator 部署到 k8s 内部