Home » Android » android – Shadow layer work with the emulator but not on an actual device

android – Shadow layer work with the emulator but not on an actual device

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m creating a custom drawable (it extends from Drawable) and i’m trying to put a shadow effect on it.

Here’s a part of my code :

public void draw(Canvas canvas) {
    Path path = new Path();
    path.moveTo(0, 0);
    path.lineTo(0, Y/2);
    path.lineTo(X/2, Y);
    path.lineTo(X, Y/2);
    path.lineTo(X, 0);
    path.lineTo(0, 0);
    Paint paint = new Paint();
    paint.setColor(context.getResources().getColor(R.color.red_dark));
    paint.setStyle(Style.FILL_AND_STROKE);
    paint.setStrokeWidth(2f);
    paint.setShadowLayer(1, 0, 10f, context.getResources().getColor(R.color.black));
    canvas.drawPath(path, paint);
}

I don’t know why but it put a shadow with the same color, i double checked everything and i don’t see where the problem is. I test it on a galaxy nexus. But on the emulator it works just fine.

How to&Answers:

I’m not sure, but this maybe because of hardware acceleration. setShadowLayer doen’t work if view accelerated. Try to disable acceleration for whole app and check. Read this.

Unsupported Drawing Operations

setShadowLayer(): works with text only

Use a software layer type to force a view to be rendered in software. If a view that is hardware accelerated (for instance, if your whole application is hardware acclerated), is having rendering problems, this is an easy way to work around limitations of the hardware rendering pipeline.

Use setLayerType to set layer type to individual views or turn off acceleration in your manifest for whole app.