当前位置: 首页 > news >正文

Android Shape设置背景

设置背景时,经常这样 android:background=“@drawable/xxx” 。如果是纯色图片,可以考虑用 shape 替代。

shape 相比图片,减少资源占用,缩减APK体积。

开始使用。

<?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape=["rectangle" | "oval" | "line" | "ring"] ><cornersandroid:radius="integer"android:topLeftRadius="integer"android:topRightRadius="integer"android:bottomLeftRadius="integer"android:bottomRightRadius="integer" /><gradientandroid: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"] /><paddingandroid:left="integer"android:top="integer"android:right="integer"android:bottom="integer" /><sizeandroid:width="integer"android:height="integer" /><solidandroid:color="color" /><strokeandroid:width="integer"android:color="color"android:dashWidth="integer"android:dashGap="integer" />
</shape>

概览

使用 shape ,可以实现 矩形、椭圆、直线、圆环。

  • corners :设置圆角,android:radius 设置统一的圆角。也可以单独设置四个角的圆角。
  • gradient :渐变,有线性渐变(默认)、放射渐变(类似中心往外扩散的效果)、扫描式渐变(转一圈的效果)。
  • padding :内边距。和 View 的 padding 使用一样。可以不设置,由 View 来决定。
  • size :大小。设置宽高,和 View 的 padding 使用一样。可以不设置,由 View 来决定。
  • solid :填充颜色。
  • stroke :描边。可以设置边界的颜色,设置边界边缘为虚线。

使用方法:

  • 1.在 res/drawable/ 目录创建 shape_demo.xml 。
  • 2.在布局文件中 android:background=“@drawable/shape_demo”

矩形

默认直角矩形

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--矩形--><!--填充颜色--><solid android:color="@color/purple_200" />
</shape>

圆角

用 corners 设置圆角,圆角的幅度由 android:radius 控制。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--矩形--><!--填充颜色--><solid android:color="@color/purple_200" /><!--圆角--><cornersandroid:radius="10dp"/>
</shape>

描边

用 stroke 描边,默认边缘时曲线,添加了 android:dashWidth 、android:dashGap 就是虚线。

  • android:width 指定宽度,
  • android:color 是边缘颜色,
  • android:dashWidth 是虚线线段的宽度,
  • android:dashGap 是虚线之间的间隔
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--矩形--><!--填充颜色--><solid android:color="@color/purple_200" /><!--圆角--><cornersandroid:radius="20dp"/><!--描边--><strokeandroid:width="2dp"android:color="@color/my_red"android:dashWidth="10dp"android:dashGap="2dp" />
</shape>

渐变色

用 gradient 设置渐变色,

  • android:type :渐变色类型,线性渐变(默认)、放射渐变(类似中心往外扩散的效果)、扫描式渐变(转一圈的效果)。
  • android:angle :渐变开始角度。线性渐变下有效。
    Angle of the gradient, used only with linear gradient. Must be a multiple of 45 in the range [0, 315].
  • android:startColor :渐变开始的颜色
  • android:centerColor :渐变中间的颜色
  • android:endColor :渐变结束的颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><!--矩形--><!--圆角--><corners android:radius="20dp" /><gradientandroid:centerColor="@android:color/holo_orange_dark"android:endColor="@color/my_red"android:startColor="@android:color/holo_green_dark" /></shape>

几种矩形效果对比,
在这里插入图片描述

学会了矩形,其他的也就会了。

椭圆

线性渐变,

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><!--椭圆--><!--填充颜色--><solid android:color="@color/teal_200" /><!--描边--><strokeandroid:width="2dp"android:color="@color/purple_200" /><gradientandroid:endColor="@color/teal_200"android:startColor="@color/my_red"android:type="linear" /></shape>

放射渐变,在 线性渐变 基础上把 android:type 改为 radial ,同时设置 android:gradientRadius ,它决定内圆的大小。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="oval"><!--椭圆--><!--填充颜色--><solid android:color="@color/teal_200" /><!--描边--><strokeandroid:width="2dp"android:color="@color/purple_200" /><gradientandroid:gradientRadius="50dp"android:endColor="@color/teal_200"android:startColor="@color/my_red"android:type="radial" />
</shape>

扫描式渐变,在 线性渐变 基础上把 android:type 改为 sweep 。

linear 、radial 、sweep ,三种渐变色的对比,
在这里插入图片描述

直线/虚线

直线

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line"><!--线--><size android:width="100dp" android:height="2dp"/><!--直线--><strokeandroid:width="2dp"android:color="@color/my_red"/></shape>

