我如何在android中制作像这样的(阴影)回收视图项目?
https://dribbble.com/shots/4020189-Scroll-and-sidebar-interaction
答案 0 :(得分:0)
要添加阴影,可以使用如下代码:
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;
} ... }