gpt4 book ai didi

android - 添加阴影(绘图路径)时绘制的不同结果

转载 作者:行者123 更新时间:2023-11-30 02:55:57 26 4
gpt4 key购买 nike

当我有这段代码来添加阴影时:

// set shadow
paint.setShadowLayer(2, 1, 1, Color.BLACK);
// Important for certain APIs
setLayerType(LAYER_TYPE_SOFTWARE, paint);

我明白了:

当我有这段代码来添加阴影时:

// set shadow
paint.setShadowLayer(2, 1, 1, Color.BLACK);

我明白了:

你看得到吗?它在第二张图片上模糊了,但那个饼图看起来应该是这样。另一方面,第一张图片有漂亮的阴影但搞砸了。为什么?

PS:如果我不设置任何阴影饼图,则不会模糊也不会弄乱。但我需要把那个影子放在那里。

编辑:

绘制饼图的类:

  public class MyGraphView extends View {
public static final int PADDING = 4;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
int strokeWidth;
private int[] COLORS = { Color.parseColor("#47B243"),
Color.parseColor("#DB262A") };

// size of bigger half circle
RectF rectf = new RectF(2, 2, 62, 62);
// size of smaller half circle
RectF rectf2 = new RectF(9, 9, 55, 55);
// size of the smallest half circle
RectF rectf3 = new RectF(16, 16, 48, 48);

int temp = 0;

public MyGraphView(Context context, float[] values, int size) {

super(context);
// setting up size of pie chart dynamically
int difference = size / 9;

rectf.set(PADDING, PADDING, size + PADDING, size + PADDING);
rectf2.set(difference + PADDING, difference + PADDING, size
- difference + PADDING, size - difference + PADDING);
rectf3.set(difference * 2 + PADDING, difference * 2 + PADDING, size
- difference * 2 + PADDING, size - difference * 2 +
PADDING);
// setting up brush size
strokeWidth = size / 15;
// assign degrees of agree and disagree to array
value_degree = new float[values.length];
for (int i = 0; i < values.length; i++) {
value_degree[i] = values[i];
}
}

@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);

for (int i = 0; i < value_degree.length; i++) {
// set type of "brush"
paint.setStrokeWidth(strokeWidth);
paint.setStyle(Paint.Style.STROKE);
// set shadow
paint.setShadowLayer(2, 1, 1, Color.BLACK);
// Important for certain APIs
// setLayerType(LAYER_TYPE_SOFTWARE, paint);
// agree
if (i == 0) {
final Path path = new Path();
paint.setColor(COLORS[i]);
// draw 3 paths to show 3 curves
path.addArc(rectf, 180, value_degree[i] - 4);
path.addArc(rectf2, 180, value_degree[i] - 5);
path.addArc(rectf3, 180, value_degree[i] - 6);
// draw the path
canvas.drawPath(path, paint);

// disagree
} else {
temp += (int) value_degree[i - 1];
paint.setColor(COLORS[i]);
final Path path = new Path();
path.addArc(rectf, temp + 180 + 4, value_degree[i] - 4);
path.addArc(rectf2, temp + 180 + 5, value_degree[i] - 5);
path.addArc(rectf3, temp + 180 + 6, value_degree[i] - 6);
// draw the path
canvas.drawPath(path, paint);

}

}
}
}

编辑 2:

当我这样编辑它时:

setLayerType(LAYER_TYPE_SOFTWARE, null);

红色部分一直在那个圆形轨迹中运行绘制。

最佳答案

setShadowLayer 在硬件加速模式下不受支持 see here

因此,您应该使用 setLayerType(LAYER_TYPE_SOFTWARE, null); 将 View 设置为软件,它应该可以正常工作。也许您在绘图时遇到了一些错误?

关于android - 添加阴影(绘图路径)时绘制的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23251779/

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