Artur Siwek
Artur Siwek

Reputation: 55

Android : Switching and update layout

I have the application that contains the game hangman. I have created a separate activity on their responsibility for drawing. Here's the code:

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();
    }

How do I switch between the various stages of drawing from another activity which the code is below:

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);

Upvotes: 1

Views: 174

Answers (1)

Piotr Chojnacki
Piotr Chojnacki

Reputation: 6857

You should call invalidate() method on your DrawWisielec view:

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

According to Android documentation:

public void invalidate () Added in API level 1

Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().

It will call again onDraw() method, which you are interested in. Note that this must be called from UI thread.

Upvotes: 2

Related Questions