Android进阶之路 - 字体阴影、文字阴影

news/2024/7/24 13:16:00 标签: Android, 字体阴影, 文字阴影

最近几个月都挺忙,忙着工作,忙着成长…

一直以来我认为在开发中首当其冲的肯定是需求、功能,然后才是UI细节;所以我自己一般不太会去深究一些看不明显的UI,不过这俩日同事提醒我文字有阴影效果,细看之下果然UI设计图中有进行标注

嗯… 对于没接触过的技术,如果开发周期很充裕的话,我还是很愿意去研究,但是如果开发周期很短的话,我就比较反感未知的部分了… 不过有点尴尬的是 文字阴影效果 Android早就已经帮写好了,我们仅需调用几个属性即可…

      • 前置了解
      • 阴影实现
      • 阴影测试
        • 垂直偏移(shadowDx)
        • 水平偏移(shadowDy)
        • 阴影范围(shadowRadius)
      • 源码兴趣

前置了解

看一下UI提供的设计图(如果设计标注中没有标明阴影的偏移量、色值等数据可自行找UI要数值

在这里插入图片描述

有的人可能看到有提供Android的伪代码,其实伪代码没有阴影的设置部分

在这里插入图片描述


阴影实现

Android很早以前就提供了字体阴影的设置方式,关于如何设置文字的阴影效果,主要用到了以下四种阴影属性

  • android:shadowColor 阴影颜色
  • android:shadowDx 阴影水平偏移量
  • android:shadowDy 阴影垂直偏移量
  • android:shadowRadius 阴影范围

实现效果
在这里插入图片描述

实现布局

Tip:在设计标注中一般都是采用的px(像素),可自行设置px或dp看看效果,哪个合适选哪个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="正常字体" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:shadowColor="#687BF3"
        android:shadowDx="0"
        android:shadowDy="2"
        android:shadowRadius="4"
        android:text="阴影字体" />
</LinearLayout>

Look Here:如果仅是为了实现字体阴影的效果,那么看到这里就够了,有兴趣、有时间的可以继续往下看~


如果字体阴影场景比较多的话,也可以在 values - style.xml 写个Style

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="shadowStyle">
        
        <item name="android:shadowColor">#687BF3</item>
        <item name="android:shadowRadius">4</item>
        <item name="android:shadowDx">0</item>
        <item name="android:shadowDy">2</item>
        
    </style>
</resources>

引用方式

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        style="@style/shadowStyle"
        android:text="阴影字体" />

阴影测试

经测试,得结果:水平偏移默认向右,垂直偏移默认向下;偏移值可为正负,不同值显示偏移方向所有不同

垂直偏移(shadowDx)

在这里插入图片描述

xml 片段

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:shadowColor="#687BF3"
        android:shadowDx="0"
        android:shadowDy="22"
        android:shadowRadius="1"
        android:text="阴影字体" />

经过测试,可设垂直偏移负值,显示向上

在这里插入图片描述

xml 片段

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:shadowColor="#687BF3"
        android:shadowDx="0"
        android:shadowDy="-22"
        android:shadowRadius="1"
        android:text="阴影字体" />

水平偏移(shadowDy)

在这里插入图片描述

xml 片段

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:shadowColor="#687BF3"
        android:shadowDx="22"
        android:shadowDy="0"
        android:shadowRadius="1"
        android:text="阴影字体" />

经过测试,可设水平偏移负值,显示向左

在这里插入图片描述

xml 片段

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:shadowColor="#687BF3"
        android:shadowDx="-22"
        android:shadowDy="0"
        android:shadowRadius="1"
        android:text="阴影字体" />

阴影范围(shadowRadius)

经测试,得结果:随着 shadowRadius 设置的越大,阴影效果也越大,但是也会越模糊

Tip:shadowRadius:0 - 1 - 5 - 50 - 500 显示效果

在这里插入图片描述

xml 片段

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="正常字体" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:shadowColor="#687BF3"
        android:shadowDx="0"
        android:shadowDy="22"
        android:shadowRadius="1"
        android:text="阴影字体" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:shadowColor="#687BF3"
        android:shadowDx="0"
        android:shadowDy="22"
        android:shadowRadius="5"
        android:text="阴影字体" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:shadowColor="#687BF3"
        android:shadowDx="0"
        android:shadowDy="22"
        android:shadowRadius="52"
        android:text="阴影字体" />


    <TextView
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:shadowColor="#687BF3"
        android:shadowDx="0"
        android:shadowDy="22"
        android:shadowRadius="522"
        android:text="阴影字体" />
</LinearLayout>

源码兴趣

TextView的自定义属性开始追溯

在这里插入图片描述

values 自定义属性

在这里插入图片描述

TextViewsetShadowLayer 表示将绘制一个阴影,阴影部分不参与交互;同事说明了用到的自定义属性

在这里插入图片描述

PaintsetShadowLayer 表示在主层之下将绘制一个阴影层,范围为0就会移除该阴影层

在这里插入图片描述

PaintsetShadowLayer具体实现

在这里插入图片描述

nSetShadowLayer 好像是调C的方法,具体就不往下深究了

在这里插入图片描述


http://www.niftyadmin.cn/n/380246.html

相关文章

23种设计模式之模板方法模式(Template Method Pattern)

前言&#xff1a;大家好&#xff0c;我是小威&#xff0c;24届毕业生&#xff0c;在一家满意的公司实习。本篇文章将23种设计模式中的模板方法模式&#xff0c;此篇文章为一天学习一个设计模式系列文章&#xff0c;后面会分享其他模式知识。 如果文章有什么需要改进的地方还请大…

ARINC429板卡PCIE-1553B板卡 MIL-STD-1553B总线接口卡 PCIe 1553B通信模块

本系列产品均为自主研发&#xff0c;在有效实现MIL-STD-1553总线功能的同时能为用户的具体应用提供的技术支持。为用户提供了具有友好人机交互界面的总线分析软件&#xff0c;能够满足大多数的通讯、测试及仿真任务要求。本系列产品均为自主研发&#xff0c;在有效实现MIL-STD-…

人工智能-机器学习人工神经网络

机器学习 机器学习部分主要学习的内容是朴素贝叶斯算法和决策树算法。 决策树算法 决策树算法是机器学习中常用的一种分类算法&#xff0c;在决策树中&#xff0c;使用各个属性进行分类&#xff0c;选取每次分类的属性的方式有多种。最简单的是ID3算法&#xff0c;使用信息增…

出学校干了 5 年外包,已经废了

如果不是女朋友和我提分手&#xff0c;我估计现在还没醒悟 本科大专&#xff0c;17年通过校招进入某软件公司做测试&#xff0c;干了接近5年的功能。 今年年初&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落&#xff01;而我已经…

Echarts 的 dispatchAction

一、Echarts中的action echarts中支持的图表行为&#xff0c;通过dispatchAction触发。 1.highlight 高亮指定的数据图形 dispatchAction({ type: highlight, // 可选&#xff0c;系列 index&#xff0c;可以是一个数组指定多个系列 seriesIndex?: number|Array…

迭代器遍历ArrayList

ArrayList 常用遍历方法 1、for 循环遍历 2、增强 for 循环遍历 3、迭代器遍历 迭代器主要用来遍历集合&#xff0c;增强 for 循环底层实现也是迭代器&#xff0c;ListIterator 是更强大的的 Iterator 的字类型 1、只适合于 List 集合的遍历 2、它可以前后双向移动&#xff08…

Spring高手之路——深入理解与实现IOC依赖查找与依赖注入

本文从xml开始讲解&#xff0c;注解后面给出 文章目录 1. 一个最基本的 IOC 依赖查找实例2. IOC 的两种实现方式2.1 依赖查找&#xff08;Dependency Lookup&#xff09;2.2 依赖注入&#xff08;Dependency Injection&#xff09; 3. 在三层架构中的 service 层与 dao 层体会依…

协众信息UI设计怎么提升用户体验呢

随着软件行业的发展兴起的一个新的设计行业。UI设计除了对美观有要求外&#xff0c;还对用户体验有要求&#xff0c;这也是UI设计不同于其他设计的地方。那么&#xff0c;UI设计怎么提升用户体验呢&#xff1f;   1、层次结构   要确保项目设计开始之前&#xff0c;就梳…