为什么当我调用 postInvalidate() 时 OnDraw 没有触发?
Why OnDraw is not firing when I call postInvalidate()?
我正在尝试在 Activitie 的布局上绘制 GridView,但未触发 onDraw()
方法。有什么问题吗?
自定义视图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();
}
}
调用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();
}
}
当然还有布局
<?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>
感谢您的帮助,谢谢!
我已经解决了。问题出在无限循环中。它阻止了 UI 线程显示视图。
我正在尝试在 Activitie 的布局上绘制 GridView,但未触发 onDraw()
方法。有什么问题吗?
自定义视图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();
}
}
调用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();
}
}
当然还有布局
<?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>
感谢您的帮助,谢谢!
我已经解决了。问题出在无限循环中。它阻止了 UI 线程显示视图。