【问题标题】:Android Bitmap Unwanted Black BorderAndroid 位图不需要的黑色边框
【发布时间】:2013-01-04 11:36:26
【问题描述】:

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

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

这是我的布局 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>

有问题的imageview是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();
        }
    }
}

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

【问题讨论】:

  • 你可以使用 scalyType="fitXY" 但这会拉伸图像,我猜结果会更糟。目标是什么?拉伸图像还是仅将其放入图像视图中?或者只是玩一下 scaleType 看看是否会有适合您的结果。
  • 其实我想用它的实际大小保存图像而不拉伸它或添加不需要的黑色边框

标签: android image bitmap save border


【解决方案1】:

你已经设置了

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

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

【讨论】:

    猜你喜欢
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 1970-01-01
    相关资源
    最近更新 更多