Android 更改 Material 高程阴影颜色
Android change Material elevation shadow color
是否可以更改 xml 高度 属性 产生的阴影颜色?
我希望通过代码动态更改阴影。
我知道这个问题很老了,可能作者不再需要这个答案了。我就把它留在这里,以便其他人可以找到它。
Lollipop 的高度系统不支持彩色阴影。
但是,如果您需要彩色阴影,可以使用 Carbon 获得它们。它是 Material 设计的一种支持库,在最新版本中有一个选项可以更改阴影颜色。 Behance 上有大量具有彩色阴影的漂亮设计,尽管 Android 中缺少此类功能,但我认为拥有它们会很好。重要的是要注意 all Android 版本以及 5.0+ 版本也会模拟彩色阴影。
https://github.com/ZieIony/Carbon
下面的图片和代码可以在Carbon的samples中找到。
代码:
<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) and View#setOutlineSpotShadowColor(int color) 在视图 class 中可用。
如果您在 View 上使用高程,则可以使用这两种方法来更改阴影的颜色。
您可以使用 Shadow Layout. Check my answer.
我知道我来晚了,但我想分享解决方案,因为我努力搜索这个问题,上面 ref 批准的方法: 效果不佳,这些阴影显示一些随机形状在小应用程序中..
顺便说一句,解决方案是您必须使用“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)
谢谢:)
是否可以更改 xml 高度 属性 产生的阴影颜色? 我希望通过代码动态更改阴影。
我知道这个问题很老了,可能作者不再需要这个答案了。我就把它留在这里,以便其他人可以找到它。
Lollipop 的高度系统不支持彩色阴影。
但是,如果您需要彩色阴影,可以使用 Carbon 获得它们。它是 Material 设计的一种支持库,在最新版本中有一个选项可以更改阴影颜色。 Behance 上有大量具有彩色阴影的漂亮设计,尽管 Android 中缺少此类功能,但我认为拥有它们会很好。重要的是要注意 all Android 版本以及 5.0+ 版本也会模拟彩色阴影。
https://github.com/ZieIony/Carbon
下面的图片和代码可以在Carbon的samples中找到。
代码:
<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) and View#setOutlineSpotShadowColor(int color) 在视图 class 中可用。
如果您在 View 上使用高程,则可以使用这两种方法来更改阴影的颜色。
您可以使用 Shadow Layout. Check my answer.
我知道我来晚了,但我想分享解决方案,因为我努力搜索这个问题,上面 ref 批准的方法:
依赖性: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)
谢谢:)