Android中Shape的用法详解
作者:Code_legend 发布时间:2023-12-21 14:05:44
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>
好了,其实里面的东西很简单,总结一下就好了。希望大家用的开心。
来源:http://blog.csdn.net/code_legend/article/details/74615251
猜你喜欢
- maven报错:Failed to execute goal on project项目结构在hzz-entity项目上运行Maven ins
- Spring整合mybatis的mapper生成过程mapperScannerConfigurer实现了BeandifinitionRegi
- 0 写在前面在实际工作中有一些地方需要用到截取字符串的方法,所以在此记录下截取字符串的几种方法。.substring()StringUtil
- 在往常的 HTTP 调用中,一直都是使用的官方提供的 RestTemplate 来进行远程调用,该调用方式将组装代码冗余到正常业务代码中,不
- 本文研究的主要是Spring的事务机制的相关内容,具体如下。JAVA EE传统事务机制通常有两种事务策略:全局事务和局部事务。全局事务可以跨
- 开发中遇到Eclipse报错:java.lang.OutOfMemoryError: PermGen space。PermGen space
- BigDecimal的舍入模式(RoundingMode)BigDecimal.divide方法中必须设置roundingMode,不然会报
- 前言最近代码里和WebView有很多的交互,webview是android中的浏览器控件,这里主要介绍一下webview如何重载WebVie
- 目录前言一、小提二、将传感器改成事件分发机制三、使用四、源码总结前言在之前写了一篇关于3D效果的文章,借助传感器展示,有小伙伴问可不可以改成
- 可扩展标记语言(XML)文件是一种标准的文本文件,它使用特定的标记来描述文档的结构以及其他特性。通过将XML转换为PDF,能够便于文件传输及
- Optional在JAVA中被定义为一个容器类,更确切的说只存一个元素的容器。container object which may or m
- Android 双击Back键退出应用的实现方法实现原理:双击退出程序的原理无非就是设置一个退出标识(询问是否退出),如果改变了这个标识(确
- 本文实例讲述了C#实现功能强大的中国农历日历操作类。分享给大家供大家参考。具体如下:这个C#类定义了中国农历日历,除了可以输入正常的日历外还
- 集合>队列Queue>创建队列System.Collections.Queue类提供了四种重载构造函数。using System
- 最近在做的工作要用到本地方法,需要在Java中加载不少动态链接库(以下为方便延用Windows平台下的简写dll,但并不局限于Windows
- 实现代码超简单,具体实现方法如下:有时候当我们的游戏人物遇敌时,我们需我怪物随机根据概率选择处理方式,如下:1、50%的机会友好的问候2、2
- 背景笔者所在项目组在搭建一个全新项目的时候选用了SpringBoot3.x,项目中应用了很多SpringBoot2.x时代相关的第三方组件例
- 单元测试是编写测试代码,应该准确、快速地保证程序基本模块的正确性。JUnit是Java单元测试框架,已经在Eclipse中默认安装。JUni
- 这篇文章使用Java组件显示窗口,在通过输入的图片url地址在窗口中显示出来,可作为一个网络图片查看器,感兴趣的可以打包成jar或者.exe
- Java内存分配与管理是Java的核心技术之一,之前我们曾介绍过Java的内存管理与内存泄露以及Java垃圾回收方面的知识,今天我们再次深入