Paint mShadow = new Paint();
// radius=10, y-offset=2, color=black
mShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);
// in onDraw(Canvas)
canvas.drawBitmap(bitmap, 0.0f, 0.0f, mShadow);
希望这个有用。
注**
别忘了对于Honeycomb和更高版本,你需要调用setLayerType(LAYER_TYPE_SOFTWARE, mShadow),否则你将看不到你的影子!(@Dmitriy_Boichenko) 不幸的是,SetShadowLayer无法与硬件加速配合使用,因此大大降低了性能(@Matt Wear) 答案取自Here 对于大于21的Api,您可以尝试在imageview或Button中使用xml:Read here at developer site
7条答案
按热度按时间brtdzjyr1#
我们也可以使用CardView,它提供圆角背景和阴影。要使用它,您需要将v7 CardView库作为依赖项添加到build.gradle中的项目,如下所示。
注:将上一行中的
23.0.1
更改为相关版本。所以我用
CardView
包围了ImageView
,使阴影如下图所示。它会在图像周围添加阴影。
注:我不知道这是否是一个好的变通方案。我是一个初学者。我试图实现
CardView
,这给了一个想法,以实现同样的这一点。如果它是不好的,请更新我的原因。vkc1a9a22#
这是从罗曼盖伊的演示文稿在Devoxx,pdf找到here。
希望这个有用。
别忘了对于Honeycomb和更高版本,你需要调用
setLayerType(LAYER_TYPE_SOFTWARE, mShadow)
,否则你将看不到你的影子!(@Dmitriy_Boichenko)不幸的是,
SetShadowLayer
无法与硬件加速配合使用,因此大大降低了性能(@Matt Wear)答案取自Here
对于大于21的Api,您可以尝试在imageview或Button中使用xml:Read here at developer site
cbjzeqam3#
创建文件shadow_rect. xml
并在Imageview中将其用作android:background="@drawable/shadow_rect。
sr4lhrrt4#
图像视图
背景可绘制对象定义为圆角矩形:
vhipe2zx5#
你可以创建一个图层列表,并把你的两个项目(绘图)在它的图像和你的阴影。
您的阴影项目的位置和内容可能会根据您想要应用阴影的位置(顶部,左侧,右侧,左右两侧等)和阴影的样式而改变。
p1tboqfb6#
可以使用属性
outlineSpotShadowColor
要将阴影设置为true,请尝试
setOutlineSpotShadowColor
和@color/black
,并将elevation
增加到12dp将阴影设置为false
setOutlineSpotShadowColor
与@android:color/transparent
并减小elevation
i2byvkas7#
CardView
是完美的解决方案,但如果你不想使用,那么你可以使用XML层。这里我应用阴影的图像第四侧。android:right android:left android:top android:bottom
的值在布局上,你必须使用它作为背景,
android:elevation
将给予一个灯光样式。为了避免裁剪,你可以使用android:layout_margin
。