Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning an image inside an ImageView with max height and max width set

I have an ImageView with max height and max width both set to 100. The figure below is clearly not a square, but you can use your imagination ;)

Figure 1:

╔══════════════════════════════════════════════╗
║ ImageView    ╔══════════════╗                ║
║              ║              ║                ║
║              ║ Actual image ║                ║
║              ║              ║                ║
║              ║              ║                ║
║              ║              ║                ║
║              ╚══════════════╝                ║
╚══════════════════════════════════════════════╝

Anyway, If I try to set a BitMap to the ImageView that does not have a ratio of 1:1, the image is positioned like pictured in Figure 1. What I want is for the picture to be placed to the left inside the ImageView like pictured in Figure 2 below.

Figure 2:

╔══════════════════════════════════════════════╗
║══════════════╗                               ║
║              ║                               ║
║ Actual image ║                               ║
║              ║                               ║
║              ║                               ║
║              ║                               ║
║══════════════╝                               ║
╚══════════════════════════════════════════════╝

You can see my ImageView in XML below. maxHeight, maxWidth and adjustViewBounds are set during runtime.

<ImageView android:id="@+id/someImage"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textName"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:paddingRight="4dp"
    />

This is in a RelativeLayout if it makes any difference.

like image 940
wtf Avatar asked May 04 '10 01:05

wtf


People also ask

How do you load an image from a file and set on an ImageView?

If you're working with an Android application, this source code works as a way to load an image from a file: Bitmap bitmap = BitmapFactory. decodeFile(pathToPicture);

Which attribute is used to set an image in ImageView?

An ImageView control is used to display images in Android applications. An image can be displayed by assigning it to the ImageView control and including the android:src attribute in the XML definition of the control. Images can also be dynamically assigned to the ImageView control through Java code.

How can I set image width and height in android?

If you want to just fit the image in image view you can use" wrap content" in height and width property with scale-type but if you want to set manually you have to use LayoutParams. Layoutparams is efficient for setting the layout height and width programmatically.


1 Answers

try this

<ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/home_icon" 
     android:scaleType="fitStart"/>

It just helps for me.

like image 113
ihrupin Avatar answered Oct 18 '22 13:10

ihrupin