在android中缩放和旋转图像 - 图像不会出现

时间:2014-04-14 17:15:13

标签: android

我是android的新手,在使用相同的比例并使用我的第一个活动旋转代码后,我在第二个活动中出现图像时出现问题。 在第一项活动中,我目前正在从照片库中检索图像。然后我将此图像显示在ImageView中。在我实际显示图像之前,我必须缩放位图然后旋转它。这是有效的 - 我从这个网站获得了大部分代码 - 谢谢!然后我将此图像的详细信息保存到我的SQLite数据库(如sd卡上图像的路径名) 然而,在第二个活动中 - 我然后尝试立即检索图像以在imageview中显示(从它的路径名 - 我保存到SQLite数据库),并再次使用相同的代码来缩放和旋转相同的图像到在这个ImageView中显示,但是这次,图像没有出现.....?如果我只是执行缩放位图的代码 - 图像确实出现,但只有当我尝试执行旋转代码时才会出现图像。如上所述 - 在第一个活动中 - 比例和旋转工作,但对于第二个活动 - 只有比例工作。 这是缩放和旋转的代码

    public static void process (String filePath, ImageView imageView) {

    Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
    int h = 250;
    int w = 250;
    Bitmap scaled = Bitmap.createScaledBitmap(yourSelectedImage, h, w, true);

    Matrix matrix = new Matrix();
    imageView.setScaleType(ScaleType.MATRIX);
    imageView.setImageBitmap(scaled);
    matrix.postRotate(90f, imageView.getDrawable().getBounds().width()/2,
            imageView.getDrawable().getBounds().height()/2);
    imageView.setImageMatrix(matrix);
}

第一项活动的XML:

        <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/result"
        android:layout_width="250dip"
        android:layout_height="250dip"
        android:layout_alignParentTop="true"
        android:layout_marginTop="40dp"
        android:contentDescription="@string/desc1"
        android:scaleType="matrix" />

    <Button
        android:id="@+id/selectButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/textView2"
        android:layout_marginBottom="15dp"
        android:text="Select Photo" />

    <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/selectButton"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="15dp"
        android:layout_marginRight="27dp"
        android:text="Save" />

    <LinearLayout
        android:id="@+id/addbloglinkcontainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/result" >

        <TextView
            android:id="@+id/addbloglinktext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/add_blog_link" />

        <EditText
            android:id="@+id/blogLink"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="10"
            android:ems="10"
            android:inputType="textUri" />
    </LinearLayout>

</RelativeLayout>

第二项活动的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="250dip"
        android:layout_height="250dip"
        android:layout_alignParentTop="true"
        android:layout_marginTop="40dp"
        android:contentDescription="@string/desc1"
        android:onClick="mthdtocall"
        android:scaleType="matrix" />

</RelativeLayout

1 个答案:

答案 0 :(得分:0)

不知道为什么图像不会出现但是......您可以尝试使用LRU缓存在两个活动中重用相同的位图图像。

developer.android.com/reference/android/util/LruCache.html