开发者

draw background (checkerboard) java android

I am fairly new to java and android, but am looking for a good tutorial that will get on the right track. I want to write a function that draws/redra开发者_运维技巧ws a black on white checkerboard pattern to the background of my app's current view. I don't want to use an actual image, as i want to be able to redraw the background on resize events, ect. and i want to dynamically change the size of the squares..

Thanks for any help


The most efficient way to do it is to have a Bitmap containing a checkerboard pattern (for instance 2x2 squares) and use a BitmapShader with the REPEAT tile mode:

BitmapShader shader = new BitmapShader(bitmapTemplate, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
Paint paint = new Paint();
paint.setShader(shader);

// in your draw method

canvas.drawRect(x, y, width, height, paint);


I required this and needed it to be a little bit more flexible than supplying my own bitmap resources. So here is a on-the-fly option.

Call this method in your onCreate() or constructor methods and save the paint. Then simply use the paint during fill operations in your onDraw().

private Paint createCheckerBoard(int pixelSize)
{
    Bitmap bitmap = Bitmap.createBitmap(pixelSize * 2, pixelSize * 2, Bitmap.Config.ARGB_8888);

    Paint fill = new Paint(Paint.ANTI_ALIAS_FLAG);
    fill.setStyle(Paint.Style.FILL);
    fill.setColor(0x22000000);

    Canvas canvas = new Canvas(bitmap);
    Rect rect = new Rect(0, 0, pixelSize, pixelSize);
    canvas.drawRect(rect, fill);
    rect.offset(pixelSize, pixelSize);
    canvas.drawRect(rect, fill);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setShader(new BitmapShader(bitmap, BitmapShader.TileMode.REPEAT, BitmapShader.TileMode.REPEAT));
    return paint;
}


I've made a custom Drawable which can draw the background checkerboard, see this github repo - CheckerboardDrawable.

You can use it like this:

yourView.setBackgroundDrawable(CheckerboardDrawable.create());

and it's customizable:

CheckerboardDrawable drawable = new CheckerboardDrawable.Builder()
        .colorOdd(Color.LTGRAY)
        .colorEven(Color.DKGRAY)
        .size(20)
        .build();
yourView.setBackgroundDrawable(drawable);

the output will be:

draw background (checkerboard) java android

Hope it can help.


Kotlin, based on Simon answer above,

fun createCheckerBoard(tileSize: Int) = Paint(Paint.ANTI_ALIAS_FLAG).apply {
    shader = BitmapShader(Bitmap.createBitmap(tileSize * 2, tileSize * 2, Bitmap.Config.ARGB_8888).apply {
        Canvas(this).apply {
            val fill = Paint(Paint.ANTI_ALIAS_FLAG).apply { style = Paint.Style.FILL; color = 0x22000000 }
            drawRect(0f, 0f, tileSize.toFloat(), tileSize.toFloat(), fill)
            drawRect(tileSize.toFloat(), tileSize.toFloat(), tileSize * 2f, tileSize * 2f, fill)
        }
    }, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT)
}


load 2 images: black and white

create a 2d array chessboard[8][8] out of a class which will contain the drawable and the size

create a 2d loop

for (i < 8)
 for (j < 8)
  draw with canvas (chessboard[i][j].drawable) 

keywords for you to google: 2d arrays, drawing with canvas

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