詳解Android如何實現(xiàn)陰影效果
實現(xiàn)形式
elevation
Material Design
提供了View
的陰影效果設(shè)置。主要由兩個屬性決定:elevation和translationZ。
Z = elevation + translationZ
PS:這種實現(xiàn)方式只有API21以及以上才能支持實現(xiàn)。
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
提供的官方控件自身支持設(shè)置陰影效果。陰影實現(xiàn)由cardElevation
和cardMaxElevation
實現(xiàn)。
<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屬性實現(xiàn)陰影效果
<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
屬性文件實現(xiàn)陰影效果。使用layer-list
實現(xiàn)兩層不同背景色實現(xiàn)疊加實現(xiàn)像是陰影的效果,但最終實現(xiàn)效果并不是例如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>
自定義實現(xiàn)
自定義形式是通過自定義Drawable
實現(xiàn),該形式實現(xiàn)目標(biāo)View
必須關(guān)閉硬件加速。自定義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); } }
小結(jié)
實現(xiàn)方式 | 優(yōu)缺點 |
---|---|
elevation | 優(yōu)點:自帶功能實現(xiàn)簡單 缺點:不可自定義顏色 |
CardView | 優(yōu)點:自帶功能實現(xiàn)簡單 缺點:自帶圓角不一定可適配所有需求 |
Textshadow | 優(yōu)點:自帶功能實現(xiàn)簡單 缺點:只可在TextView中使用 |
layer | 優(yōu)點:實現(xiàn)形式簡單 缺點:效果一般 |
自定義實現(xiàn) | 優(yōu)點:實現(xiàn)效果好可配置能力高 缺點:需要開發(fā)者自行開發(fā) |
到此這篇關(guān)于詳解Android如何實現(xiàn)陰影效果的文章就介紹到這了,更多相關(guān)Android陰影效果內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Flutter+Metal實現(xiàn)圖像處理詳細(xì)流程
Flutter使用CVPixelBuffer和iOS交互,我們可以直接使用CVPixelBuffer創(chuàng)建MTLTexture,然后將MTLTexture設(shè)置為渲染目標(biāo),這篇文章主要介紹了Flutter+Metal實現(xiàn)圖像處理,需要的朋友可以參考下2022-06-06Android實現(xiàn)音樂播放進(jìn)度條傳遞信息的兩種方式(在service和activity中)
這篇文章主要介紹了Android:在service和activity之中,實現(xiàn)音樂播放進(jìn)度條傳遞信息的兩種方式,MediaPlayer做音樂播放器采坑以及解決辦法,需要的朋友可以參考下2020-05-05Android指紋識別功能深入淺出分析到實戰(zhàn)(6.0以下系統(tǒng)解決方案)
指紋識別在現(xiàn)實應(yīng)用中已經(jīng)很多了,本篇文章主要介紹了Android指紋識別功能,具有一定的參考價值,有需要的可以了解一下。2016-11-11Kotlin實現(xiàn)網(wǎng)絡(luò)圖片下載和保存功能
根據(jù)Android多線程和網(wǎng)絡(luò)編程的知識講解和案例使用,使用Handler消息機(jī)制實現(xiàn)網(wǎng)絡(luò)圖片下載,并且保存到模擬器中,強(qiáng)化對Android多線程編程、網(wǎng)絡(luò)編程和文件讀寫的理解,這篇文章主要介紹了Kotlin實現(xiàn)網(wǎng)絡(luò)圖片下載和保存功能,需要的朋友可以參考下2023-02-02Android 中ViewPager中使用WebView的注意事項
這篇文章主要介紹了Android 中ViewPager中使用WebView的注意事項的相關(guān)資料,希望通過本文大家在使用過程中遇到這樣的問題解決,需要的朋友可以參考下2017-09-09Android提高之自定義Menu(TabMenu)實現(xiàn)方法
這篇文章主要介紹了Android自定義Menu(TabMenu)實現(xiàn)方法,是非常實用的功能,需要的朋友可以參考下2014-08-08Android如何實現(xiàn)設(shè)備的異顯功能詳解
這篇文章主要給大家介紹了關(guān)于Android如何實現(xiàn)設(shè)備的異顯功能的相關(guān)資料,這篇文章通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-02-02