Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android make custom layout shape [closed]

I'm trying to make custom ViewGroup layout shapes, that the layout be star or heart, triangles or even make the lines diagonal.

So the the goal is to make collage view for two or more ImageViews. triangle layouts

hearts layouts

Could someone give me a library or a way to do this trick?

like image 969
Marwan Zakariya Avatar asked Jul 12 '15 23:07

Marwan Zakariya


1 Answers

You can achieve this by using standard methods of Canvas and BitmapShader.

First method could look like this:

@Override protected void onDraw(Canvas canvas) {
    //bitmap 01
    canvas.save(Canvas.CLIP_SAVE_FLAG);
    canvas.clipPath(shape01);
    canvas.drawBitmap(bitmap01, 0, 0, paint);
    canvas.restore();

    //bitmap 02
    canvas.save(Canvas.CLIP_SAVE_FLAG);
    canvas.clipPath(shape02);
    canvas.drawBitmap(bitmap02, 0, 0, paint);
    canvas.restore();
}

The second could look like this:

BitmapShader shader01 = new BitmapShader(bitmap01, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
BitmapShader shader02 = new BitmapShader(bitmap02, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint01 = new Paint();
Paint paint02 = new Paint();
paint01.setShader(shader01);
paint02.setShader(shader02);

@Override protected void onDraw(Canvas canvas) {
    //bitmap 01
    canvas.drawPath(path01, paint01);

    //bitmap 02
    canvas.drawPath(path02, paint02);
}

So you need to define your shapes, load bitmaps and draw them.

like image 53
eleven Avatar answered Oct 20 '22 03:10

eleven