我有一条封闭的路径,我想用一种颜色(白色)填充它并在其周围放置另一种颜色(红色)的边缘。我认为自定义视图类可以让我实现这个目的:
public class StrokeFill extends View{
private Path shapePath;
private Paint strokePaint;
private Paint fillPaint;
public StrokeFill(Context context, Path path) {
super(context);
shapePath = path;
fillPaint.setColor(android.graphics.Color.WHITE);
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setStrokeWidth(0);
strokePaint.setColor(android.graphics.Color.RED);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(3);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
//canvas.drawColor(Color.BLACK);
super.onDraw(canvas);
canvas.drawPath(shapePath, fillPaint);
canvas.drawPath(shapePath, strokePaint);
}
}
在我的主Activity中,我刚刚做了这个(没有XML布局文件):
setContentView(new StrokeFill(this, testpath));
testpath是我在Activity中定义的路径。这是有效的,因为我在使用它定义PathShape时能够绘制它。 但在这种情况下,Eclipse给出了错误java.lang.NullPointerException。我尝试在XML布局中定义自定义视图,但这也不起作用。到目前为止,使用android中的形状一直非常令人沮丧,所以如果有人可以提供帮助就会很棒!
答案 0 :(得分:1)
初始化问题看看这个
public class StrokeFill extends View{
private Path shapePath;
private Paint strokePaint;
private Paint fillPaint;
public StrokeFill(Context context, Path path) {
super(context);
shapePath = new Path();
shapePath = path;
fillPaint = new Paint();
fillPaint.setColor(android.graphics.Color.WHITE);
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setStrokeWidth(0);
strokePaint = new Paint();
strokePaint.setColor(android.graphics.Color.RED);
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(3);
// TODO Auto-generated constructor stub
}
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
//canvas.drawColor(Color.BLACK);
super.onDraw(canvas);
canvas.drawPath(shapePath, fillPaint);
canvas.drawPath(shapePath, strokePaint);
}
}
答案 1 :(得分:0)
strokePaint永远不会在您的代码中初始化...是您的nullPointer吗?
欢迎来到堆栈...如果你发现有用的答案可以投票给他们......如果你认为他们是正确的,给他们绿色的勾选标记!