繁体   English   中英

Android-保持纵横比减小imageview的大小

[英]Android -decrease imageview size with keeping aspect ratio

我想降低ImageView的高度与TextView的高度一样,并保持其长宽比。 图像的实际尺寸是:128 * 128

这是我的代码:

   <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>

我的结果是: 在此处输入图片说明

有什么建议吗?

嗨,请使用此代码在运行时调整图像大小。

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;
}

使用RelativeLayout:

android:paddingBottom =“ 3dip”
android:gravity =“ center_horizo​​ntal” android:orientation =“ horizo​​ntal”>

<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"           
    />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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