虚线,虚线就是直线加上描边效果。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="line"><!--线--><size android:width="100dp" android:height="2dp"/><!--虚线--><strokeandroid:width="2dp"android:color="@color/teal_200"android:dashWidth="2dp"android:dashGap="2dp" /></shape>

对比,
在这里插入图片描述

圆环

android:shape=“ring” ,

  • android:innerRadius :内圆半径,直接设置 dp 值。
  • android:thickness :圆环厚度,直接设置 dp 值。
  • android:useLevel :设为 false ,否则不显示。
  • android:innerRadiusRatio :内圆半径,圆环宽度占比的形式,如 设为 4 ,意思是 内圆半径 = 圆环宽度 / 4 。
  • android:thicknessRatio :圆环厚度,圆环宽度占比的形式,如 设为 4 ,意思是 内圆半径 = 圆环宽度 / 4 。

本例 View 限定宽高都为 100 dp ,这两种写法,圆环大小是一样的。
写法1 ,

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="ring"android:innerRadius="25dp"android:thickness="25dp"android:useLevel="false"><!--颜色--><solid android:color="@color/purple_200"/>
</shape>

写法2 ,

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:innerRadiusRatio="4"android:shape="ring"android:thicknessRatio="4"android:useLevel="false"><!--颜色--><solid android:color="@color/my_red" /><!--渐变色--><gradientandroid:angle="0"android:endColor="@color/teal_200"android:startColor="@color/my_red"/>
</shape>

写法 2 , gradient 中设置 android:angle=“90” ,看下和 0 的对比

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:innerRadiusRatio="4"android:shape="ring"android:thicknessRatio="4"android:useLevel="false"><!--颜色--><solid android:color="@color/my_red" /><!--渐变色--><gradientandroid:angle="90"android:endColor="@color/teal_200"android:startColor="@color/my_red"/>
</shape>

三种效果对比,
在这里插入图片描述

  • android:angle 为 0 是中间的效果,左边是开始渐变的颜色,右边是结束渐变的颜色。
  • android:angle 为 90 是右边的效果,下面是开始渐变的颜色,上面是结束渐变的颜色。

相关文章:

Android Shape设置背景

设置背景时&#xff0c;经常这样 android:background“drawable/xxx” 。如果是纯色图片&#xff0c;可以考虑用 shape 替代。 shape 相比图片&#xff0c;减少资源占用&#xff0c;缩减APK体积。 开始使用。 <?xml version"1.0" encoding"utf-8"?…...

什么是GraphQL?它与传统的REST API有什么不同?

聚沙成塔每天进步一点点 ⭐ 专栏简介⭐ 什么是GraphQL&#xff1f;⭐ 与传统的REST API 的不同⭐ 写在最后 ⭐ 专栏简介 前端入门之旅&#xff1a;探索Web开发的奇妙世界 欢迎来到前端入门之旅&#xff01;感兴趣的可以订阅本专栏哦&#xff01;这个专栏是为那些对Web开发感兴趣…...

如何定时备份使用Docker构建的MySQL容器中的数据库

&#x1f468;&#x1f3fb;‍&#x1f4bb; 热爱摄影的程序员 &#x1f468;&#x1f3fb;‍&#x1f3a8; 喜欢编码的设计师 &#x1f9d5;&#x1f3fb; 擅长设计的剪辑师 &#x1f9d1;&#x1f3fb;‍&#x1f3eb; 一位高冷无情的编码爱好者 大家好&#xff0c;我是 DevO…...

Java【手撕链表】LeetCode 143. “重排链表“, 图文详解思路分析 + 代码

文章目录 前言一、两数相加1, 题目2, 思路分析2,1 找到中间结点2.2, 逆序后半段链表2.3, 合并两个链表 3, 代码 前言 各位读者好, 我是小陈, 这是我的个人主页, 希望我的专栏能够帮助到你: &#x1f4d5; JavaSE基础: 基础语法, 类和对象, 封装继承多态, 接口, 综合小练习图书管…...

C语言 cortex-A7核 按键中断 实验【重点】

一、KEY1 include/key.h #ifndef __KEY_H__ #define __KEY_H__#include "stm32mp1xx_rcc.h" #include "stm32mp1xx_gpio.h" #include "stm32mp1xx_exti.h" #include "stm32mp1xx_gic.h"//RCC/GPIO章节初始化 void hal_rcc_gpio_init…...

freertos中函数调用和启动第一个任务(栈相关!!!!!!)

本内容仅就一些较难理解的点讲解&#xff0c;请结合其它文章实用 在函数调用时&#xff0c;m3的处理器使用r0-r3共四个寄存器传参&#xff0c;其余的使用栈传参。 但是&#xff0c;如果传入的参数是全局变量&#xff0c;则不需传参&#xff0c;因为全局变量在函数内部是可见的…...

