开发者

How to set a background bitmap to a rect or rectF?

Is there a way to set a background image for a rectangle drawn in a canvas ? For exemple i have the following onDraw method :

protected void onDraw(Canvas canvas) {
    this.setBackgroundGradient();
    RectF rect = new RectF();
    rect.set(0, 0,开发者_如何学Python canvas.getWidth(), 50);
    canvas.drawRoundRect(rect, 0, 0, this.paint);
}

private void setBackgroundGradient()
{
    this.paint.setShader(new LinearGradient(0, 0,0, getHeight(), 0xff919191, 0xff424242, Shader.TileMode.MIRROR));
}

I would like to change my gradient by a background image (repeatable if possible).

Note : i would rather to keep rectangle and not use drawBitmap.


A Rect is not a drawable, it is a convenience class and only holds the four values that define the rect. Canvas knows how to draw a rect with the Paint object you give it. If you want to have a background (image) instead of a rect, then you either use drawBitmap on the canvas or have a (bitmap)drawable that you pass the canvas to when drawing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