2012-08-16 61 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);