【发布时间】:2018-10-25 20:06:28
【问题描述】:
如何在 android 中制作像这样(阴影)的回收视图项目?
https://dribbble.com/shots/4020189-Scroll-and-sidebar-interaction
【问题讨论】:
-
使用 CardView。
如何在 android 中制作像这样(阴影)的回收视图项目?
https://dribbble.com/shots/4020189-Scroll-and-sidebar-interaction
【问题讨论】:
要添加阴影,您可以使用如下代码:
Paint forShadow = new Paint();
// radius=10, y-offset=2, color=black
forShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);
// in onDraw(Canvas) method
canvas.drawBitmap(bitmap, 0.0f, 0.0f, forShadow);
同样对于 Honeycomb 及以上你需要调用setLayerType(LAYER_TYPE_SOFTWARE, forShadow),否则你将看不到你的影子!
SetShadowLayer 很遗憾不能与硬件加速一起使用,因此会大大降低性能。
完整的代码可能看起来像这样:
public class ShadowImage extends Drawable {
Bitmap bm;
@Override
public void draw(Canvas canvas) {
Paint forShadow = new Paint();
//trying for rectangle
Rect rect = new Rect(0,0,bm.getWidth(), bm.getHeight());
forShadow.setAntiAlias(true);
forShadow.setShadowLayer(5.5f, 4.0f, 4.0f, Color.BLACK);
canvas.drawRect(rect, forShadow);
canvas.drawBitmap(bm, 0.0f, 0.0f, null);
}
public ShadowImage(Bitmap bitmap) {
super();
this.bm = bitmap;
} ... }
【讨论】: