Java Android中旋转后未显示ImageView
实际上,我正在编写一个使用图像旋转的应用程序。但当我旋转图像时,它不会显示任何内容 这是我的代码: XML文件:Java Android中旋转后未显示ImageView,java,android,Java,Android,实际上,我正在编写一个使用图像旋转的应用程序。但当我旋转图像时,它不会显示任何内容 这是我的代码: XML文件: <?xml version="1.0" encoding="utf-8" ?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rotate_test_layout" android:layout_width=
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rotate_test_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/android"
android:src="@drawable/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="matrix"
android:adjustViewBounds="true"
android:contentDescription="@string/android_description" >
</ImageView>
</LinearLayout>
您在这里犯了一个错误:
imageView.setImageMatrix(matrix);
不必这样做,您必须将该矩阵应用于现有位图,以获得新位图,然后将其分配给ImageView
Drawable drawable = getResources().getDrawables(R.drawable.android);
Bitmap existingBitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap rotated = Bitmap.createBitmap(existingBitmap, 0, 0, existingBitmap.getWidth(), existingBitmap.getHeight(), matrix, true);
imageView.setImageBitmap(rotated);
您在这里犯了一个错误:
imageView.setImageMatrix(matrix);
不必这样做,您必须将该矩阵应用于现有位图,以获得新位图,然后将其分配给ImageView
Drawable drawable = getResources().getDrawables(R.drawable.android);
Bitmap existingBitmap = ((BitmapDrawable) drawable).getBitmap();
Bitmap rotated = Bitmap.createBitmap(existingBitmap, 0, 0, existingBitmap.getWidth(), existingBitmap.getHeight(), matrix, true);
imageView.setImageBitmap(rotated);
可以在不使用矩阵的情况下旋转ImageView。 测试一下
imageView.setRotation(90.0f);
可以在不使用矩阵的情况下旋转ImageView。 测试一下
imageView.setRotation(90.0f);
此方法将导致大位图上出现OOM。有一篇帖子用这个解决方案讨论这个问题。但对我来说,这个解决方案不起作用,因为你的答案说明了原因。我不明白为什么很多人可以在不创建新位图的情况下旋转ImageView的图像。这种方法会在大位图上造成OOM。有一篇帖子用这个解决方案讨论这个问题。但对我来说,这个解决方案不起作用,因为你的答案说明了原因。我很困惑为什么很多人可以旋转ImageView的图像而不创建新的位图。