Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compose multiple shapes and add emboss?

Tags:

android

The goal is to create a composite shape and add an emboss effect. I can successfully create the shape as illustrated below.

    woodPaint = new Paint();
    woodPaint.setAntiAlias(true);
    woodBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.wood);
    woodShader = new BitmapShader(woodBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
    woodPaint.setShader(woodShader);

    ...

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawCircle(handleX, radius, radius, woodPaint);
        canvas.drawRoundRect(baseRectF, 25, 25, woodPaint);

        super.onDraw(canvas);
    }

Image:

Then I add an EmbossMaskFilter

    paintEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.1f, 8f, 5f);
    woodPaint.setMaskFilter(paintEmboss);

Image:

As you can see the emboss mask is applied to the two shapes separately. How can I compose the shapes together and apply the emboss to the entire object? I've tried setting the xfer mode to some flavor off porter duff but this doesn't effect the fact that the emboss mask is applied to each shape separately.

Thanks for any help!

Edit: As illustrated by Orabîg, you must draw one path with the paint that you've set an emboss filter on. NOTE: the method setMaskFilter() is one of the handful of methods that don't work when hardware acceleration is turned on. I resolved an issue I was having with a phone running jelly bean by disabling hardware acceleration for the activity. You can disable hardware acceleration at any level you choose:

  • Application
  • Activity
  • Window
  • View

Cheers!

like image 404
Chris Allen Avatar asked Nov 26 '25 19:11

Chris Allen


1 Answers

Well, you need only one emboss effect, so you should ony draw one shape.

So you should use the Canvas.drawPath() method.

You'll just have to define a Path object, with the following methods : Path definition steps Start by defining 3 RectF objects, which will be the bounding boxes of the left-most round (imagine the circle behind it), the right-most one, and the sliding one : boxes definitions You'll maybe have to do some additional maths to determine the correct angles to use for box2 (they depend on the respective size of the circle and the whole rectangle)

Good luck !

like image 157
Orabîg Avatar answered Nov 28 '25 07:11

Orabîg