我正在从另一个活动中读取一系列点并从这些点创建一条路径。我有一个名为“DrawCanvas”的自定义视图,它扩展了 View。目前代码正在投影一个空白的白色画布。有人可以帮我理解为什么画布没有绘制路径吗?
PreviewActivity.java
public class Preview extends Activity{
Canvas newCanvas = new Canvas();
MyPointsList object;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getIntentData();
Path path = new Path();
if(object.getArrayList().get(0) != null){
for(int i = 0; i < object.getArrayList().size(); i++){
path.moveTo(object.getMyPoint(i).getX(), object.getMyPoint(i).getY());
path.lineTo(object.getMyPoint(i).getX(), object.getMyPoint(i).getY());
}
}
FrameLayout canvas1 = new FrameLayout(this);
DrawCanvas view;
view = new DrawCanvas(Preview.this);
view.onDraw(newCanvas, path);
canvas1.addView(view);
setContentView(canvas1);
}
public void getIntentData(){
Intent i = this.getIntent();
if(i.hasExtra("parcel")){
Bundle b = getIntent().getExtras();
object = b.getParcelable("parcel");
if(object != null){
Log.d("Debug", object.getArrayList().get(5).getX() + " " + object.getArrayList().get(5).getY());
}
else if(object == null){
Log.d("Debug", "Object is equal to Null");
}
}
}
画布.java
public class DrawCanvas extends View {
public DrawCanvas(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public void onDraw(Canvas canvas, Path path){
super.onDraw(canvas);
canvas.drawARGB(255,255,255,255);
Paint black = new Paint();
black.setColor(Color.BLACK);
black.setStyle(Paint.Style.STROKE);
black.setStrokeJoin(Paint.Join.ROUND);
black.setStrokeCap(Paint.Cap.ROUND);
black.setStrokeWidth(3);
canvas.drawPath(path, black);
}
}