这个例子是一帧一帧的播放20张图片,通过两个按钮控制播放的开始和停止

frame.xml文件

        
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent">
  6. <ImageViewandroid:id="@+id/imageview"
  7. android:layout_width="200px"
  8. android:layout_height="200px"
  9. android:src="@drawable/anim_01"
  10. android:scaleType="fitCenter"
  11. android:layout_centerHorizontal="true"
  12. android:background="#ffffff"/>
  13. <Buttonandroid:id="@+id/start"
  14. android:text="开始动画"
  15. android:layout_below="@id/imageview"
  16. android:layout_width="fill_parent"
  17. android:layout_height="wrap_content"/>
  18. <Buttonandroid:id="@+id/stop"
  19. android:text="结束动画"
  20. android:layout_below="@id/start"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"/>
  23. </RelativeLayout>

其中,ImageView中的android:scaleType属性(ImageView.setScaleType(ImageView.ScaleType))说明:

CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示 FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY 把图片不按比例

        
  1. publicclassFrameActivityextendsActivity{
  2. privateImageViewimageView;
  3. privateButtonstart;
  4. privateButtonstop;
  5. privateAnimationDrawableanimationDrawable;
  6. @Override
  7. publicvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.frame);
  10. imageView=(ImageView)findViewById(R.id.imageview);
  11. start=(Button)findViewById(R.id.start);
  12. start.setOnClickListener(start_listener);
  13. stop=(Button)findViewById(R.id.stop);
  14. stop.setOnClickListener(stop_listener);
  15. animationDrawable=newAnimationDrawable();
  16. //添加每一帧动画
  17. for(inti=1;i<=20;i++){
  18. intid=getResources().getIdentifier(
  19. "anim"+(i>9?"_":"_0")+i,"drawable","com.will.frame");
  20. animationDrawable.addFrame(getResources().getDrawable(id),150);
  21. }
  22. //设置手否重复播放,false为重复
  23. animationDrawable.setOneShot(false);
  24. imageView.setImageDrawable(animationDrawable);
  25. }
  26. //开始播放
  27. Button.OnClickListenerstart_listener=newButton.OnClickListener(){
  28. @Override
  29. publicvoidonClick(Viewarg0){
  30. animationDrawable.start();
  31. }
  32. };
  33. //停止播放
  34. Button.OnClickListenerstop_listener=newButton.OnClickListener(){
  35. @Override
  36. publicvoidonClick(Viewarg0){
  37. animationDrawable.stop();
  38. }
  39. };
  40. }

更多相关文章

  1. Android学习笔记10——Android的调试
  2. Android(安卓)PreferenceActivity简介
  3. drawable(hdpi,ldpi,mdpi)的区别
  4. Android实现一行有好几个TextView 第一个TextView使用省略
  5. Bitmap.Options和LruCache——Android高效加载大图、多图解决方
  6. Picasso源码分析
  7. Android(安卓)背景图片重复 Background repeat
  8. Android获取视频文件某一帧并设置图片
  9. Android(安卓)Toast 部分手机无法显示问题(如小米手机)解决

随机推荐

  1. 高级 Android(安卓)开发工程师-人工智能
  2. 安卓开发入门-与java关系
  3. Android(安卓)UI开发第二十五篇——分享
  4. android Lru图片缓存管理方案
  5. Android上鲜为人知的UI控件介绍和使用
  6. Android(安卓)context 文件模式
  7. Android的性能优化方法
  8. Android(安卓)进程保活,Service进程常驻
  9. Android数据库操作--greenDAO的入门使用
  10. Android(安卓)如何在关于手机界面添加个