gpt4 book ai didi

android - 如何防止 android 图层可绘制形状(例如圆形)缩放

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:45:35 24 4
gpt4 key购买 nike

我尝试使用 XML layer-list drawable 创建一个简单的插图。
我有两种形状,一个圆形和一个矩形
我想要一个不缩放的圆。
以下是布局:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ImageView
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shape5"
android:layout_gravity="center"
android:scaleType="center"
/>
</LinearLayout>

这是 shape5.xml 可绘制对象

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="0dp" android:left="0dp">
<shape android:shape="rectangle" android:scaleType="center">
<stroke android:width="2dp" android:color="@android:color/holo_red_light" />
<size android:width="200dp" android:height="100dp"/>
</shape>
</item>

<item android:top="10dp" android:left="10dp">
<shape android:shape="oval" android:gravity="center" android:scaleType="center" >
<stroke android:width="2dp" android:color="@android:color/holo_blue_light" />
<corners android:radius="10dp" />
<size android:width="20dp" android:height="20dp"/>

</shape>
</item>
</layer-list>

生成的绘图如下所示:

screenshot of the rendered activity

很明显项目 android:top 完成了工作,但没有什么能阻止形状缩放。 size width 不起作用,android:scaleType="center"也不起作用。

我快速查看了 LayerDrawable.java 的实现。似乎在幕后,插图是根据项目属性创建的。所以我想通过计算这些我可以达到我想要的结果。那是唯一的方法吗?

更新:

根据这个answer ,我知道操纵项目的 android:top、android:left 等,我可以创建一个虚拟插图,它将根据我的需要缩放我的“形状”。我检查了这个,它是正确的。所以这是一种解决方法。然而,我对它的反直觉感到非常失望。我仍然希望有人能指出一种禁用缩放的简单方法。

更新:根据 this Google Android documentation page ,设置 ImageView scaleType 应该可以防止缩放。显然在我的情况下它没有用。有些东西不见了。

最佳答案

像这样给你的圆圈绘制相同的高度和宽度:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid
android:color="@color/grey" />
<size
android:width="16dp"
android:height="16dp"/>
</shape>

你输入的高度和宽度的值并不重要,重要的是高度 = 宽度。将它们视为纵横比,您想要一个完美的圆的比例,即 1:1。

您不必担心确切值的原因是可绘制对象将通过 scaleType 属性进行缩放。

将可绘制对象插入 ImageView 后,您可以设置 scaleType 来缩放它:

scaleType = fitCenter -> Drawable 将与 ImageView 大小相同,但会保持一个完美的圆!scaleType = centerInside -> Drawable 将是您在参数中指定的高度和宽度等等……

关于android - 如何防止 android 图层可绘制形状(例如圆形)缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22768011/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com