【问题标题】:How to make image fit the whole screen in android?如何使图像适合android中的整个屏幕?
【发布时间】:2015-01-27 23:00:42
【问题描述】:

我试图使图像适合整个屏幕尺寸。我的活动布局 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);

    }

屏幕上的输出如下:

大家可能会观察到,图像的左右两侧都有空格。有什么方法可以让图片完美贴合屏幕?

【问题讨论】:

  • 尝试为 ImageView 使用 android:scaleType="fitXY" 属性。
  • 谢谢!!!解决了我的问题。 :)

标签: android image layout


【解决方案1】:

使用setScaleType 属性可以做到。

类似这样的:

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

【讨论】:

    【解决方案2】:

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

    android:scaleType="fitXY"
    

    imageView.setScaleType(ImageView.ScaleType.FIT_XY)
    

    【讨论】:

      【解决方案3】:

      android:scaleType="centerCrop" 添加到&lt;ImageView&gt; 声明中。它会填满整个空间并切断多余的部分。

      【讨论】:

      • 其实是centerCrop,不是cropCenter,我忘记了这个选项——谢谢!
      【解决方案4】:

      使用任一

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

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

      【讨论】:

        猜你喜欢
        • 2020-05-21
        • 1970-01-01
        • 2014-12-27
        • 1970-01-01
        • 1970-01-01
        • 2016-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多