是否可以通过更改xml elevation属性产生的阴影颜色?我希望通过代码动态更改阴影。
是否可以通过更改xml elevation属性产生的阴影颜色?我希望通过代码动态更改阴影。
我知道这个问题很久远,可能作者不再需要答案。但我会把答案留在这里以便其他人能够找到。
Lollipop的高程系统不支持彩色阴影。
但是,如果你需要彩色阴影,可以使用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>
"卡片视图":
<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 (Pie)开始,View#setOutlineAmbientShadowColor(int color) 和 View#setOutlineSpotShadowColor(int color) 可在 View 类中使用。
如果您在 View 上使用 elevation,您可以使用这两种方法来更改阴影的颜色。
我知道我来晚了,但我想分享这个解决方案,因为我为此问题搜索了很久,而上面引用的方法https://dev59.com/k14b5IYBdhLWcg3waA1P#42717993并不好用,这些阴影在应用程序的小尺寸中显示一些随机形状。
顺便说一下,解决方案是您必须使用“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)
谢谢 :)
https://github.com/gabrielemariotti/cardslib/blob/master/doc/SHADOW.md
,我还发现了这个:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.1_r1/frameworks/support/v7/cardview/res/values/colors.xml
。 - Davideashttps://developer.android.com/training/material/shadows-clipping.html
- Davideas