2

实际上我编写了一个使用图像旋转的应用程序。但是当我旋转图像时,它没有显示任何内容。

这是我的代码:

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);
}
4

2 回答 2

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);
于 2012-08-16T20:43:12.383 回答
-1

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

imageView.setRotation(90.0f); 
于 2014-07-07T05:04:11.220 回答