Android实现图片添加阴影效果的2种方法

所属分类: 软件编程 / Android 阅读数: 34
收藏 0 赞 0 分享

给图片添加阴影效果,这是很常见的需求。第一种方法是自定义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>

运行即可看到以下效果:

可以看到这种方法绘制出的阴影有虚化效果,多了立体感和层次感,所以更推荐使用。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多