1

I am upgradring an Android app from a target version of 2.3 (10) to 4.4 (19) but the Canvas Path draw does not work in the same way - and I can't find any docs saying what has changed.

I it regarding the grey semi transparent overlay - it should be like the first image(Target Android 2.3)

Target: Android 2.3 (10)
Target: Android 2.3 (10)

Target: Android 4.4 (19)
Target: Android 4.4 (19)

Here is the code:

public class ProgramItemBackgroundDrawable extends Drawable{

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int height;
private int width;

private int topViewHeight;
private boolean showBorder;

public ProgramItemBackgroundDrawable(){
    paint.setStyle(Paint.Style.FILL_AND_STROKE);
    paint.setStrokeWidth(1);

    ...
}

public void draw(Canvas canvas) {
    int cornerCutSize = convertDpToPixels(20, context);

    int smallCornerTopMargin = convertDpToPixels(4, context);
    if (cornerCutSize > height)
        cornerCutSize = height - smallCornerTopMargin;

    drawBaseBackgroundColors(canvas, cornerCutSize);

    int margin = convertDpToPixels(3, context);
    paint.setColor(Color.BLACK);
    paint.setAlpha((int) (255 * 0.12));
    Path path = new Path();

    if(showBorder){
        //Marks bottom part of item with space around for the border
        path.moveTo(margin, margin + topViewHeight);
        path.lineTo(margin, height - margin);
        path.lineTo(width - cornerCutSize, height - margin);
        path.lineTo(width - margin, height - cornerCutSize);
        path.lineTo(width - margin, margin + topViewHeight);
        path.close();

        //Marks bottom corner
        path.moveTo(width - cornerCutSize, height);
        path.lineTo(width, height - cornerCutSize);
        path.lineTo(width, height);
        path.close();
    } else {
        //Marks bottom part of item
        path.moveTo(0, margin + topViewHeight);
        path.lineTo(0, height);
        path.lineTo(width, height);
        path.lineTo(width, margin + topViewHeight);
        path.close();

        //Marks bottom corner if it overlaps with the top view area
        if(height < (topViewHeight + margin + cornerCutSize)) {
            int smallCornerCutSize =  Math.abs((height - cornerCutSize) - topViewHeight - margin);

            path.moveTo(width - smallCornerCutSize, margin + topViewHeight);
            path.lineTo(width+1, margin + topViewHeight - smallCornerCutSize);
            path.lineTo(width+1, margin + topViewHeight);
            path.close();
        }
    }

    path.setFillType(Path.FillType.INVERSE_WINDING);
    canvas.drawPath(path, paint);
}

private void drawBaseBackgroundColors(Canvas canvas, int cornerCutSize) {
    ...
}

public static int convertDpToPixels(float dp, Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi/160f);
    return (int) px;
}
}

Any idea what has changed in the way android handles the drawing?

Update 1:
I think it might have something to do with the Hardware Acceleration changes in Api level 14

Morten Holmgaard
  • 7,484
  • 8
  • 63
  • 85

1 Answers1

4

This fixes it:

containingView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

I disable Hardware Acceleration.
But isn't there some drawbacks with this solution?
And how could I get it working with Hardware Accelerations?

Community
  • 1
  • 1
Morten Holmgaard
  • 7,484
  • 8
  • 63
  • 85