Android实现图片添加阴影效果的2种方法
作者:至浊至愚 时间:2022-10-31 06:46:55
给图片添加阴影效果,这是很常见的需求。第一种方法是自定义drawable,使用layer-list定义两个图片,代码如下:
show_view.xml:
<?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>
<corners android:radius="25dp"/>
<solid android:color="#60000000"/>
</shape>
</item>
<!-- 背景图片,android:right表示阴影图片右边到背景图片右边的距离
android:bottom表示阴影图片下边到背景图片下边的距离-->
<item android:bottom="5dp"
android:right="5dp">
<shape>
<corners android:radius="25dp"/>
<solid android:color="#000000"/>
</shape>
</item>
</layer-list>
在main.xml中定义一个textview作为待显示控件,将show_view.xml设为这个testview的背景,main.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.liusiyutaloner.frescotest.MainActivity">
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@drawable/shadow_view"/>
</RelativeLayout>
运行程序显示效果如下:
看着还可以,但是这里面有一个缺陷,大家细看就会发现这个阴影是实边的,没有虚化的效果,这样就不够真实,影响用户体验。下面我们来看第二种方法。
第二种方式就是自定义view,代码里通过setShadowLayer绘制图片阴影,代码如下:
CustomShadowView类:
package com.example.liusiyutaloner.frescotest;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class CustomShadowView extends View {
private Paint mPaint;
public CustomShadowView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//绘制阴影,param1:模糊半径;param2:x轴大小:param3:y轴大小;param4:阴影颜色
mPaint.setShadowLayer(10F, 15F, 15F, Color.GRAY);
RectF rect = new RectF(0 , 0, 200, 200);
canvas.drawRoundRect(rect, (float)75, (float)75, mPaint);
}
}
再将CustomShadowView类加到main.xml中,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="150dp"
android:layout_height="150dp"
tools:context="com.example.liusiyutaloner.frescotest.MainActivity">
<com.example.liusiyutaloner.frescotest.CustomShadowView
android:layout_gravity="center"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_centerHorizontal="true" />
</RelativeLayout>
运行即可看到以下效果:
可以看到这种方法绘制出的阴影有虚化效果,多了立体感和层次感,所以更推荐使用。
来源:http://blog.csdn.net/u011421608/article/details/51923812
标签:Android,图片,阴影
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈@FeignClient中name和value属性的区别
2023-11-06 13:04:14
![](https://img.aspxhome.com/file/2023/0/78270_0s.png)
Java并发问题之乐观锁与悲观锁
2022-07-13 19:59:05
![](https://img.aspxhome.com/file/2023/1/84201_0s.png)
SpringCloud用Zookeeper搭建配置中心的方法
2022-12-17 18:16:18
浅谈android Fragment横竖屏翻转对重新加载的要求
2023-07-27 21:55:28
Spring Cloud Gateway 如何修改HTTP响应信息
2021-07-20 13:33:40
java递归实现汉诺塔步骤介绍
2022-03-03 09:44:17
![](https://img.aspxhome.com/file/2023/1/64411_0s.png)
使用Nexus搭建Maven私服教程的方法步骤
2023-03-05 18:56:58
![](https://img.aspxhome.com/file/2023/3/66203_0s.jpg)
Java实战之用Swing实现通讯录管理系统
2021-08-15 08:24:09
![](https://img.aspxhome.com/file/2023/5/71905_0s.jpg)
springboot中bean的加载顺序问题
2022-01-04 19:55:57
![](https://img.aspxhome.com/file/2023/0/62510_0s.png)
C# 面向对象三大特性:封装、继承、多态
2022-09-15 17:00:53
Spring实战之XML与JavaConfig的混合配置详解
2023-07-11 18:07:20
![](https://img.aspxhome.com/file/2023/0/85430_0s.png)
Spring Boot整合Mybatis并完成CRUD操作的实现示例
2023-11-09 04:36:46
![](https://img.aspxhome.com/file/2023/7/59377_0s.png)
JAVA技术实现上传下载文件到FTP服务器(完整)
2023-08-26 15:35:43
计算字符串和文件MD5值的小例子
2023-12-10 20:31:19
Redis缓存策略超详细讲解
2023-11-08 06:20:10
![](https://img.aspxhome.com/file/2023/8/66078_0s.png)
java 定时器线程池(ScheduledThreadPoolExecutor)的实现
2023-03-31 20:52:10
Java利用数组随机抽取幸运观众如何实现
2022-03-20 11:51:02
![](https://img.aspxhome.com/file/2023/9/78249_0s.png)
spring cloud gateway中如何读取请求参数
2021-08-19 16:19:02
![](https://img.aspxhome.com/file/2023/7/84217_0s.png)
mybatis 集合嵌套查询和集合嵌套结果的区别说明
2022-10-12 15:17:02
![](https://img.aspxhome.com/file/2023/8/71858_0s.png)
java中LinkedList使用迭代器优化移除批量元素原理
2021-12-05 11:26:07
![](https://img.aspxhome.com/file/2023/0/66180_0s.png)