|
Android example source code file (DivideAndConquerActivity.java)
The DivideAndConquerActivity.java Android example source code
/*
* Copyright (C) 2008 Google Inc.
*
* 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.google.android.divideandconquer;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Color;
import java.util.Stack;
/**
* The activity for the game. Listens for callbacks from the game engine, and
* response appropriately, such as bringing up a 'game over' dialog when a ball
* hits a moving line and there is only one life left.
*/
public class DivideAndConquerActivity extends Activity
implements DivideAndConquerView.BallEngineCallBack,
NewGameCallback,
DialogInterface.OnCancelListener {
private static final int NEW_GAME_NUM_BALLS = 1;
private static final double LEVEL_UP_THRESHOLD = 0.8;
private static final int COLLISION_VIBRATE_MILLIS = 50;
private boolean mVibrateOn;
private int mNumBalls = NEW_GAME_NUM_BALLS;
private DivideAndConquerView mBallsView;
private static final int WELCOME_DIALOG = 20;
private static final int GAME_OVER_DIALOG = 21;
private WelcomeDialog mWelcomeDialog;
private GameOverDialog mGameOverDialog;
private TextView mLivesLeft;
private TextView mPercentContained;
private int mNumLives;
private Vibrator mVibrator;
private TextView mLevelInfo;
private int mNumLivesStart = 5;
private Toast mCurrentToast;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Turn off the title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mBallsView = (DivideAndConquerView) findViewById(R.id.ballsView);
mBallsView.setCallback(this);
mPercentContained = (TextView) findViewById(R.id.percentContained);
mLevelInfo = (TextView) findViewById(R.id.levelInfo);
mLivesLeft = (TextView) findViewById(R.id.livesLeft);
// we'll vibrate when the ball hits the moving line
mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
}
/** {@inheritDoc} */
public void onEngineReady(BallEngine ballEngine) {
// display 10 balls bouncing around for visual effect
ballEngine.reset(SystemClock.elapsedRealtime(), 10);
mBallsView.setMode(DivideAndConquerView.Mode.Bouncing);
// show the welcome dialog
showDialog(WELCOME_DIALOG);
}
@Override
protected Dialog onCreateDialog(int id) {
if (id == WELCOME_DIALOG) {
mWelcomeDialog = new WelcomeDialog(this, this);
mWelcomeDialog.setOnCancelListener(this);
return mWelcomeDialog;
} else if (id == GAME_OVER_DIALOG) {
mGameOverDialog = new GameOverDialog(this, this);
mGameOverDialog.setOnCancelListener(this);
return mGameOverDialog;
}
return null;
}
@Override
protected void onPause() {
super.onPause();
mBallsView.setMode(DivideAndConquerView.Mode.PausedByUser);
}
@Override
protected void onResume() {
super.onResume();
mVibrateOn = PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(Preferences.KEY_VIBRATE, true);
mNumLivesStart = Preferences.getCurrentDifficulty(this).getLivesToStart();
}
private static final int MENU_NEW_GAME = Menu.FIRST;
private static final int MENU_SETTINGS = Menu.FIRST + 1;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_NEW_GAME, MENU_NEW_GAME, "New Game");
menu.add(0, MENU_SETTINGS, MENU_SETTINGS, "Settings");
return true;
}
/**
* We pause the game while the menu is open; this remembers what it was
* so we can restore when the menu closes
*/
Stack<DivideAndConquerView.Mode> mRestoreMode = new Stack
Other Android examples (source code examples)Here is a short list of links related to this Android DivideAndConquerActivity.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.