android shape的使用详解以及常用效果(渐变色、分割线、边框、半透明阴影效果等)

shape使用、渐变色、分割线、边框、半透明、半透明阴影效果。

首先简单了解一下shape中常见的属性。(详细介绍参看 api文档

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >   --- 默认为rectangle
    <corners  -- shape=“rectangle”时使用, 
        android:radius="integer"  -- 半径,会被下边的属性覆盖,默认为1dp,
        android:topLeftRadius="integer" 
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient  -- 渐变
        android:angle="integer"
        android:centerX="integer"
        android:centerY="integer"
        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    -- 指定大小,一般用在imageview配合scaleType属性使用。大小一般会适配滴
        android:width="integer"
        android:height="integer" />
    <solid    -- 填充颜色,可是是十六进制颜色。(比如想设置半透明效果,直接使用十六就只就OK)
        android:color="color" />
    <stroke    -- 指定边框,border,dashWidth和dashGap有一个为0dp则为
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"    -- 虚线宽度
        android:dashGap="integer" />    -- 虚线间隔宽度
</shape>

注意:
<corners>
1、android:radius,半 径,会被下边的单个角度半径属性覆盖,默认为1dp,
2、在使用时,如果单独设置四个角度,又大小不一致时,eclipse的graphics preview会报错。但是直接真机运行即可。(比如实线上边直角,下边屈角的效果)
<size>
Note:  The shape scales to the size of the container View proportionate to the dimensions defined here, by default. When you use the shape in an  ImageView , you can restrict scaling by setting the  android:scaleType  to  "center"

举个栗子:

1、渐变色

 res/drawable/gradient_box.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>
如图:

2、白色边框、半透明效果

[html]  view plain copy

在CODE上查看代码片

派生到我的代码片

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle" >  
  4.     <corners android:radius="16dp" />  
  5.     <!-- 这是半透明,还可以设置全透明,那就是白色边框的效果了 -->  
  6.     <solid android:color="#80065e8d" />  
  7.     <stroke  
  8.         android:dashGap="0dp"  
  9.         android:width="4dp"  
  10.         android:color="@android:color/white" />  
  11. </shape>  
如图:

   
3、分割线效果:
[html]  view plain copy

在CODE上查看代码片

派生到我的代码片

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="line" >  
  4.     <stroke  
  5.         android:width="4dp"  
  6.         android:color="@android:color/black" />  
  7. </shape>  
如果:
4、单边屈角效果
[html]  view plain copy

在CODE上查看代码片

派生到我的代码片

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <corners   
  5.         android:topLeftRadius="5dp"  
  6.         android:topRightRadius="5dp"  
  7.         android:bottomLeftRadius="30dp"  
  8.         android:bottomRightRadius="30dp"/>  
  9.   
  10.     <!-- 这是半透明,还可以设置全透明,那就是白色边框的效果了 -->  
  11.     <solid android:color="#ff065e8d" />  
  12.   
  13.     <stroke  
  14.         android:dashGap="0dp"  
  15.         android:width="4dp"  
  16.         android:color="@android:color/white" />  
  17.   
  18. </shape>  

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谭祖爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值