AnimationDrawable缩小图像以进行动画处理

时间:2013-09-27 18:39:20

标签: android imageview animationdrawable

我想动画四个图像连续显示。在布局XML文件中,我正在创建一个ImageView:

<ImageView
            android:id="@+id/exercisesAnimated"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:layout_gravity="center_horizontal"/>

在我的代码中,我然后通过

创建AnimationDrawable
private void startAnimation() throws IOException {
    AnimationDrawable animation = new AnimationDrawable();
    String path = "Graphics/" + exercise.getName() + "/";
    InputStream is = getAssets().open(path + "1.jpg");
    animation.addFrame(Drawable.createFromStream(is, null), 800);
    is = getAssets().open(path + "2.jpg");
    animation.addFrame(Drawable.createFromStream(is, null), 800);
    is = getAssets().open(path + "3.jpg");
    animation.addFrame(Drawable.createFromStream(is, null), 800);
    is = getAssets().open(path + "4.jpg");
    animation.addFrame(Drawable.createFromStream(is, null), 800);
    animation.setOneShot(false);

    ImageView imageAnim = (ImageView) findViewById(R.id.exercisesAnimated);
    imageAnim.setBackgroundDrawable(animation);

    animation.start();
}

所以我的问题是动画图像变得非常小,就像图像原始大小的1/3。它们的大小为460x250,我希望它们以这个尺寸显示。

如果我只是将android:src="@drawable/oneOfTheImages"放到ImageView上而不做动画,则图像会以正确的大小显示。

怎么了?

1 个答案:

答案 0 :(得分:2)

尝试将你的jpg文件放在res / drawable-nodpi文件夹中,并在startAnimation方法中将它们拉成Drawables,如下所示:

getResources().getDrawable(R.drawable.1) // this will pull the 1.jpg file and return a Drawable