2

我有一个列表视图。每行是一个图像和文本。我想缩放图像,使其适合行高,即

  • 图像不应导致行高更大
  • 图像高度应与行高相同
  • 应计算图像宽度,以保持原始宽度高度比

我想出的所有解决方案似乎都太复杂了,我认为这是一个相对常见的要求,所以我想检查一下我是否遗漏了什么——你知道如何以更简单的方式实现这一点吗?

以下是我考虑的解决方案。

解决方案1:

ListView 项目的嵌套视图并设置 android:layout_height="match_parent"。图像已正确调整大小,但 ImageView 占用宽度,就好像它没有调整大小一样。(见下图,我加了黑色背景,看看ImageView占用了多少空间。)

在此处输入图像描述

<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">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView android:id="@+id/Image"
            android:layout_height="match_parent"
            android:layout_width="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitStart"

            android:background="#000000"
            android:padding="1dp"/>
        <TextView
            android:id="@+id/Text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/Image" />
    </LinearLayout>
</RelativeLayout>

解决方案 2: 使用

ViewTreeObserver 观察者 = MyTextView.getViewTreeObserver() observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

public boolean onPreDraw(){
    observer.removeOnPreDrawListener(this);
    // Finding out height of TextView and then calculating width and height of image and setting size of ImageView. Of course, I could also get observer from ImageView and then just set width as height is correct
}

}

这似乎太复杂了。

解决方案3:

使用,例如,Paint.FontMetrics 来计算高度,但我还需要找出使用的字体(例如,系统一......) ListView 可能有一些填充等,所以有很多东西要检索。

请问还有其他更简单的解决方案吗?

编辑 - 澄清

图像具有最大值。大小,如果达到,将停止 ImageView 大小的任何进一步增加。

4

2 回答 2

0

由于您将父线性布局高度定义为换行内容,因此它将采用其内容的大小,并且可能会根据其子元素尺寸而有所不同。

尝试在行布局中定义图像的确切大小如果您可以将其宽度和高度定义为 40dp 或 48dp,您可能能够实现您的目标。

如果所有元素都依赖于彼此的大小,您可能会看到变化。尝试在特定的dp中定义头像/图像大小。

我试图调整你的布局并得到了一些结果。查看以下代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp">
    <ImageView
        android:id="@+id/earthquake_magnitude"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:background="@android:color/black"
        android:fontFamily="sans-serif-medium"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_weight="1"
        android:text="CHROME" />
</LinearLayout>
于 2017-12-18T22:59:15.583 回答
0

如果您想根据宽度和高度设置 ImageView 的宽度和高度,您始终可以通过TextView编程方式获取宽度和高度,然后实现您自己的算法来相应地缩放 ImageView。

要从 textView 中获取宽度和高度,我们称之为它item_textView,它相当简单:

TextView item_textView = findViewById(R.id.item_textView);
int text_width = item_textView.getWidth();
int text_height  = item_textView.getHeight();

此时,您可以执行任何需要执行的数学运算,以将 imageView 缩小到正确的大小。设置 imageView 的大小:

my_imageView.getLayoutParams().width = text_width/2;
my_imageView.getLayoutParams().height = text_height/2;
于 2017-12-18T20:03:56.620 回答