Search code examples
androidviewdrawandroid-custom-viewondraw

Why OnDraw is not firing when I call postInvalidate()?


I'm trying to draw GridView on Activitie's layout, but onDraw() method is not being triggered. What can be wrong?

Custom view class

package com.example.twinkle94.lab_work;

import android.content.Context;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.widget.GridView;

public class GameView extends GridView
{
    private int background_color;
    private int player_color;
    private int wall_color;

    private int[][] game_field;

    private GameViewAdapter adapter;

    public GameView(Context context)
    {
        super(context);

        setWillNotDraw(false);
    }

    public GameView(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);

        setWillNotDraw(false);

        background_color = ContextCompat.getColor(context, R.color.background_color);
        player_color = ContextCompat.getColor(context, R.color.player_color);
        wall_color = ContextCompat.getColor(context, R.color.wall_color);

        game_field = new int[3][3];

        adapter = new GameViewAdapter(context);
        this.setAdapter(adapter);

        //TODO: change to dynamic!
        setNumColumns(3);

        //TODO: change to dynamic!
        game_field[0][0] = player_color;
        game_field[0][1] = player_color;
        game_field[0][2] = wall_color;
        game_field[1][0] = background_color;
        game_field[1][1] = wall_color;
        game_field[1][2] = player_color;
        game_field[2][0] = wall_color;
        game_field[2][1] = background_color;
        game_field[2][2] = player_color;
    }

    public GameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);

        setWillNotDraw(false);
    }

    public void onUpdate()
    {

    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);

        for(int i = 0; i < game_field.length; i++)
        {
            for(int j = 0; j < game_field[i].length; j++)
            {
                adapter.add(game_field[i][j]);
                adapter.update();
            }
        }
    }

    public void draw()
    {
        postInvalidate();
    }
}

Place where I'm calling draw.

package com.example.twinkle94.lab_work;


import android.content.Context;

public class Game
{
    private Context context;
    private GameView gameView;

    public Game(Context context)
    {
        this.context = context;
        gameView = (GameView) ((MainActivity)context).findViewById(R.id.gameView);
    }

    public void run()
    {
        while(true)
        {
            gameView.draw();

            try
            {
                Thread.sleep(1000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
}

Activity class

package com.example.twinkle94.lab_work;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new Game(this).run();
    }


}

And layout, of course

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.twinkle94.lab_work.MainActivity">

    <com.example.twinkle94.lab_work.GameView
        android:id="@+id/gameView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>

Appreciate your help, Thanks!


Solution

  • I've solved it. The problem was in infinite while cycle. It blocked a UI thread from showing the view.