【问题标题】:Android -decrease imageview size with keeping aspect ratioAndroid - 在保持纵横比的情况下减小图像视图大小
【发布时间】:2016-05-17 20:36:32
【问题描述】:

我想减少 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>

我的结果是:

有什么建议吗?

【问题讨论】:

  • 使用RelativeLayout并将顶部和底部与ImageView对齐
  • 将drawable设置为TextView背景。还要为图像的背景颜色区域创建一个 9-patch!

标签: java android android-imageview image-scaling


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    使用相对布局:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-21
      • 2010-12-03
      • 2013-05-12
      • 1970-01-01
      • 2013-06-26
      • 2012-04-15
      相关资源
      最近更新 更多