4

我正在从另一个活动中读取一系列点并从这些点创建一条路径。我有一个名为“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);
}

}

4

1 回答 1

1

您不会onDraw(Canvas canvas)在自定义视图中覆盖正确的方法。正如我所看到的,您将onDraw一个空画布传递给超级。然后,您正在添加路径,但不要使视图无效以绘制您的修改。
而且,其他阻止您的结果可见的事情,您DrawView在调用您的onDraw. 我的猜测是,视图将“重置”其生命周期(onMeasure-> onLayout-> onDraw...),但会进行基础初始化。(我不确定)。因此,您的所有修改都不会被存储。
此外,您不应该在绘制某些东西时分配新对象。每次视图无效时,这将消耗大量内存。

因此,尝试Paint在开始时初始化对象:

private Paint paint;
private Path path;

public DrawCanvas(Context c) {
    super(c);
    init();
}

public DrawCanvas(Context c, AttributesSet a) {
    super(c, a);
    init();
}

private void init() {
    // create your paint object
    paint = new Paint();
    paint.setColor(Color.BLACK);
    ...
}

接下来是您Path的 s 初始化:

public void setPath(Path path) {
    // store your new path
    this.path = path;
    // invalidate the view to call onDraw()
    invalidate();
}

最后,覆盖真实的onDraw方法并绘制你的逻辑:

@Override
public void onDraw(Canvas canvas) {
    if (paint == null || path == null)
        return;

    canvas.drawPath(path, paint);
}

现在您可以按如下方式设置新Path的:onCreate()

setContentView(canvas1);

DrawCanvas draw = new DrawCanvas(this);
canvas1.addView(draw);
draw.setPath(path);

如果您将“重”处理作为 Path 对象,则应考虑调用postInvalidate(),因为invalidate()将在主线程中调用。

于 2017-03-17T08:10:50.297 回答