2013-01-21 23 views
0

我在我的imageview上有這個位圖,當我保存位圖時,它包含位圖周圍的黑色邊框。邊框在那裏,因爲位圖的大小與屏幕大小不同。任何人都可以告訴我爲什麼以及如何解決它?提前感謝你!Android位圖不需要的黑色邊框

下面是截圖我的形象的前後: img

這裏是我的佈局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(); 
     } 
    } 
} 

的幫助再次感謝提前!

+0

你可以使用scalyType =「fitXY」,但這會拉伸圖像,我估計結果會​​變差。目標是什麼?拉伸圖像或只是爲了適應它在imageview?或者只是使用scaleType來查看是否有適合您的結果。 – mihail

+0

其實我想保存圖片的實際尺寸而不拉伸它或添加不需要的黑色邊框 – user1950532

回答

1

您已設置

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

綿延圖像視圖其父視圖的邊界(不保證該圖像將具有的尺寸與父視圖相同)。如果您不想黑色邊框,則該視圖應具有"wrap_content",因此它會根據加載的圖像自動調整大小。