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