繁体   English   中英

如何在Android中使图像适合整个屏幕?

[英]How to make image fit the whole screen in android?

我试图使图像拍摄以适合整个屏幕尺寸。 我的活动布局XML如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >

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

我的代码来显示图像:

private void showImage(String imageName){
    String directory = "/sdcard/DCIM/cat/" + imageName;
    File  imageFile = new File(directory);
    if(imageFile.exists()){
        Bitmap imageBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

        //rotating the bitmap by 90 degree clockwise. Assume the bitmap is 270 degree clockwise.
        Matrix mtx = new Matrix();
        mtx.preRotate(90);

        int w = imageBitmap.getWidth();
        int h = imageBitmap.getHeight();

        imageBitmap = Bitmap.createBitmap(imageBitmap,0,0,w,h,mtx,false);
        imageBitmap = imageBitmap.copy(Bitmap.Config.ARGB_8888,true);
        ImageView imageView = (ImageView)findViewById(R.id.imageView1);
        imageView.setImageBitmap(imageBitmap);

    }

屏幕上的输出如下: 活动的屏幕截图

大家都可以看到,图像的左右两侧都有空白。 有什么方法可以使图像完美地适合屏幕?

使用android:scaleType属性填充图片:

android:scaleType="fitXY"

要么

imageView.setScaleType(ImageView.ScaleType.FIT_XY)

android:scaleType="centerCrop"<ImageView>声明。 它将充满整个空间并切断多余的部分。

使用setScaleType属性可以做到这一点。

像这样的东西:

imgView.setScaleType(ImageView.ScaleType.FIT_CENTER); // OR FIT_XY

使用任一

android:scaleType = "fitCenter" //This will preserve the aspect ratio of image

要么

android:scaleType="fitXY" //This won't preserve the aspect ratio of image

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM