简体   繁体   中英

Android - get image inside imageview to fill completely

I have a bitmap which I am reading from a particular location on the device like such and am setting the image to an ImageView

    scaledBitmap = BitmapFactory.decodeFile(selectedImagePath);
    ivImage.setImageBitmap(scaledBitmap);

And thats how I am setting the xml code for that imageview

<ImageView
    android:id="@+id/ivImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:adjustViewBounds="true"
    android:layout_centerHorizontal="true"
    android:scaleType="fitXY" />

Is there a way I can remove the blank space above the image inside the ImageView?

在此处输入图片说明

尝试将android:scaleType="fitXY"更改为android:scaleType="centerCrop"

I had the same problem, and what I remember is removing adjustViewBounds attribute line from code will fix it. Try this and let me know.

scaleType="fitCenter" (default when omitted)

  • will make it as wide as the parent allows and up/down-scale as needed keeping aspect ratio.

scaleType="centerInside"

  • if the intrinsic width of src is smaller than parent width
    will center the image horizontally
  • if the intrinsic width of src is larger than parent width
    will make it as wide as the parent allows and down-scale keeping aspect ratio.

It doesn't matter if you use android:src or ImageView.setImage* methods and the key is probably the adjustViewBounds .

use this

<ImageView
    android:id="@+id/ivImage"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:scaleType="centerCrop" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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