2012-08-16 145 views
2

其实我编程使用图像旋转的应用程序。但是当我旋转图像时,它不显示任何东西。在Android中旋转后未显示ImageView

这是我的代码:

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="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> 

活动文件:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.rotate_test_layout); 
    ImageView imageView = (ImageView)findViewById(R.id.android); 
    imageView.setScaleType(ScaleType.MATRIX); 
    Matrix matrix = new Matrix(); 
    matrix.postRotate(90); 
    imageView.setImageMatrix(matrix); 
} 

回答

1

您正在这里一个错误:

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); 
+0

这种方法会在大的位图上导致OOM。有一篇文章讨论这个解决方案的这个问题。 http://stackoverflow.com/a/10104318/2123400 但对我来说,这个解决方案不起作用,因为你的答案告诉你为什么。我很困惑为什么很多人可以在不创建新位图的情况下旋转ImageView的图像。 – Eftekhari 2016-05-30 15:22:35

-1

您可以在不使用矩阵的情况下旋转ImageView。 测试它。

imageView.setRotation(90.0f);