2016-02-15 75 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"/>