Android中Shape的用法详解

所属分类: 软件编程 / Android 阅读数: 24
收藏 0 赞 0 分享

ShapeDrawable是一种很常见的Drawable,可以理解为通过颜色来构造的图形,它既可以是纯色的图形,也可以是具有渐变效果的图形,ShapeDrawabled语法稍显复杂,如下所示:

<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="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
    android:width="integer"
    android:height="integer" />
  <solid
    android:color="color" />
  <stroke
    android:width="integer"
    android:color="color"
    android:dashWidth="integer"
    android:dashGap="integer" />
</shape>

•Android: shape

•有4个选项,rectangle(矩形)oval(椭圆)line(横线)ring(圆环),默认为rectangle,需要注意line和ring需要通过标签来指定线的宽度和颜色等信息,否则无法达到预期效果

•首先来说一下最常用的rectangle(矩形),一般都是在按钮或者字体上面设置一个background的Drawable。一般设置效果为正方形或者两边有弧度的形状。

◦第一种情况就是设置矩形背景

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle" 
 > 
 <size 
 android:width="200dp" 
 android:height="20dp" 
 /> 
 <solid android:color="#d22121"/> 
 </shape> 

通过设置size设置矩形的宽度和高度,*这里需要说明一下,咱们在这里设置size的宽高,在最终显示尺寸是没有用的,也就是说当你在一个控件中设置background的时候,这个shape是会被拉伸或者缩小为view的大小。*solid属性设置矩形里面的背景颜色。

这里写图片描述

将背景色设置为渐变

<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle" 
 > 
 <size 
 android:width="200dp" 
 android:height="20dp" 
 /> 
 <gradient 
 android:startColor="#fff" 
 android:centerColor="#f1a9a9" 
 android:endColor="#ec5b5b" 
 android:type="linear" 
 /> 
 </shape>

效果图:

这里写图片描述

这里默认的type就是linear,里面还有其他两个属性可以选择分别是radial(径向渐变)和sweep(扫描渐变)
一般最常用的也就是线性渐变还有其他几个属性没有用但是很好理解

android:angle——渐变的角度,默认为0,其值必须是45的倍数,0表示从左到右,90表示从下到上。

android:centerX——渐变的中心点横坐标

android:centerY——渐变的中心点纵坐标

android:gradientRadiu——渐变半径,仅当android:type=”radial”时有效

•接下来说一下画圆角的矩形背景

◦其实只用设置一下corners的属性就是了。

这里写图片描述

◦具体详细的说明

◦android:radius—— 给四个角设置相同的角度,优先级较低,会被其他四个属性覆盖

android:bottomLeftRadius——设定左下角的角度

android:bottomRightRadius——设定右下角的角度

android:TopLeftRadius——设定左上角的角度

android:TopRightRadius——设定右上角的角度

接下来就是如何画一个空心的背景

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle" 
 > 
 <size 
 android:width="200dp" 
 android:height="20dp" 
 /> 
 <stroke 
 android:width="1px" 
 android:color="#ffff1c77" 
 /> 
 <corners android:radius="50dp"/> 
 </shape> 

效果图如下

这里写图片描述 

 当然里面也可以自由发挥设置渐变色,但是一般里面都纯色。

◦这里里面也可以设置为虚线

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle" 
 > 
 <size 
 android:width="200dp" 
 android:height="20dp" 
 /> 
 <stroke 
 android:dashWidth="4dp" 
 android:dashGap="2dp" 
 android:width="1px" 
 android:color="#ffff1c77" 
 /> 
 <corners android:radius="50dp"/> 
 </shape>

这里写图片描述

好了,其实里面的东西很简单,总结一下就好了。希望大家用的开心。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多