【发布时间】:2017-08-09 13:15:30
【问题描述】:
我有一个以前用来显示图像的 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" />
现在我需要在 ImageView 中使用 drawable animation 并将其设置为 imageView 的背景。
布局文件:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/image_view"
android:src="@android:color/transparent"
android:scaleType="centerCrop" />
Java:
AnimationDrawable animDraw;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageview = (ImageView) findViewById(R.id.image_view);
imageview.setBackgroundResource(R.drawable.my_animation);
animDraw = (AnimationDrawable) imageview.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
animDraw .start();
return true;
}
return super.onTouchEvent(event);
}
my_animation 可绘制
<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>
动画效果很好,但可绘制对象现在设置为背景,我无法更改它的 scaleType 以保持纵横比。
有没有办法让动画保持纵横比?
【问题讨论】:
标签: android android-layout android-animation