【PHP】如何关闭buffer实时输出内容到前端

前言 默认情况下&#xff0c;我们在PHP里使用echo等函数输出的内容&#xff0c;是不会马上发送给前端的&#xff0c;原因是有 buffer 的存在&#xff0c;buffer又分两处&#xff0c;一处是PHP本身的buffer&#xff0c;另一处是Nginx的buffer。只有当buffer满了之后&#xff0c…...

Scala第二章节

Scala第二章节 scala总目录 章节目标 掌握变量, 字符串的定义和使用掌握数据类型的划分和数据类型转换的内容掌握键盘录入功能理解Scala中的常量, 标识符相关内容 1. 输出语句和分号 1.1 输出语句 方式一: 换行输出 格式: println(里边写你要打印到控制台的数据);方式二…...

Spring修炼之路(2)依赖注入(DI)

一、概念 依赖注入&#xff08;Dependency Injection,DI&#xff09;。 测试pojo类 : Address.java 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 . 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 . 二、 注入方式 2.1构造器注入 我们在之前的案例已经…...

编写Android.mk / Android.bp 引用三方 jar 包,aar包,so 库

一.前言 在Android10之后&#xff0c;所有项目工程中&#xff0c;官方推荐使用Android.bp去编译构建&#xff0c;以前使用Android.mk构建的项目随着版本迭代升级&#xff0c;慢慢需要变更为Android.bp&#xff0c; 两者的语法都需要去了解并熟练使用。 笔者之前写过Android.mk的…...

【kylin】【ubuntu】搭建本地源

文章目录 一、制作一个本地源仓库制作ubuntu本地仓库制作kylin本地源 二、制作内网源服务器ubuntu系统kylin系统 三、使用内网源ubuntukylin 一、制作一个本地源仓库 制作ubuntu本地仓库 首先需要构建一个本地仓库&#xff0c;用来存放软件包 mkdir -p /path/to/localname/pac…...

为什么 Go 语言 struct 要使用 tags

在 Go 语言中&#xff0c;struct 是一种常见的数据类型&#xff0c;它可以用来表示复杂的数据结构。在 struct 中&#xff0c;我们可以定义多个字段&#xff0c;每个字段可以有不同的类型和名称。 除了这些基本信息之外&#xff0c;Go 还提供了 struct tags&#xff0c;它可以用…...

WebGL笔记:WebGL中JS与GLSL ES 语言通信,着色器间的数据传输示例:用鼠标控制点位

