/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.snake;
import android.app.Activity;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.TextView;
/*
* 每一种移动开发环境都有自己的基类。如J2ME应用程序的基类是midlets,BREW的基类是applets,
* 而Android程序的基类是Activity。这个activity为我们提供了对移动操作系统的基本功能和事件
* 的访问。这个类包含了基本的构造方法,键盘处理,挂起来恢复功能,以及其他底层的手持设备的访问。
* 实质上,我们的应用程序将是一个Activity类的扩展。
* 一个Activty类是一个简单的启动程序和控制程序的类。
*/
/**
* 它可以根据需要创建界面,但是不是必须。在Android程序中,用户界面是由叫做views类来组织的,一个
* view可以简单理解为可以绘制的对象.
*/
/**
* Snake: a simple game that everyone can enjoy.
*
* This is an implementation of the classic Game "Snake", in which you control a
* serpent roaming around the garden looking for apples. Be careful, though,
* because when you catch one, not only will you become longer, but you'll move
* faster. Running into yourself or the walls will end the game.
*
*/
public class Snake extends Activity {
private SnakeView mSnakeView;
private static String ICICLE_KEY = "snake-view";
/**
* Called when Activity is first created. Turns off the title bar, sets up
* the content views, and fires up the SnakeView.
*
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//可以用setTitle("程序标题");来设置程序的标题
setContentView(R.layout.snake_layout);
/**
* 可以用下面的方法去掉了标题栏
*/
//requestWindowFeature(Window.FEATURE_NO_TITLE);
/**
* 可以用下面的方法实现全屏效果
*/
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//查找设置视图
mSnakeView = (SnakeView) findViewById(R.id.snake);
mSnakeView.setTextView((TextView) findViewById(R.id.text));
//视图状态的为空创建一个新视图,设置为准备状态
if (savedInstanceState == null) {
// We were just launched -- set up a new game
mSnakeView.setMode(SnakeView.READY);
} else {
// We are being restored
//获取资源的信息
Bundle map = savedInstanceState.getBundle(ICICLE_KEY);
if (map != null) {
mSnakeView.restoreState(map);
} else {
//暂停状态
mSnakeView.setMode(SnakeView.PAUSE);
}
}
}
/**
* 暂停
*/
@Override
protected void onPause() {
super.onPause();
// Pause the game along with the activity
mSnakeView.setMode(SnakeView.PAUSE);
}
@Override
public void onSaveInstanceState(Bundle outState) {
//Store the game state
outState.putBundle(ICICLE_KEY, mSnakeView.saveState());
}
}
Android的SDK中Snake代码分析(一)——Snake
最新推荐文章于 2024-10-08 14:09:57 发布