详解Android如何实现阴影效果
作者:JulyYu 时间:2022-04-14 13:11:16
实现形式
elevation
Material Design
提供了View
的阴影效果设置。主要由两个属性决定:elevation和translationZ。
Z = elevation + translationZ
PS:这种实现方式只有API21以及以上才能支持实现。
elevation
属性表示View
高度加上高度就会有阴影效果。 translationZ
属性表示给View
增加一个Z轴的变换效果。配合elevation
属性一起使用阴影效果更突出。
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_margin="15dp"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@android:color/holo_blue_bright"
android:elevation="10dp"
android:translationZ="10dp"
android:paddingBottom="10dp"
/>
官网介绍
CardView属性
CardView
是Android
提供的官方控件自身支持设置阴影效果。阴影实现由cardElevation
和cardMaxElevation
实现。
<androidx.cardview.widget.CardView
android:layout_margin="15dp"
android:layout_width="match_parent"
android:layout_height="100dp"
android:outlineAmbientShadowColor="@android:color/holo_blue_bright"
android:outlineSpotShadowColor="@android:color/holo_red_dark"
app:cardElevation="5dp"
app:cardMaxElevation="10dp"
/>
shadow属性
若是TextView
则可以通过shadow属性实现阴影效果
<TextView
android:id="@+id/test_shadow"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:shadowColor="#aa22ff22"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Test Shadow"
android:textColor="#cc000000"
android:textSize="60sp" />
layer配置文件
通过配置xml
的layer
属性文件实现阴影效果。使用layer-list
实现两层不同背景色实现叠加实现像是阴影的效果,但最终实现效果并不是例如CardView
的渐变阴影效果。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 阴影图片,android:left表示阴影图片左边到背景图片左边的距离
android:top表示阴影图片上边到背景图片上边的距离-->
<item android:left="5dp"
android:top="5dp">
<shape>
<solid android:color="#60000000"/>
</shape>
</item>
<!-- 背景图片,android:right表示阴影图片右边到背景图片右边的距离
android:bottom表示阴影图片下边到背景图片下边的距离-->
<item android:bottom="5dp"
android:right="5dp">
<shape>
<solid android:color="#000000"/>
</shape>
</item>
</layer-list>
自定义实现
自定义形式是通过自定义Drawable
实现,该形式实现目标View
必须关闭硬件加速。自定义Drawable
主要通过重写draw
方法绘制矩形或圆形形状增加阴影效果。
@Override
public void draw(@NonNull Canvas canvas) {
if (mBgColor != null) {
if (mBgColor.length == 1) {
mBgPaint.setColor(mBgColor[0]);
} else {
mBgPaint.setShader(new LinearGradient(mRect.left, mRect.height() / 2, mRect.right,
mRect.height() / 2, mBgColor, null, Shader.TileMode.CLAMP));
}
}
if (mShape == SHAPE_ROUND) {
canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mShadowPaint);
canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mBgPaint);
} else {
canvas.drawCircle(mRect.centerX(), mRect.centerY(), Math.min(mRect.width(), mRect.height())/ 2, mShadowPaint);
canvas.drawCircle(mRect.centerX(), mRect.centerY(), Math.min(mRect.width(), mRect.height())/ 2, mBgPaint);
}
}
完整版代码
小结
实现方式 | 优缺点 |
---|---|
elevation | 优点:自带功能实现简单 缺点:不可自定义颜色 |
CardView | 优点:自带功能实现简单 缺点:自带圆角不一定可适配所有需求 |
Textshadow | 优点:自带功能实现简单 缺点:只可在TextView中使用 |
layer | 优点:实现形式简单 缺点:效果一般 |
自定义实现 | 优点:实现效果好可配置能力高 缺点:需要开发者自行开发 |
来源:https://juejin.cn/post/7110975396253794335
标签:Android,阴影
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android Activity通用悬浮可拖拽View封装的思路详解
2023-08-08 15:31:48
Android 快速实现防止网络重复请求&按钮重复点击的方法
2023-08-29 17:31:45
java微信支付接入流程详解
2023-07-28 18:37:46
![](https://img.aspxhome.com/file/2023/2/82992_0s.png)
Java网络编程之IO模型阻塞与非阻塞简要分析
2021-08-10 18:10:25
Spring @Transaction 注解执行事务的流程
2022-12-30 15:41:23
![](https://img.aspxhome.com/file/2023/6/81906_0s.jpg)
解析Java的Hibernate框架中的持久化类和映射文件
2023-08-19 15:00:05
浅谈java中守护线程与用户线程
2023-11-26 20:46:41
![](https://img.aspxhome.com/file/2023/3/60173_0s.png)
java调用外部程序的方法及代码演示
2023-11-13 22:42:55
Android简单音乐播放实例
2023-04-13 05:49:55
![](https://img.aspxhome.com/file/2023/4/138364_0s.png)
Java 中责任链模式实现的三种方式
2023-11-08 14:32:31
![](https://img.aspxhome.com/file/2023/5/58915_0s.png)
利用Spring Session和redis对Session进行共享详解
2021-10-18 18:22:45
![](https://img.aspxhome.com/file/2023/3/105943_0s.png)
Android App如何防止抓包
2023-01-03 05:20:13
Android下拉列表spinner的实例代码
2023-07-31 20:39:47
![](https://img.aspxhome.com/file/2023/2/86012_0s.jpg)
Android BottomSheet实现可拉伸控件
2023-07-05 15:07:51
详细解读Java编程中面向字符的输入流
2023-09-27 10:39:21
![](https://img.aspxhome.com/file/2023/8/108938_0s.jpg)
浅谈C#指针问题
2022-07-19 17:06:00
IntelliJ IDEA快速创建getter和setter方法
2023-06-04 00:44:46
![](https://img.aspxhome.com/file/2023/0/61650_0s.jpg)
Java代理深入讲解之静态代理
2022-10-25 14:56:12
![](https://img.aspxhome.com/file/2023/6/122016_0s.png)
举例讲解Java的Spring框架中AOP程序设计方式的使用
2022-11-21 21:22:15
Android破解微信获取聊天记录和通讯录信息(静态方式)
2023-03-05 04:27:31
![](https://img.aspxhome.com/file/2023/3/118963_0s.png)