【问题标题】:9-patch - Conservation of the height/width ratio of the non-stretchable area when scaling down9-patch - 缩小时保持不可拉伸区域的高/宽比
【发布时间】:2016-12-15 23:08:15
【问题描述】:

我有以下 9 个补丁文件作为卡片视图的背景:

为了将图像缩放到背景,我在 ImageView 上使用了缩放类型“fitXY”:

          <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/background_player_right_elo"
            android:scaleType="fitXY"/>                          

当卡片的尺寸大于图片时效果很好。但是,在我的情况下,图像的高度低于图像的高度(45dp),结果失真:

我可以减小图像的大小,但它会产生像素化图像。 我不能使用标准图像而不是 9-patch,因为卡片的宽度取决于设备的屏幕宽度。

在缩小 9-patch 图像时是否可以保留不可拉伸区域的高/宽比例?

否则,有没有其他方法可以得到想要的结果?

提前致谢!

【问题讨论】:

  • 你可以保持中心区域不拉伸,只拉伸它周围的部分。
  • 中心区域在9-patch文件中没有标记为可拉伸(如果是你的意思)但是卡片的高度低于图像的高度所以它必须被拉伸填充卡片的背景。
  • The central area is not marked as stretchable in the 9-patch file OKthe height of the card is lower than the height of the image 这是不行。然后你必须提供一个 smaller 9 补丁。拉伸只能保证在扩展的意义上起作用,而不是在装置的意义上。
  • 好的,我知道不能保证 9-patch。但是,如果我使用较小的图像,它会被像素化。您认为还有其他方法可以做到这一点吗?我不知道是否可以叠加 2 个 ImageView(1 个带有 9 个背景补丁,1 个带有不是 9 个补丁的绘图)。
  • 好的,谢谢您的建议!

标签: android scale android-cardview nine-patch


【解决方案1】:

根据约束有几种可能的答案:

  1. 如果结果不是太像素化,则减小 9-patch 图像的大小;
  2. 如果您不介意,请增加 CardView 的大小;
  3. 否则,你可以使用两个ImageView:

    • 第一个带有 scaleType "fitXY" 的图片将包含一个具有可拉伸背景的 9-patch 图像;
    • 第二个带有 scaleType“centerInside”,仅包含徽标(不是 9 补丁)和透明背景。

示例:

        <!-- ImageView for the 9-patch background -->
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/background"
            android:scaleType="fitXY"/>

        <!-- ImageView for the non 9-patch logo -->
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/logo"
            android:scaleType="centerInside"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 2018-04-29
    • 2023-01-09
    • 2018-12-04
    相关资源
    最近更新 更多