用鼠标控制点位 <canvas id"canvas"></canvas><!-- 顶点着色器 --> <script id"vertexShader" type"x-shader/x-vertex">attribute vec4 a_Position;void main() {// 点位gl_Position a_Position;// 尺寸gl_PointSize…...

算法 主持人调度-(双指针+贪心)

牛客网: BM96 题目: 一个主持人只能参加一个活动&#xff0c;至少需要多少主持人 思路: 对start, end排序从小到大&#xff1b;初始化指针l, r 0, 0&#xff1b;start[r]< end[l]时需要累加人数同时r&#xff0c;否则l,r同时移动&#xff1b;直至不再满中l<n &&am…...

Elasticsearch 集群时的内部结构是怎样的?

Apache Lucene : Flush, Commit Elasticsearch 是一个基于 Apache Lucene 构建的搜索引擎。 它利用 Lucene 的倒排索引、查询处理和返回搜索结果等功能来执行搜索。 它还扩展了 Lucene 的功能&#xff0c;添加分布式处理功能以支持大型数据集的搜索。 让我们看一下 Apache Luc…...

IoTDB 在国际数据库性能测试排行榜中位居第一?测试环境复现与流程详解第一弹!...

最近我们得知&#xff0c;Apache IoTDB 多项性能表现位居 benchANT 时序数据库排行榜&#xff08;Time Series: DevOps&#xff09;性能排行第一名&#xff01;&#xff08;榜单地址&#xff1a;https://benchANT.com/ranking/database-ranking&#xff09; benchANT 位于德国&…...

react项目优化

随着项目体积增大&#xff0c;打包的文件体积会越来越大&#xff0c;需要优化&#xff0c;原因无非就是引入的第三方插件比较大导致&#xff0c;下面我们先介绍如何分析各个文件占用体积的大小。 1.webpack-bundle-analyzer插件 如果是webpack作为打包工具的项目可以使用&…...

青藏高原1-km分辨率生态环境质量变化数据集(2000-2020)

青藏高原平均海拔4000米以上&#xff0c;人口1300万&#xff0c;是亚洲九大河流的源头&#xff0c;为超过15亿人口提供淡水、食物和其他生态系统服务&#xff0c;被誉为地球第三极和亚洲水塔。然而&#xff0c;在该地区的人与自然的关系的研究是有限的&#xff0c;尤其是在精细…...

Nature Communications | 张阳实验室:端到端深度学习实现高精度RNA结构预测

RNA分子是基因转录的主要执行者&#xff0c;也是细胞运作的隐形功臣。它们在基因表达调控、支架构建以及催化活性等多个生命过程中都扮演着关键角色。虽然RNA如此重要&#xff0c;但由于实验数据的缺乏&#xff0c;准确预测RNA 的三维空间结构仍然是目前计算生物学面临的重大挑…...

提升您的Mac文件拖拽体验——Dropzone 4 for mac

大家都知道&#xff0c;在Mac上进行文件拖拽是一件非常方便的事情。然而&#xff0c;随着我们在工作和生活中越来越多地使用电脑&#xff0c;我们对于这个简单操作的需求也越来越高。为了让您的文件拖拽体验更加高效和便捷&#xff0c;今天我们向大家介绍一款强大的工具——Dro…...

线程同步:确保多线程程序的安全与高效!

全文目录&#xff1a; 开篇语前序前言第一部分&#xff1a;线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分&#xff1a;synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分&#xff…...

Swift 协议扩展精进之路:解决 CoreData 托管实体子类的类型不匹配问题(下)

概述 在 Swift 开发语言中&#xff0c;各位秃头小码农们可以充分利用语法本身所带来的便利去劈荆斩棘。我们还可以恣意利用泛型、协议关联类型和协议扩展来进一步简化和优化我们复杂的代码需求。 不过&#xff0c;在涉及到多个子类派生于基类进行多态模拟的场景下&#xff0c;…...

linux 错误码总结

1,错误码的概念与作用 在Linux系统中,错误码是系统调用或库函数在执行失败时返回的特定数值,用于指示具体的错误类型。这些错误码通过全局变量errno来存储和传递,errno由操作系统维护,保存最近一次发生的错误信息。值得注意的是,errno的值在每次系统调用或函数调用失败时…...

React19源码系列之 事件插件系统

事件类别 事件类型 定义 文档 Event Event 接口表示在 EventTarget 上出现的事件。 Event - Web API | MDN UIEvent UIEvent 接口表示简单的用户界面事件。 UIEvent - Web API | MDN KeyboardEvent KeyboardEvent 对象描述了用户与键盘的交互。 KeyboardEvent - Web…...

ffmpeg(四):滤镜命令

FFmpeg 的滤镜命令是用于音视频处理中的强大工具&#xff0c;可以完成剪裁、缩放、加水印、调色、合成、旋转、模糊、叠加字幕等复杂的操作。其核心语法格式一般如下&#xff1a; ffmpeg -i input.mp4 -vf "滤镜参数" output.mp4或者带音频滤镜&#xff1a; ffmpeg…...

Qt Http Server模块功能及架构

Qt Http Server 是 Qt 6.0 中引入的一个新模块&#xff0c;它提供了一个轻量级的 HTTP 服务器实现&#xff0c;主要用于构建基于 HTTP 的应用程序和服务。 功能介绍&#xff1a; 主要功能 HTTP服务器功能&#xff1a; 支持 HTTP/1.1 协议 简单的请求/响应处理模型 支持 GET…...

使用Spring AI和MCP协议构建图片搜索服务

目录 使用Spring AI和MCP协议构建图片搜索服务 引言 技术栈概览 项目架构设计 架构图 服务端开发 1. 创建Spring Boot项目 2. 实现图片搜索工具 3. 配置传输模式 Stdio模式&#xff08;本地调用&#xff09; SSE模式&#xff08;远程调用&#xff09; 4. 注册工具提…...

在 Spring Boot 中使用 JSP

jsp&#xff1f; 好多年没用了。重新整一下 还费了点时间&#xff0c;记录一下。 项目结构&#xff1a; pom: <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0" xmlns:xsi"http://ww…...

【C++】纯虚函数类外可以写实现吗?

1. 答案 先说答案&#xff0c;可以。 2.代码测试 .h头文件 #include <iostream> #include <string>// 抽象基类 class AbstractBase { public:AbstractBase() default;virtual ~AbstractBase() default; // 默认析构函数public:virtual int PureVirtualFunct…...

加密通信 + 行为分析:运营商行业安全防御体系重构

在数字经济蓬勃发展的时代&#xff0c;运营商作为信息通信网络的核心枢纽&#xff0c;承载着海量用户数据与关键业务传输&#xff0c;其安全防御体系的可靠性直接关乎国家安全、社会稳定与企业发展。随着网络攻击手段的不断升级&#xff0c;传统安全防护体系逐渐暴露出局限性&a…...