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

时间:2014-04-23 17:54:40

标签: android

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

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

我明白了:

http://i59.tinypic.com/vhxbib.png

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

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

我明白了:

http://i59.tinypic.com/qryfyp.png

你能看到吗?它在第二张图片上模糊不清,但那个饼图看起来应该看起来像。另一方面,第一张照片有很好的阴影,但是搞砸了。为什么呢?

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

        }

    }
}
}

EDIT2:

当我像这样编辑它时:

setLayerType(LAYER_TYPE_SOFTWARE, null);

红色部分继续以圆圈轨迹绘制。

2 个答案:

答案 0 :(得分:1)

硬件加速模式see here

不支持

setShadowLayer

因此,您应该使用setLayerType(LAYER_TYPE_SOFTWARE, null);将视图设置为软件,它应该可以正常运行。也许你在绘画中遇到了一些错误?

答案 1 :(得分:0)

我发现了。应该在我将图形分配给线性布局的活动中调用方法setLayerType(LAYER_TYPE_SOFTWARE, null);,结果是:

myLinearLayout.setLayerType(LAYER_TYPE_SOFTWARE, null);