android自定义视图中的DrawPath
我有一个封闭的路径,我想用一种颜色(白色)填充它,然后用另一种颜色(红色)的边围绕它。我认为自定义视图类将允许我实现此目的:android自定义视图中的DrawPath,android,canvas,Android,Canvas,我有一个封闭的路径,我想用一种颜色(白色)填充它,然后用另一种颜色(红色)的边围绕它。我认为自定义视图类将允许我实现此目的: public class StrokeFill extends View{ private Path shapePath; private Paint strokePaint; private Paint fillPaint; public StrokeFill(Context context, Path path) { super(context);
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);
}
}
在我的主要活动中,我只是这样做了(没有XML布局文件):
testpath是我在活动中定义的路径。它是有效的,因为我在使用它定义PathShape时能够绘制它。
但在本例中,Eclipse向我提供了错误java.lang.NullPointerException。我尝试在XML布局中定义自定义视图,但这也不起作用。到目前为止,在安卓系统中使用形状一直非常令人沮丧,所以如果有人能帮上忙,那就太好了 strokePaint从未在代码中初始化。。。那是你的空指针吗
欢迎来到stack。。。如果你发现答案有用,就投票给他们。。。如果你认为他们是正确的,给他们绿色的复选标记 strokePaint从未在代码中初始化。。。那是你的空指针吗
欢迎来到stack。。。如果你发现答案有用,就投票给他们。。。如果你认为他们是正确的,给他们绿色的复选标记 初始化有问题看看这个
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);
}
}
你的初始化有问题看看这个
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);
}
}
就是这样。有很多小细节需要注意…谢谢!就是这样。有很多小细节需要注意…谢谢!