【问题标题】:Scale ImageButton inside chat bubble在聊天气泡中缩放 ImageButton
【发布时间】:2016-09-07 19:56:17
【问题描述】:

我有一个设置为android:background 的聊天气泡和一个设置为android:src 的图像,我想调整我的图像,如果图像大于我的聊天气泡250dp height and/or width(在下面的示例中是我的身高)然后应该裁剪掉顶部和底部,以便整个图像保留在聊天气泡中。

我不想使用以下内容:

  • scaleType =cropInside |这会使我的图像居中,在左右宽度上留下太多空间。
  • scaleType = fitXY |这会使图像在聊天气泡的高度和宽度中均匀填充,但问题是它会拉伸图像。

我的 XML:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="5dp"
     >
    <ImageButton
        android:id="@+id/ib"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:src="@drawable/img"
        android:background="@drawable/bubble_yellow"
        />

</LinearLayout>

总而言之,我想要与 whatsapp 相同的图像聊天气泡 - 它的作用是通过裁剪顶部和底部(或隐藏)来保持图像(如果是纵向)的比例,并且只显示整个图像一旦通过单击就可以展开。

请看下面我的图片描述:

编辑:

实际图片下方(黄色背景聊天气泡用 9 个补丁完成)。

【问题讨论】:

  • 你是用 9-path 图片作为背景吗?
  • 请附上真实图片示例,以便我们尝试使其工作

标签: android


【解决方案1】:

您可以计算背景图像左侧部分的宽度(以 dp 为单位)并将其添加为左侧填充。背景不会受到填充的影响

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="5dp"
     >
    <ImageButton
        android:id="@+id/ib"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:src="@drawable/img"
        android:background="@drawable/bubble_yellow"
        android:paddingLet="40dp"
        android:paddingStart="40dp"
        android:scaleType="centerCrop"
        />

</LinearLayout>

由于ImageButton 的大小是在 dp 中给出的,因此在 dp 中设置左填充会使其在所有设备中看起来都相同

【讨论】:

    猜你喜欢
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    • 1970-01-01
    相关资源
    最近更新 更多