我正在开发一个简单的游戏引擎,并且看到了invalidate()问题。基本上,当我尝试使()来自任何地方的视图无效但默认的Activity方法如果无法调用onDraw()。有什么想法吗?
public class GameActivity extends Activity
{
//Name for my app
String sTag = "CloneOut";
//Variable to point to my view
View myView;
//Variable controlling the running of the game
boolean bRunning = true;
//Fixed framerate variables
long desiredFramerate = 1;
long targetTimeMillis;
//Creates a new MusicManager object to handle all music
MusicManager mp = new MusicManager(this);
//Creates a new SoundEffectManager object to handle all sound effects
SoundEffectManager sp = new SoundEffectManager(this);
//Create new int variables to hold SoundEffectManager sound ids
//int iBeer;
//Create an array of bitmaps to store all the actors in the game
Bitmap[] actor = new Bitmap[10];
//Automatically called when an activity begins
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Critical line that sets teh view of this activity to BitmapView and
// thus BitmapView.draw is called to repaint the view
setContentView(new BitmapView(this));
// Show the Up button in the action bar.
setupActionBar();
myView = findViewById(android.R.id.content);
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar()
{
//Action bar only exists in API 11+ so check before displaying
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
class BitmapView extends View
{
public BitmapView(Context context)
{
super(context);
}
@Override
public void onDraw(Canvas canvas)
{
//Writes an informational message to the LogCat window
Log.i(sTag,"Drawing");
//Draw the background first
canvas.drawColor(Color.BLACK);
//Draw all the actors in the game
canvas.drawBitmap(actor[0], 500, 100, null);
}
}
//-----------------------------------------------------------------------------------------
//Game Engine Code Start
//-----------------------------------------------------------------------------------------
//The following methods run automatically in this order
// onCreate() - Call immediately on starting an activity
// onStart()
// onResume() - Called when the activity starts interacting with the user
public void onResume()
{
super.onResume();
//Populate the first actor with the paddle
actor[0] = BitmapFactory.decodeResource(getResources(), R.drawable.paddle);
//Calculate the time at which to render the next frame
targetTimeMillis = System.currentTimeMillis() + (1000 / desiredFramerate);
//Start the game running
GameLoop();
}
public void GameLoop()
{
while (bRunning)
{
CheckFramerate();
}
}
public void CheckFramerate()
{
if (System.currentTimeMillis() > targetTimeMillis)
{
//Calculate the time at which to render the next frame
targetTimeMillis = System.currentTimeMillis() + (1000L / desiredFramerate);
//Writes an informational message to the LogCat window
Log.i(sTag,"Renderframe");
RenderFrame();
}
}
//Invalidates the entire view and thus forces a redraw of the screen
public void RenderFrame()
{
//Writes an informational message to the LogCat window
Log.i(sTag,"Inside Renderframe");
myView.invalidate();
}
}