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
原创粉丝点击