【问题标题】:How to create ring shape drawable in android?如何在android中创建可绘制的环形?
【发布时间】:2015-06-05 21:36:40
【问题描述】:

使用这段代码,我得到了一个边框:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadius="15dp"
    android:thickness="2dp"
    android:useLevel="false">
    <solid android:color="#4d4d4d" />

</shape> 

如何制作如下图所示的环形:

【问题讨论】:

  • 但是,如果您可以在 Photoshop 中制作这个形状并将其用作可绘制对象,为什么您需要在 Android 中执行此操作?
  • 我认为在 android 中使用 shape 比在 photoshop 中使用 shape 更好。
  • 我想知道使用 VectorDrawable 是否也是一个不错的选择,而不是其他解决方案。

标签: android shape


【解决方案1】:

具有 2dp 间隙的 2dp 外圈:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:top="4dp"
        android:right="4dp"
        android:bottom="4dp"
        android:left="4dp">
        <shape
            android:shape="oval">
            <solid android:color="#4d4d4d" />
        </shape>
    </item>
    <item>
        <shape
            android:shape="oval">
            <stroke android:width="2dp"
                android:color="#4d4d4d"/>
        </shape>
    </item>
</layer-list>

【讨论】:

  • 戒指怎么用?
【解决方案2】:

您需要创建一个可绘制对象并将其设置为背景。

shape_primary_ring.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:innerRadiusRatio="2.5"
    android:shape="ring"
    android:thickness="4dp"
    android:useLevel="false">

    <solid android:color="@color/colorPrimary"/>

</shape>

预览

【讨论】:

    【解决方案3】:

    Ring drawable是元素的并置,使用layer-list

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:right="6dip" android:left="6dip">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
               android:innerRadius="0dp"
               android:shape="ring"
               android:thicknessRatio="3"
               android:useLevel="false" >
          <solid android:color="@android:color/transparent" />
          <stroke
              android:width="5dp"
              android:color="@color/maroon" />
        </shape>
      </item>
      <item android:right="20dip"
            android:left="20dip"
            android:bottom="0dip"
            android:top="34dip">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
               android:shape="rectangle"
               android:innerRadius="0dp">
          <solid android:color="@color/maroon" />
          <stroke android:width="1dip" android:color="@android:color/transparent" />
        </shape>
      </item>
      <item android:right="20dip"
            android:left="20dip"
            android:bottom="34dip"
            android:top="0dip">
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
               android:shape="rectangle"
               android:innerRadius="0dp">
          <solid android:color="@color/maroon" />
          <stroke android:width="1dip" android:color="@android:color/transparent" />
        </shape>
      </item>
    </layer-list>
    

    Xml drawable 比静态图像更有用,它们可以正确缩放而不需要 .9 库或从 Gimp、Photoshop 生成不同大小的集合

    【讨论】:

      【解决方案4】:

      我认为在 android 中使用 shape 比在 photoshop 中使用 shape 更好。

      正确,创建一个可绘制对象更好,因为您可以使用代码更改颜色或形状,而不是创建新的图像资源。 使用 2 个 Views 和一个 TextView 创建一个 FrameLayout。第一个视图背景将是您的外环(形状),第二个视图背景将是圆形(形状)。最后一个View(更大的z-index)你的TextView:

      <FrameLayout>
         <View/><!-- (outer ring)-->
         <View/><!-- (filed circle)-->
         <TextView/><!-- (text)-->
      </FrameLayout>
      

      【讨论】:

        【解决方案5】:
        <?xml version="1.0" encoding="UTF-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android"
            android:fromDegrees="120"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="140">
            <item android:id="@android:id/background">
                <shape
                    android:angle="0"
                    android:innerRadiusRatio="2.3"
                    android:shape="ring"
                    android:thicknessRatio="20.0"
                    android:type="sweep"
                    android:useLevel="false">
                    <solid android:color="@color/red" />
                </shape>
            </item>
        </layer-list>
        

        【讨论】:

        • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
        【解决方案6】:

        <item>
            <shape
                android:innerRadiusRatio="4"
                android:shape="ring"
                android:thicknessRatio="15"
                android:useLevel="false" >
                <solid android:color="@color/white_color" />
                <size
                    android:height="48dip"
                    android:width="48dip" />
            </shape>
        
        </item>
        <item>
            <shape xmlns:android="http://schemas.android.com/apk/res/android"
                android:shape="ring"
                android:innerRadius="0dp"
                android:thickness="55dp"
                android:useLevel="false">
                <solid android:color="@color/white_color"/>
                <size android:height="200dp"
                    android:width="200dp"/>
                <stroke android:color="@color/green_color" android:width="5dp"/>
            </shape>
        </item>
        

        【讨论】:

        • 如何阻止斯托克在戒指的右手边划一条线?
        【解决方案7】:
        <?xml version="1.0" encoding="utf-8"
            <selector xmlns:android="http://schemas.android.com/apk/res/android">
                <item>
                    <shape android:innerRadius="10dp" android:shape="ring" android:thickness="2dp" android:useLevel="false">
                        <solid android:color="#dfdfdf" />
                    </shape>
                </item>
             </selector>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多