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

Mic*_*riv 5 xml android shape xml-drawable layer-list

我尝试使用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>
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

生成的图形如下所示:

渲染活动的屏幕截图

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

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

更新:

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

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

A. *_*gen 6

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

<?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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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