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>
源代码:点击打开链接
Android学习笔记进阶十一图片动画播放(AnimationDrawable)的更多相关文章
- Android学习笔记进阶之在图片上涂鸦(能清屏)
Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...
- Android学习笔记进阶16之BitmapShader
<1>简介 具体的看一下博文:Android学习笔记进阶15之Shader渲染 public BitmapShader(Bitmap bitmap,Shader.TileMode ti ...
- Android学习笔记进阶17之LinearGradient
具体的看一下博文:Android学习笔记进阶15之Shader渲染 package xiaosi.BitmapShader; import android.app.Activity; import a ...
- Android学习笔记进阶18 之画图并保存图片到本地
1.首先创建一个Bitmap图片,并指定大小: 2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可: 3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...
- Android学习笔记进阶18之画图并保存图片到本地
1.首先创建一个Bitmap图片,并指定大小: 2.在该图片上创建一个新的画布Canvas,然后在画布上绘制,并保存即可: 3.需要保存的目录File,注意如果写的目录如“/sdcard/so ...
- Android学习笔记(十一)——ListView的使用(下)
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! ListView 这个控件比较复杂, 就是因为它有很多的细节可以优化,下面我们在试试提高它的运行效率:一.提高 ...
- Android 学习笔记进阶十二之裁截图片
package xiaosi.cut; import java.io.File; import android.app.Activity; import android.content.Intent; ...
- Android学习笔记进阶20 之得到图片的缩略图
<1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在A ...
- Android学习笔记进阶20之得到图片的缩略图
<1>简介 之前往往是通过Bitmap.Drawable和Canvas配合完成,需要写一系列繁杂的逻辑去缩小原有图片,从而得到缩略图. 现在我给大家介绍一种比较简单的方法:(网上有) 在A ...
随机推荐
- ArcGIS api for javascript——显示地图属性
描述 本例展示了如哦读取地图和图层的属性和返回信息给用户.本例中的四个按钮允许用户接收地图属性.每个按钮调用不同的函数. ·Get All Map Layers - 这个按钮调用getMapLayer ...
- libvirt 部分API 介绍
感谢朋友支持本博客.欢迎共同探讨交流,因为能力和时间有限,错误之处在所难免,欢迎指正! 假设转载,请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- UI_UISegmentedControl 控件
创建控件 - (void)createSegmentControl { UISegmentedControl *segmentedControl = [[UISegmentedControl allo ...
- Launcher3实现壁纸居中
Launcher3的wallpaper显示是动态的,与Launcher预置桌面数目有关,让壁纸居中,仅仅能确保第一次开机时壁纸居中,后面用户改动桌面数目后,就无法达到了.怎样要在默认桌面数目配置时居中 ...
- AES加密解密在JAVA和ANDROID下互通
<span style="font-family: Arial, Helvetica, sans-serif;">昨天外包安卓的那个人说AES的加解密结果不一样.于是百 ...
- FPGA设计中的电源管理(转载)
过去,FPGA设计者主要关心时序和面积使用率问题.但随着FPGA不断取代ASSP和ASIC器件,设计者们现正期望能够开发低功耗设计,在设计流程早期就能对功耗进行正确估算,以及管理和对与FPGA相关的各 ...
- url与图片
http://restapi.amap.com/v3/staticmap?location=116.481485,39.990464&zoom=10&size=750*300& ...
- hdoj--2955--Robberies(背包好题)
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Google Summer of Code 2017 经验谈
Google Summer of Code (GSoC) 2018 又要开始了. 如果想实现你心中的开源梦想, 用代码让世界变得更美好. 参加GSoC可能是你进入开源的世界最好途径. GSoC是什么 ...
- 在C# 获取当前应用网址
/// <summary> /// 获取当前应用网址 /// </summary> /// <returns></r ...