简体   繁体   中英

How to resize image appropriately and set its height and width in android

I know this may strike out as a dumb question but i bet some of you had this problem before.From the screenshot,i am attempting to set an image in an activity.However my image brings up these spaces i dont know how to get rid of them.I mean is there a way,either in xml or to programmatically resize image to fit the height and width?In short,is there a way i can get rid of these spaces above and below? 在此处输入图片说明

My 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="com.snappy.stevekamau.cosmeticsapp.Categories.Lips"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include android:id="@+id/app_bar" layout="@layout/app_bar"></include> <ImageView android:id="@+id/imageTop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/white_rectangle" android:src="@drawable/img4" /> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout> 

Yes, it can be done by a single line of code. Here's the trick :)

android:adjustViewBounds="true"

Add this in ImageView .

<ImageView
        android:id="@+id/imageTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/white_rectangle"
        android:src="@drawable/img4" />

to make the bitmap fill the width and height you can add android:scaleType="fitXY" to your ImageView. This way your bitmap will be scaled in x and y independently, making your bitmap fill entirely the ImageView

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