Android 如何在设置为ImageView背景的可绘制XML动画中保持纵横比

Android 如何在设置为ImageView背景的可绘制XML动画中保持纵横比,android,android-layout,android-animation,Android,Android Layout,Android Animation,我有一个ImageView,我以前用它来显示图像 imageview.setImageResource(R.drawable.my_image); 我在XML布局中将scaleType定义为centerCrop,以保持图像的纵横比 <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/i

我有一个ImageView,我以前用它来显示图像

imageview.setImageResource(R.drawable.my_image);
我在XML布局中将scaleType定义为centerCrop,以保持图像的纵横比

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/image_view"
        android:src="@drawable/my_image"
        android:scaleType="centerCrop" />
my_动画可绘制

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/my_image1" android:duration="150" />
    <item android:drawable="@drawable/my_image2" android:duration="150" />
    <item android:drawable="@drawable/my_image3" android:duration="150" />
</animation-list>

动画效果很好,但是drawable现在设置为背景,我无法更改其scaleType以保持纵横比

有没有办法让动画保持纵横比

imageview.setImageResource((R.drawable.my_animation);
您必须设置ImageResource而不是imageBackground。
缩放类型不适用于收进背景

更多信息:

您必须设置ImageResource而不是imageBackground。
缩放类型不适用于收进背景


更多信息:

感谢您的回复。我尝试使用
imageview.setImageResource((R.drawable.my_animation);
设置动画,问题是我无法启动动画。我尝试使用
animsdraw=(AnimationDrawable)imageview.getBackground();
animsdraw=(AnimationDrawable)imageview.getDrawable()获取对动画的引用
但是它不适用于
animDraw.start();
你可以试试这个或你的动画文件我有一个可绘制的文件“my_animation.xml”这在原始文章中显示。它只是指定帧图像及其持续时间。如果在背景上设置,则此动画效果良好,但如果使用setImageResource设置,则不会运行。我可以尝试使用gif方法,但我认为可以将动画设置为背景,然后重新编程ImageView的布局参数匹配动画纵横比。那么,不同屏幕大小的拉伸不会是问题。感谢您的响应。我尝试使用
imageview.setImageResource((R.drawable.my_animation)设置动画;
问题是我无法启动动画。我尝试使用
animDraw=(AnimationDrawable)imageview.getBackground();
animDraw=(AnimationDrawable)imageview.getDrawable();
获取对动画的引用,但它不适用于
animDraw.start()
你可以试试这个或你的动画文件我有一个可绘制的文件“my_animation.xml”这在原始文章中显示。它只是指定帧图像及其持续时间。如果在背景上设置,则此动画效果良好,但如果使用setImageResource设置,则不会运行。我可以尝试使用gif方法,但我认为可以将动画设置为背景,然后重新编程ImageView的布局参数为了匹配动画的纵横比,那么不同的屏幕大小就不会有拉伸的问题。
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/my_image1" android:duration="150" />
    <item android:drawable="@drawable/my_image2" android:duration="150" />
    <item android:drawable="@drawable/my_image3" android:duration="150" />
</animation-list>
imageview.setImageResource((R.drawable.my_animation);