【问题标题】:android 9-patch graphic doesn't scale in image viewandroid 9-patch 图形在图像视图中不缩放
【发布时间】:2011-07-11 16:51:36
【问题描述】:

我有

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView android:layout_height="wrap_content"
        android:layout_width="wrap_content" android:id="@+id/ImageView01"
        android:src="@drawable/phone80"></ImageView>
    <ImageView android:id="@+id/ImageView02"
        android:layout_height="wrap_content" android:src="@drawable/underline"
        android:layout_width="fill_parent" android:layout_gravity="fill_vertical|fill_horizontal"></ImageView>
</LinearLayout>

第一张图片是固定大小的图片,根本不应该缩放。其右侧的图像应水平缩放到最大空间。源指向一个有效的 .9.png 文件。不幸的是,它总是只显示其原始大小。我必须设置什么属性? ImageView 是否是正确的对象?

谢谢, A.

【问题讨论】:

    标签: android imageview nine-patch


    【解决方案1】:

    尝试使用 setImageResource(9patchResourceID) 代替 setImageBitmap 或 setImageBackground,它可以工作 :)

    【讨论】:

      【解决方案2】:

      还要检查以确保侧面的比例线不会一直延伸到图像的角落。我知道那里有黑色像素并没有什么意义,但我认为 Android 会忽略它们,但它实际上阻止了缩放工作。它还在输出窗口中为您提供“错误:9-patch image xxx.9.png malformed”消息,但您可能没有注意到这一点。一旦我取出角落中的像素,它就开始为我工作了。

      【讨论】:

        【解决方案3】:

        只需将android:scaleType="fitXY" 添加到您的ImageView。例如对于 myimage.9.png:

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/myimage"
            android:scaleType="fitXY" />
        

        【讨论】:

        • 这是正确的。如果没有 fitXY,9 补丁将无法使用,图像将正常拉伸。
        • 绝对是正确的答案,因为当 ImageView 位于 RelativeLayout 内部并且相对于其他视图时,不会考虑 android:background。
        • 只是对这个答案的补充。在我将补丁点从左角移到中间之前,比例仍然是错误的
        【解决方案4】:

        请改用android:background。如果它只是一个下划线,您可以使用View 而不是ImageView。至于填充屏幕的其余部分,您可能需要使用TableLayout

        另外,请确保您的 9-patch 是正确的。 1px 边必须是黑色的。 #000

        【讨论】:

        • 刚投了反对票...希望得到一些反馈,以便其他人受益。
        • 将图像添加到背景而不是 src 意味着另一层内容会降低性能。不是我的反对意见,但我也希望能够使用填充图像并在加载时将其与最终图像交换;没有堆叠两张图片。
        • 对与错。您可以将背景添加到根线性布局,然后您可以对其进行填充或格式化,这也意味着内容将是第二层并且更易于维护......两者都很棒,但取决于用例。感谢您的评论!
        • 虽然这个答案有一些好处,但@almaz_from_kazan 的答案是这个问题的更好答案。
        • 下面的答案应该被认为是正确的,原因在下面的答案的评论中解释。
        猜你喜欢
        • 1970-01-01
        • 2012-05-24
        • 1970-01-01
        • 2012-01-31
        • 1970-01-01
        • 1970-01-01
        • 2014-12-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多