可绘制对象-shape

https://developer.android.google.cn/guide/topics/resources/drawable-resource?hl=zh-cn

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false">
    <solid android:color="#00FF00" />
    <stroke android:width="1dip" android:color="#000000" />
    <size
        android:width="15dp"
        android:height="15dp" />
</shape>

1:shape必须为根元素   xmlns:命名空间


  android:shape指定形状类型

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >


    rectangle:填充包含视图的矩形,当不写该属性时默认此形状
    oval:椭圆形状
    line:线形状,此形状需要 <stroke> 元素定义线宽
    ring:环形
   当android:shape="ring"时,有可选属性
        android:innerRadius:尺寸。环内部(中间的孔)的半径
        android:innerRadiusRatio:浮点型。环内部的半径,以环宽度的比率表示。例如,如果 android:innerRadiusRatio="5",内           半径等于环宽度除以 5。此值被 android:innerRadius 覆盖。默认值为 9。
        android:thickness:环的厚度
        android:thicknessRatio:浮点型。环的厚度,表示为环宽度的比率。例如,如果 android:thicknessRatio="2",则厚度等于环        宽度除以 2。此值被 android:innerRadius 覆盖。默认值为 3。
    需要注意的是,如果你使用了上面几个属性绘制一个圆,会发现在控件中并没有效果,我们还需要一个重要属性
        android:useLevel:布尔值。如果这用作 LevelListDrawable,则此值为“true”。这通常应为“false”,否则形状不会显示。
:

   
  2:   corners为形状产生圆角,仅当形状为矩形时使用

 <corners
        android:radius="integer" 
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />


        android:radius:尺寸。所有角的半径,以尺寸值或尺寸资源表示。对于每个角,这会被以下属性覆盖。
        android:topLeftRadius:左上角的半径,以尺寸值或尺寸资源表示。
        android:topRightRadius:右上角的半径,以尺寸值或尺寸资源表示。
        android:bottomLeftRadius:左下角的半径,以尺寸值或尺寸资源表示。
        android:bottomRightRadius:右下角的半径,以尺寸值或尺寸资源表示
    -->
   
       3:gredient 指定形状的渐变颜色,使用渐变时不要使用solid,否则会被覆盖,看不到效果

    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />


        android:angle:整型。渐变的角度(度)。0 为从左到右,90 为从上到上。必须是 45 的倍数。默认值为 0,如果写该值不是45的整数倍,将没有效果。
        android:centerX:浮点型。渐变中心的相对 X 轴位置 (0 - 1.0)。
        android:centerY:浮点型。渐变中心的相对 Y 轴位置 (0 - 1.0)。
        android:centerColor:颜色。起始颜色与结束颜色之间的可选颜色,以十六进制值或颜色资源表示。
        android:endColor:颜色。结束颜色,表示为十六进制值或颜色资源。
        android:gradientRadius:浮点型。渐变的半径。仅在 android:type="radial" 时适用。且radial时必须有该属性,否则无效果
        android:startColor:颜色。起始颜色,表示为十六进制值或颜色资源。
        android:type:要应用的渐变图案的类型。有效值为
                      "linear"  线性渐变。这是默认值。
                      "radial"  径向渐变。起始颜色为中心颜色。
                      "sweep"   流线型渐变。
        android:useLevel:布尔值。如果这用作 LevelListDrawable,则此值为“true”。
  
    4:padding    要应用到包含视图元素的内边距(这会填充视图内容的位置,而非形状)。

    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />


        android:left:尺寸。左内边距,表示为尺寸值或尺寸资源
        android:top:尺寸。上内边距,表示为尺寸值或尺寸资源
        android:right:尺寸。右内边距,表示为尺寸值或尺寸资源
        android:bottom:尺寸。下内边距,表示为尺寸值或尺寸资源

        5: size 指定形状的大小

 <size
        android:width="integer"
        android:height="integer" />

 注:默认情况下,形状按照此处定义的尺寸按比例缩放至容器视图的大小。在 ImageView 中使用形状时,可通过将 android:scaleType 设置为 "center" 来限制缩放。

      6 solid  用于填充形状的颜色

<solid  android:color="color" />


        android:color:颜色。应用于形状的颜色

     7:stoke   形状的笔划中线。即形状的边框

 <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />

</shape>

        android:width:尺寸。线宽,以尺寸值或尺寸资源表示。
        android:color:颜色。线的颜色,表示为十六进制值或颜色资源。
        android:dashGap:尺寸。短划线的间距,以尺寸值或尺寸资源表示。仅在设置了 android:dashWidth 时有效。
        android:dashWidth:尺寸。每个短划线的大小,以尺寸值或尺寸资源表示。仅在设置了 android:dashGap 时有效。

   8  动态修改shape

        Button button = (Button) findViewById(R.id.button);
        GradientDrawable gradientDrawable = (GradientDrawable) button.getBackground();
        gradientDrawable.setColor(Color.parseColor("#6ABD20"));
        gradientDrawable.setStroke(1,Color.parseColor("#6ABD20"));

 

图层列表layer-list

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="6dp">
        <shape android:shape="rectangle">
            <solid android:color="#8f000000" />
            <corners android:radius="3dp" />
            <size
                android:width="10dp"
                android:height="10dp" />
        </shape>
    </item>
    <item>
        <bitmap
            android:gravity="center_horizontal|top"
            android:src="@mipmap/ic_down_arrow" />
    </item>
</layer-list>

 

<?xml version="1.0" encoding="utf-8"?>

必备。这必须是根元素。包含一个或多个 <item> 元素。
1:xmlns:android:字符串。必备。定义 XML 命名空间,其必须是 "http://schemas.android.com/apk/res/android"。

<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android" >


    <!--
    定义要放在图层可绘制对象中由其属性定义的位置的可绘制对象。必须是 <selector> 元素的子项。接受子 <bitmap> 元素。
    android:drawable:可绘制对象资源。必备。引用可绘制对象资源。
    android:id:资源 ID。此可绘制对象的唯一资源 ID。要为此项新建资源 ID,请使用以下形式:"@+id/name"。加号表示应创建为新 ID。可以使用此 ID 检索和修改具有 View.findViewById() 或 Activity.findViewById() 的可绘制对象。
    android:top:整型。顶部偏移(像素)。
    android:right:整型。右边偏移(像素)。
    android:bottom:整型。底部偏移(像素)。
    android:left:整型。左边偏移(像素)。
   

<item
        android:drawable="@[package:]drawable/drawable_resource"
        android:id="@[+][package:]id/resource_name"
        android:top="dimension"
        android:right="dimension"
        android:bottom="dimension"
        android:left="dimension" />

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值