2016-02-15 145 views
0

我有一個應用程序,您可以在其中拍照,然後將其顯示爲相對佈局的背景。但是不知何故,圖像會以一種奇怪的方式被拉伸,也許被強制爲橫向模式(?)。我不知道如何以正確的方式旋轉它,這是我的代碼,希望有人能幫助。Android位圖拉伸?

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 
     setContentView(R.layout.activity_main2); 


     Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath(); 
     String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES) + "/picFolder/1.jpg"; 
     File image = new File(filePath); 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions); 

     Drawable drawable = new BitmapDrawable(bitmap); 
     RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout); 
     layout1.setBackgroundDrawable(drawable); 
    } 

This is how I take my picture

And this is how I get the result in my layout background, the image is streched, and not look like my taken image.

我的第一張圖片是原來的一個,第二個是streched,我在我的佈局得到。

如何解決這個問題?我做錯了什麼?提前致謝!

回答

2

RelativeLayout具有與圖像不同的寬高比。將圖像設置爲視圖的背景並不能提供任何真正的縮放選項,只是將圖像拉伸以適合視圖。相反,你應該把內部的ImageViewRelativeLayout(先聲明它,所以它是所有其他觀點的背後),並設置視圖(背景)的圖像內容。確保使用合適的scaleType;在這種情況下,我選擇了centerCrop,這將縮放圖像,使得寬度和高度至少與ImageView的對應尺寸一樣大。

<RelativeLayout ... > 

    <ImageView 
     android:id="@+id/background_image" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="centerCrop" /> 

    <!-- other views --> 

</RelativeLayout> 

ImageView backgroundImageView = (ImageView) findViewById(R.id.background_image); 
... 
// after taking a picture 
backgroundImageView.setImageBitmap(bitmap); 
1

我不知道,如果你可以,但如果你的圖像移動到圖像層上可以只設置規模類型,以適應中心佈局的頂部,也不會伸展。 image.setScaleType(ScaleType.FIT_CENTER); 或

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:scaleType="center"/>