簡體   English   中英

Android 改變材質高程陰影顏色

[英]Android change Material elevation shadow color

是否可以更改 xml 高程屬性產生的陰影顏色? 我希望通過代碼動態更改陰影。

我知道這個問題很老,可能作者不再需要答案了。 我會把它放在這里,以便其他人可以找到它。

棒棒糖的仰角系統不支持彩色陰影。

但是,如果你需要彩色陰影,可以使用Carbon來獲得它們。 它是Material Design的一種支持庫,在最新版本中有一個改變陰影顏色的選項。 Behance上有很多漂亮的設計,有彩色陰影,我認為盡管在Android中缺少這樣的功能,但還是很好。 重要的是要注意, 所有 Android版本都會模擬彩色陰影,也是5.0+。

https://github.com/ZieIony/Carbon

下面的圖像和代碼可以在Carbon的樣本中找到。

在此輸入圖像描述

碼:

<carbon.widget.LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <carbon.widget.Button
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_margin="@dimen/carbon_padding"
        android:background="#ffffff"
        app:carbon_cornerRadius="2dp"
        app:carbon_elevation="8dp"
        app:carbon_elevationShadowColor="@color/carbon_red_700"/>

</carbon.widget.LinearLayout>

“CardView”:

<carbon.widget.LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <carbon.widget.LinearLayout
        android:layout_width="match_parent"
        android:layout_height="160dp"
        android:layout_margin="@dimen/carbon_margin"
        android:background="#ffffff"
        app:carbon_cornerRadius="2dp"
        app:carbon_elevation="8dp"
        app:carbon_elevationShadowColor="@color/carbon_red_700">

        <carbon.widget.ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:src="@drawable/test_image"/>

        <carbon.widget.TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="test text"/>
    </carbon.widget.LinearLayout>

</carbon.widget.LinearLayout>

啟動API 28(餅圖) View類中提供了#setOutlineAmbientShadowColor(int color)View#setOutlineSpotShadowColor(int color)

如果在View上使用高程,則可以使用這兩種方法更改陰影的顏色。

您可以使用陰影布局 檢查我的答案

我知道我來晚了,但我想分享解決方案,因為我努力搜索這個問題,並且上面批准的方法參考: https://stackoverflow.com/a/42717993/18398843效果不佳,這些陰影顯示小尺寸應用程序中的一些隨機形狀..順便說一句,解決方案是你必須使用“ComplexView”來創建你的自定義陰影,

依賴: implementation 'com.github.BluRe-CN:ComplexView:v1.1'

XML

<com.blure.complexview.ComplexView
   android:id="@+id/shadow_card_1"
   android:layout_width="@dimen/_65sdp"
   android:layout_height="@dimen/_65sdp"
   android:layout_centerInParent="true"
   app:radius="@dimen/_30sdp"
   app:shadow="true"
   app:shadowAlpha="250"
   app:shadowSpread="2"/>

//這將為我的需要創建圓形陰影你可以減少半徑

自定義視圖

val shadow = ComplexView(context)
val radii = floatArrayOf(100f, 100f, 100f, 100f, 100f, 100f, 100f, 100f)//customise according to your requirement
val opacity = 150//customise according to your requirement
 shadow.shadow =Shadow(
       2,
       opacity,
       "#96B9BB",
       GradientDrawable.RECTANGLE,
       radii,
       Shadow.Position.CENTER
      )

val param: RelativeLayout.LayoutParams =
                    RelativeLayout.LayoutParams(
                        context.resources.getDimension(R.dimen._160sdp).toInt(),
                        context.resources.getDimension(R.dimen._160sdp).toInt()
                    )
shadow.layoutParams = param
shadow.addView(yourCustomView)

謝謝:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM