Android實(shí)現(xiàn)圖片添加陰影效果的2種方法
給圖片添加陰影效果,這是很常見的需求。第一種方法是自定義drawable,使用layer-list定義兩個(gè)圖片,代碼如下:
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中定義一個(gè)textview作為待顯示控件,將show_view.xml設(shè)為這個(gè)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>
運(yùn)行程序顯示效果如下:
看著還可以,但是這里面有一個(gè)缺陷,大家細(xì)看就會(huì)發(fā)現(xiàn)這個(gè)陰影是實(shí)邊的,沒有虛化的效果,這樣就不夠真實(shí),影響用戶體驗(yàn)。下面我們來看第二種方法。
第二種方式就是自定義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軸大?。簆aram3: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>
運(yùn)行即可看到以下效果:
可以看到這種方法繪制出的陰影有虛化效果,多了立體感和層次感,所以更推薦使用。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義流式布局/自動(dòng)換行布局實(shí)例
這篇文章主要介紹了Android自定義流式布局/自動(dòng)換行布局實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android實(shí)現(xiàn)輪播圖無限循環(huán)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)輪播圖無限循環(huán)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Android技巧一之啟動(dòng)屏+新功能左右導(dǎo)航邏輯
這篇文章主要介紹了Android技巧一之啟動(dòng)屏+新功能左右導(dǎo)航邏輯的相關(guān)資料,需要的朋友可以參考下2016-01-01Android關(guān)于Glide的使用(高斯模糊、加載監(jiān)聽、圓角圖片)
這篇文章主要為大家詳細(xì)介紹了Android關(guān)于Glide的使用,內(nèi)容豐富,高斯模糊、加載監(jiān)聽、圓角圖片希望大家可以掌握,感興趣的小伙伴們可以參考一下2016-11-11Android自定義時(shí)間軸的實(shí)現(xiàn)過程
這篇文章主要介紹了Android自定義時(shí)間軸的實(shí)現(xiàn)過程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android 表情面板和軟鍵盤切換時(shí)跳閃問題的解決方法
這篇文章主要介紹了Android 表情面板和軟鍵盤切換時(shí)跳閃問題的解決方法,需要的朋友可以參考下2017-08-08