Android升级2.3 - > 4.4:画布路径绘图无法正常/更改

时间:2014-10-18 10:12:16

标签: android drawing upgrade android-4.4-kitkat

我正在将Android应用程序从目标版本2.3(10)升级到4.4(19),但Canvas Path绘图不能以相同的方式工作 - 我找不到任何文档说明发生了什么变化。< / p>

我关于灰色半透明叠加 - 它应该像第一个图像(Target Android 2.3)

目标:Android 2.3(10)
Target: Android 2.3 (10)

目标:Android 4.4(19)
Target: Android 4.4 (19)

以下是代码:

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;
}
}

知道android处理绘图的方式有什么变化吗?

更新1:
我认为它可能与Hardware Acceleration changes in Api level 14

有关

1 个答案:

答案 0 :(得分:4)

This fixes it:

containingView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

我禁用硬件加速。
但这个解决方案是否存在一些缺点?
我怎样才能使用硬件加速?