AnimationDrawable加载SD卡中图片实现帧动画的坑
来源:互联网 发布:金山新城成交数据 编辑:程序博客网 时间:2024/06/03 12:16
前言,项目中遇到一个需求,首先将包含帧动画的zip包从服务器中下载,然后解压到SD卡,然后将这些图片作为帧动画的资源图片。然后就想当然的想到用AnimationDrawable来实现。 main布局的xml代码如下:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/cube_tip_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30px" android:background="#666666" android:textColor="#ffffff" android:layout_x="1531px" android:layout_y="764px" android:text="将2块小积木拼在一起" /> <ImageView android:id="@+id/cube_tip_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="1441px" android:layout_y="864px" android:scaleType="fitXY" android:src="@drawable/merge1" android:background="#ff0000" /></AbsoluteLayout>
Activity代码如下:
private ImageView mImageView; private String path ; private BitmapUtil mBitmapUtil; private String sdcardPath = Environment.getExternalStorageDirectory().getAbsolutePath(); private AnimationDrawable anim; private ImageView mCubeImg; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.git_main_layout); mCubeImg = (ImageView)findViewById(R.id.cube_tip_img); new Handler().postDelayed(new Runnable() { @Override public void run() { doStartAnimation(1); } }, 5000); } private Bitmap getImgFromLocal(String imagePath) { String filePath = sdcardPath + "/cube_img/" + imagePath; Log.e("xxx", "filePath = " + filePath); Bitmap bm = null; File file = new File(filePath); if (file.exists()) { bm = BitmapFactory.decodeFile(filePath); return bm; } return bm; } private void doStartAnimation(int type) { Log.e("ccccc", "x = "+mCubeImg.getX()+", y = "+mCubeImg.getY()); anim = new AnimationDrawable(); String imgName = ""; if(type == 0){ imgName = "shake"; }else{ imgName = "merge"; } Bitmap bm = null; for (int i = 1; i < 15; i++) { bm = getImgFromLocal(imgName+ i + ".png"); anim.addFrame(new BitmapDrawable(bm), 200); } anim.setOneShot(false); mCubeImg.setImageDrawable(anim); anim.start(); Log.e("ccccc", ".... x = "+mCubeImg.getX()+", y = "+mCubeImg.getY()); }
代码很简单,就是5s后开始帧动画,结果发现5s后动画是开始执行了,但是动画图片的大小发生了变化。然后在xml中将该图片的大小写死才能解决,看了源码也没看出啥情况,记下来,以免再次踩坑。
修改后ImageView的xml如下:
<ImageView android:id="@+id/cube_tip_img" android:layout_width="466px" android:layout_height="216px" android:layout_x="1441px" android:layout_y="864px" android:scaleType="fitXY" android:src="@drawable/merge1" android:background="#ff0000" />
1 0
- AnimationDrawable加载SD卡中图片实现帧动画的坑
- AnimationDrawable 实现正在加载的旋转动画
- android中通过AnimationDrawable实现动画背景图片
- Android 编程下 AnimationDrawable (动画)的实现
- 动画 AnimationDrawable 帧动画
- 通过AnimationDrawable实现android中的帧动画
- AnimationDrawable动画实现
- 在ImageView中加载SD卡上的图片资源
- Android中使用多张图片组成动画 AnimationDrawable
- Android中使用多张图片组成动画 AnimationDrawable
- 帧动画以及AnimationDrawable的使用
- 图片动画播放(AnimationDrawable)
- 逐帧动画 AnimationDrawable
- Android动画 - AnimationDrawable帧动画
- AnimationDrawable使用实现跳跃动画
- android 加载sd卡的图片
- Android应用通过AnimationDrawable实现View的动画效果
- CSS3实现的图片加载动画效果
- VS2013+GLFW+GLEW 配置OpenGL开发环境
- android studio快捷键 备份
- MFC使用SetFont设置CStatic不生效的问题
- Struts2实现文件上传和下载
- struts2 namespace
- AnimationDrawable加载SD卡中图片实现帧动画的坑
- static 静态变量和静态代码块的执行顺序
- iOS开发--passport
- 【wireshark】开发环境搭建
- iOS拨打电话
- JS缓冲运动函数
- C5-2 数老鼠
- LeetCode 492. Construct the Rectangle
- sping 乱码