1

我有包含游戏刽子手的应用程序。我针对他们的绘画责任创建了一个单独的活动。这是代码:

public class DrawWisielec extends View
{
    Paint paint;
    int choose;
    public DrawWisielec(Context context,int choose) 
    {
        super(context);
        this.choose = choose;
    }

    protected void onDraw(Canvas canvas)
    {
        canvas.drawRGB(0,0,0);
        Paint Circle = new Paint();
        Paint paint = new Paint();
        Circle.setARGB(255,255,255,255);
        Circle.setStyle(Paint.Style.STROKE);
        Circle.setStrokeWidth(5);
        paint.setARGB(255, 255, 255, 255);
        paint.setStrokeWidth(5);
        switch(choose)
        {
        case 1:
            {
                canvas.drawLine(50, 400, 100, 300, paint);
                break;
            }
        case 2:
            {
            canvas.drawLine(100, 300, 150, 400, paint);
            break;
            }
        case 3: canvas.drawLine(100, 300, 100, 50, paint);
        case 4:canvas.drawLine(100,50,300,50,paint);
        case 5:canvas.drawLine(300,50,300,100,paint);
        case 6:canvas.drawCircle(300, 150, 50, Circle);
        case 7:canvas.drawLine(300, 200, 300, 250, paint);
        case 8:canvas.drawLine(300, 250, 250, 200, paint);
        case 9:canvas.drawLine(300, 250, 350, 200, paint);
        case 10:canvas.drawLine(300, 250, 275, 330, paint);
        case 11:canvas.drawLine(300, 250, 325, 330, paint);
        }
        invalidate();
    }

如何在代码如下的另一个活动的绘图的各个阶段之间切换:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_mode_wisielec);

    DrawLayout = (LinearLayout)findViewById(R.id.WisielecDrawLayout);
    DrawWisielec draw = new DrawWisielec(this, choose);
    DrawLayout.addView(draw);
4

1 回答 1

2

您应该在DrawWisielec视图上调用 invalidate() 方法:

draw.setChoose(2); // Change stage of drawing to for example 2   
draw.invalidate(); // Redraw view

根据Android文档:

public void invalidate () 在 API 级别 1 中添加

使整个视图无效。如果视图可见,onDraw(android.graphics.Canvas) 将在未来的某个时间点被调用。这必须从 UI 线程调用。要从非 UI 线程调用,请调用 postInvalidate()。

它将再次调用onDraw()您感兴趣的方法。请注意,这必须从 UI 线程调用。

于 2013-03-23T08:18:19.787 回答