亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Android如何實現(xiàn)陰影效果

 更新時間:2022年06月20日 11:43:05   作者:JulyYu  
這篇文章主要為大家詳細(xì)介紹了Android是如何實現(xiàn)陰影效果的,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下

實現(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"
    />

官網(wǎng)介紹

image.png

CardView屬性

CardViewAndroid提供的官方控件自身支持設(shè)置陰影效果。陰影實現(xiàn)由cardElevationcardMaxElevation實現(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配置文件

通過配置xmllayer屬性文件實現(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+Metal實現(xiàn)圖像處理詳細(xì)流程

    Flutter使用CVPixelBuffer和iOS交互,我們可以直接使用CVPixelBuffer創(chuàng)建MTLTexture,然后將MTLTexture設(shè)置為渲染目標(biāo),這篇文章主要介紹了Flutter+Metal實現(xiàn)圖像處理,需要的朋友可以參考下
    2022-06-06
  • Android實現(xiàn)音樂播放進(jìn)度條傳遞信息的兩種方式(在service和activity中)

    Android實現(xiàn)音樂播放進(jìn)度條傳遞信息的兩種方式(在service和activity中)

    這篇文章主要介紹了Android:在service和activity之中,實現(xiàn)音樂播放進(jìn)度條傳遞信息的兩種方式,MediaPlayer做音樂播放器采坑以及解決辦法,需要的朋友可以參考下
    2020-05-05
  • Android指紋識別功能深入淺出分析到實戰(zhàn)(6.0以下系統(tǒng)解決方案)

    Android指紋識別功能深入淺出分析到實戰(zhàn)(6.0以下系統(tǒng)解決方案)

    指紋識別在現(xiàn)實應(yīng)用中已經(jīng)很多了,本篇文章主要介紹了Android指紋識別功能,具有一定的參考價值,有需要的可以了解一下。
    2016-11-11
  • 分享幾個Android開發(fā)有用的程序代碼

    分享幾個Android開發(fā)有用的程序代碼

    本文主要是給大家分享了幾個常用而且很實用的程序代碼片段,都是個人項目中提取出來的,有需要的小伙伴可以直接拿走使用
    2015-02-02
  • Kotlin實現(xiàn)網(wǎng)絡(luò)圖片下載和保存功能

    Kotlin實現(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-02
  • 深入理解Android 5.0中的Toolbar

    深入理解Android 5.0中的Toolbar

    相信大家都有所體會,搜索Toolbar相關(guān)文章滿天飛,但是大都不是很全面,每次要用到的時候又要重頭過濾一遍。而且隨著版本升級很多較早的文章的方法已經(jīng)失效,最近剛好好用到Toolbar,就將相關(guān)配置整理下,方便以后需要的時候或者有需要的朋友們參考學(xué)習(xí)。
    2017-01-01
  • Android Touch事件分發(fā)深入了解

    Android Touch事件分發(fā)深入了解

    這篇文章主要為大家詳細(xì)介紹了Android Touch事件分發(fā),內(nèi)容很詳細(xì),感興趣的朋友可以參考一下
    2016-04-04
  • Android 中ViewPager中使用WebView的注意事項

    Android 中ViewPager中使用WebView的注意事項

    這篇文章主要介紹了Android 中ViewPager中使用WebView的注意事項的相關(guān)資料,希望通過本文大家在使用過程中遇到這樣的問題解決,需要的朋友可以參考下
    2017-09-09
  • Android提高之自定義Menu(TabMenu)實現(xiàn)方法

    Android提高之自定義Menu(TabMenu)實現(xiàn)方法

    這篇文章主要介紹了Android自定義Menu(TabMenu)實現(xiàn)方法,是非常實用的功能,需要的朋友可以參考下
    2014-08-08
  • Android如何實現(xiàn)設(shè)備的異顯功能詳解

    Android如何實現(xiàn)設(shè)備的異顯功能詳解

    這篇文章主要給大家介紹了關(guān)于Android如何實現(xiàn)設(shè)備的異顯功能的相關(guān)資料,這篇文章通過示例代碼介紹的非常詳細(xì),對各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2022-02-02

最新評論