Android Drawable--Shape

解析Android Shape资源
本文详细介绍了Android中Shape资源的各种属性及用途,包括shape的基本类型、corner属性如何使矩形变圆角、gradient属性如何实现渐变效果等。适用于希望深入了解Android自定义视图和界面设计的开发者。

背景
继上一篇《Android Drawable Resources》,这里来分析shape的使用,值得注意的是此shape可以作为之前各Drawable里item的元素,所以可以让Drawable Resources更加多变。另外,shape所对应的java类是GradientDrawable,可通过线程动态改变其shape属性,实现绚丽的动画。

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <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"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

以上便是从官网摘来关于shape的所有属性,下面我们来一一介绍这些属性。

android:shape=["rectangle" | "oval" | "line" | "ring"]

rectangle:shape的默认是一个矩形;
oval:椭圆形;
line:一条线;
ring:一个环型;

没有定义android:shape时,默认显示一个矩形:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="4dp"
        android:color="@color/colorAccent"/>
</shape>

定义android:shape为oval时,是一个圆形:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="4dp"
        android:color="@color/colorAccent"/>
</shape>

定义android:shape为line时,是一条line:

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <stroke
        android:width="4dp"
        android:color="@color/colorAccent"/>
</shape>

在定义android:shape为ring时,还需要定义其他属性才能显示,如:
android:innerRadius=”50dp” 内环半径
android:innerRadiusRatio=”5” 内环半径比例
android:thickness=”10dp” 环的厚度
android:thicknessRatio=”5” 厚度比例
android:useLevel=”false” 如果不是用作LevelListDrawable,要设置为false,否则不能显示。



corner
只有shape是矩形rectangle才起作用,即可设置矩形的四个角为圆角

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



gradient
指定一个渐变的颜色,用于填充button的颜色,貌似使用它之后,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:从左到右
45:从左下到右上
90:从下到上
-45:从右上到左下
-90:从上到下
180:从右到左
剩下就不举例了,反正所有值都是45的倍数。

android:centerX=”0.1f”
android:centerY=”0.1f”
只针对android:type为sweep或radial才起作用,即设置开始渐变在整个形状内的位置,值为0~1之间。

centerColor、startColor、endColor分别表示中间,开始、结尾的颜色。

    <gradient
        android:angle="45"
        android:centerColor="@color/black"
        android:startColor="@color/colorPrimary"
        android:endColor="@color/colorAccent"
    />

这里写图片描述

android:type的三个属性分别是:
linear:线性渐变
radial:辐射性渐变
sweep:横扫性渐变
这里写图片描述这里写图片描述



size
它是设置此shape的大小,如果在线程中改变此属性,shape会更趣,弄得我想写篇关于此动画的文章。

<size android:height="30dp"
      android:width="10dp"/>



solid
填充的颜色,就一个android:color属性,没有其他。



stroke
设置shape的边框样式:

 <stroke
        android:dashGap="10dp"
        android:dashWidth="5dp"
        android:color="@color/green"
        android:width="5dp"/>

这里写图片描述

Android中,Drawable Shape是一种自定义视图绘制形状的技术,它可以让你在布局文件中创建各种复杂的图形,如圆形、矩形、弧线等。如果你想要在Shape中添加文字,通常会结合`<vector>`标签,因为Android 5.0 (Lollipop)引入了向量绘图的支持。 首先,你需要在`res/drawable`目录下创建一个新的XML文件,比如`custom_shape.xml`: ```xml <vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="wrap_content" android:width="wrap_content" android:viewportWidth="100" android:viewportHeight="100"> <!-- 定义shape --> <group> <path android:pathData="M50,0 L50,100 A50,50 0 0,1 0,50 L100,50 A50,50 0 0,1 100,0 Z" /> <!-- 添加文字 --> <text android:text="Sample Text" android:fontFamily="@font/your_font_family" android:layout_width="wrap_content" android:layout_height="wrap_content" android:x="40" <!-- 文字x坐标调整位置 --> android:y="60" <!-- 文字y坐标调整位置 --> /> </group> </vector> ``` 在这个例子中,我们首先定义了一个路径(path)来表示形状,然后嵌套了一个`text`元素来放置文字。通过调整`android:x`和`android:y`属性,你可以定位文字在shape内的位置。 要使用这个自定义的drawable,只需在你的布局文件中像引用其他drawable一样引用它,并设置宽高属性: ```xml <ImageView android:src="@drawable/custom_shape" android:layout_width="wrap_content" android:layout_height="wrap_content"/> ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值