1

我的图像视图上有这个位图,当我保存位图时,它包括位图周围的黑色边框。边框在那里是因为位图的大小与屏幕大小不同。任何人都可以告诉我为什么以及如何解决它?非常感谢您!

这是我的图片之前和之后的屏幕截图: 图像

这是我的布局 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativelayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal" >

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:visibility="gone" />

  <ImageView
      android:id="@+id/imageView2"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:adjustViewBounds="true"
      android:scaleType="fitCenter" />

</LinearLayout>
<Button
    android:id="@+id/selectPhotoButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:text="Select Photo" />

<Button
    android:id="@+id/editPhotoButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:text="Edit Now" />


 </RelativeLayout>

有问题的图像视图是imageView2

这是我保存图像的方法:

 private void savePhoto() {
    if (editPhotoImg.getDrawable() == null) {
        // Toast.makeText(getApplicationContext(), "No Photo Edited",
        // Toast.LENGTH_SHORT).show();
    } else {
        try {
            String filePath = getOutputMediaFile(RESULT).getPath();
            editPhotoImg.setDrawingCacheEnabled(true);
            Bitmap b = editPhotoImg.getDrawingCache(true);

            b.compress(CompressFormat.JPEG, 100, new FileOutputStream(
                    filePath));


            // Toast.makeText(getApplicationContext(),
            // "Image saved to: "+filePath, Toast.LENGTH_SHORT).show();

            // detectFaces(true);

        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Invalid File       Path",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

再次感谢您的提前帮助!!!

4

1 回答 1

2

你已经设置

<ImageView
      android:id="@+id/imageView2"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      ... />

它将图像视图拉伸到其父视图的边界(这不能保证图像与您的父视图具有相同的大小)。如果您不想要黑色边框,则视图应该有"wrap_content",因此它将自动调整加载图像的大小。

于 2013-01-21T13:14:46.363 回答