afn February 2016

Android -decrease imageview size with keeping aspect ratio

I want to decrease ImageView's height as much as TextView's height and keep it's aspect ratio. real size of image is : 128*128

Here is my code:

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#b6006a"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

    <ImageView
        android:id="@+id/ImageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxHeight="10dp"
        android:minHeight="40dp" 
        android:maxWidth="40dp"             
        android:layout_weight="1"
        android:adjustViewBounds="true"            
        android:padding="5dip"
        android:scaleType="fitXY"
        android:src="@drawable/icon" />

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#b6006a"            
        android:padding="10dip"
        android:text="New Message"
        android:textColor="#fff"
        android:textStyle="bold"
        android:textSize="20dp" />

    </LinearLayout>

And my result is : enter image description here

Any suggestion?

Answers


Mandeep February 2016

Hi use this code to resize your image at runtime.

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both height and width larger than the requested height and width.

        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}


afn February 2016

Use RelativeLayout :

android:paddingBottom="3dip"
android:gravity="center_horizontal" android:orientation="horizontal">

<ImageView
    android:id="@+id/ImageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"            
    android:maxWidth="40dp" 
    android:paddingTop="2dip"           
    android:adjustViewBounds="true"            
    android:scaleType="fitXY"
    android:src="@drawable/icon" />

<TextView
    android:id="@+id/TextView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#b6006a"            
    android:padding="10dip"         
    android:layout_marginLeft="30dp"
    android:text="New Message"
    android:textColor="#fff"
    android:textStyle="bold"
    android:textSize="20dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@id/ImageView1"           
    />

Post Status

Asked in February 2016
Viewed 1,952 times
Voted 5
Answered 2 times

Search




Leave an answer