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

时间:2022-11-21 12:10:42

I try to create a simple illustration using XML layer-list drawable.
I have two shapes, a circle and a rectangle
I want a circle not to scale.
The following is the layout:

我尝试使用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>

And this is the shape5.xml drawable

这是shape5.xml drawable

<?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>

The resulting drawing looks like this:

生成的图形如下所示:

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

it is clear that item android:top does the work, but nothing prevents shape from scaling. size width does not work, android:scaleType="center" does not work either.

很明显,项目android:top可以完成工作,但没有什么能阻止形状缩放。大小宽度不起作用,android:scaleType =“center”也不起作用。

I looked quickly at LayerDrawable.java implementation. Seems like behind the scenes an inset is created from item attributes. So I guess by calculating those I can achieve the result, I want. Is that the only way?

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

Update:

As per this answer, I knew that manipulating item's android:top, android:left etc, I can create a virtual inset that will scale my "shapes" as I want them. I checked this, and it is correct. So it is a workaround. I however am sorely disappointed with how counter-intuitive it is. I still hope someone can point me to a simple way to disable scaling.

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

Update: As per this Google Android documentation page, setting ImageView scaleType should have prevented scaling. Apparently in my case it did not work. Something is missing.

更新:根据此Google Android文档页面,设置ImageView scaleType应该已禁止缩放。显然在我的情况下它没有用。缺了点什么。

2 个解决方案

#1


5  

Give your circle drawable the same height and width like so:

给你的圆圈绘制相同的高度和宽度,如下所示:

<?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>

The values you put as height and width do not matter too much, the important thing is that height = width. Think of them as aspect ratio and you want a ratio for a perfect circle, which is 1:1.

您放置的高度和宽度值并不重要,重要的是高度=宽度。将它们视为纵横比,您需要一个完美圆的比率,即1:1。

The reason you don't have to worry about the exact values is because the drawable will scale via the scaleType attribute.

您不必担心确切值的原因是因为drawable将通过scaleType属性进行缩放。

Once you have inserted the drawable into your ImageView you can set the scaleType to scale it:

将drawable插入ImageView后,可以设置scaleType以缩放它:

scaleType = fitCenter -> Drawable will be the same size as the ImageView but will stay a perfect circle! scaleType = centerInside -> Drawable will be height and width you specified in the parameter etc ...

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

#2


-2  

just put android:gravity="center" into item tag

只需将android:gravity =“center”放入item标签即可

#1


5  

Give your circle drawable the same height and width like so:

给你的圆圈绘制相同的高度和宽度,如下所示:

<?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>

The values you put as height and width do not matter too much, the important thing is that height = width. Think of them as aspect ratio and you want a ratio for a perfect circle, which is 1:1.

您放置的高度和宽度值并不重要,重要的是高度=宽度。将它们视为纵横比,您需要一个完美圆的比率,即1:1。

The reason you don't have to worry about the exact values is because the drawable will scale via the scaleType attribute.

您不必担心确切值的原因是因为drawable将通过scaleType属性进行缩放。

Once you have inserted the drawable into your ImageView you can set the scaleType to scale it:

将drawable插入ImageView后,可以设置scaleType以缩放它:

scaleType = fitCenter -> Drawable will be the same size as the ImageView but will stay a perfect circle! scaleType = centerInside -> Drawable will be height and width you specified in the parameter etc ...

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

#2


-2  

just put android:gravity="center" into item tag

只需将android:gravity =“center”放入item标签即可