gpt4 book ai didi

安卓升级 2.3 -> 4.4 : Canvas path drawing not working/changed

转载 作者:行者123 更新时间:2023-11-29 15:58:19 25 4
gpt4 key购买 nike

我正在将一个 Android 应用程序从目标版本 2.3 (10) 升级到 4.4 (19),但是 Canvas 路径绘制的工作方式不同 - 我找不到任何说明发生了什么变化的文档。

关于灰色半透明覆盖层 - 它应该像第一张图片(目标 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 有关

最佳答案

This fixes it:

containingView.setLayerType(View.LAYER_TYPE_SOFTWARE, null)

我禁用了硬件加速。
但是这个解决方案不是有一些缺点吗?
我怎样才能让它与硬件加速一起工作?

关于安卓升级 2.3 -> 4.4 : Canvas path drawing not working/changed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26438602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com