I am trying out AndEngine and cannot work out why my splashscreen is not centered in the device - see the image below.
I have set a default size for the device (800, 480) and placed the splash at camera.getWidth() / 2. It seems like the corner of my splash hits the center pretty spot on but I want the center of the splash to be centered - makes sense?
MainActivity:
package com.example.caspe.getmeout;
import org.andengine.engine.Engine;
import org.andengine.engine.LimitedFPSEngine;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.WakeLockOptions;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.engine.options.EngineOptions;
import org.andengine.entity.scene.Scene;
import java.io.IOException;
public class MainActivity extends BaseGameActivity {
private ResourcesManager resourcesManager;
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private Camera camera;
public EngineOptions onCreateEngineOptions() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(800, 480), this.camera);
engineOptions.getAudioOptions().setNeedsMusic(true).setNeedsSound(true);
engineOptions.setWakeLockOptions(WakeLockOptions.SCREEN_ON);
return engineOptions;
}
#Override
public Engine onCreateEngine(EngineOptions pEngineOptions) {
return new LimitedFPSEngine(pEngineOptions, 60);
}
#Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {
ResourcesManager.prepareManager(mEngine, this, camera, getVertexBufferObjectManager());
resourcesManager = ResourcesManager.getInstance();
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException {
SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
}
#Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws IOException {
mEngine.registerUpdateHandler(new TimerHandler(2f, new ITimerCallback() {
public void onTimePassed(final TimerHandler pTimerHandler) {
mEngine.unregisterUpdateHandler(pTimerHandler);
// load menu resources, create menu scene
// set menu scene using scene manager
// disposeSplashScene();
}
}));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
And the code for Splash:
package com.example.caspe.getmeout;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.util.GLState;
import com.example.caspe.getmeout.BaseScene;
import com.example.caspe.getmeout.SceneManager.SceneType
public class SplashScene extends BaseScene {
private Sprite splash;
#Override
public void createScene() {
splash = new Sprite(0,0, resourcesManager.splash_region, vbom) {
#Override
protected void preDraw(GLState pGLState, Camera pCamera) {
super.preDraw(pGLState, pCamera);
pGLState.enableDither();
}
};
splash.setScale(1.5f);
splash.setPosition(camera.getWidth() / 2, camera.getHeight() / 2);
attachChild(splash);
}
#Override
public void onBackKeyPressed() {
}
#Override
public SceneType getSceneType() {
return SceneType.SCENE_SPLASH;
}
#Override
public void disposeScene() {
splash.detachSelf();
splash.dispose();
this.detachSelf();
this.dispose();
}
}
Please let me know if you need more code than this.
I am not directly seeking a working piece of code but more where my error is so I can use my braincells and work it out - I have just stared myself blind here.
splash.setPosition(camera.getWidth() / 2, camera.getHeight() / 2);
That is not enough. Your splash is actually centered correctly: the top left corner is in the center.
What you need is to subtract the half width and height of the splash from the position, too.
// untested code
splash.setPosition(camera.getWidth() / 2 - splash.getWidth() / 2, camera.getHeight() / 2 - splash.getHeight() / 2);
Related
I use libgdx have one player which moves in x direction from left to right. Now I want the camera to follow it (like in Flappy Bird for example).
What happen is that the player go out off screen when it reaches the right border of screen and the camera don't follow him.
I have tried following options but none of them worked:
camera.position.set(player.getX(), camera.position.y, 0);
camera.position.set(player.getX(), 0, 0);
Vector3 vector3= camera.unproject(new Vector3(player.getX(), 0f, 0f));
player.setX(vector3.x);
I know that this question already exists on SO but none answer works in this case. Maybe I miss something important that i don't know.
The code:
Game.java class
public class Game extends com.badlogic.gdx.Game implements ApplicationListener {
public static Vector2 VIEWPORT = new Vector2(320, 480);
public static int WIDTH;
public static int HEIGHT;
#Override
public void create() {
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
// VIEWPORT = new Vector2(WIDTH/2, HEIGHT/2);
VIEWPORT = new Vector2(WIDTH, HEIGHT);
setScreen(new GameScreen(this));
}
#Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
super.resize(width, height);
}
#Override
public void resume() {
}
#Override
public void pause() {
}
}
GameScreen.java class
import android.util.Log;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
import java.util.Collections;
public class GameScreen implements Screen {
private OrthographicCamera camera;
private Player player;
private PlayerInputHandler inputHandler1, inputHandler2;
private Sound sound;
FitViewport viewp;
public static int WIDTH;
public static int HEIGHT;
int width_spacing = 0;
int height_spacing = 0;
Stage stage;
Skin skin;
public GameScreen(Game game) {
stage = new Stage(new FitViewport(Game.WIDTH, Game.HEIGHT));
camera = (OrthographicCamera) stage.getCamera();
Gdx.input.setInputProcessor(stage);
}
#Override
public void show() {
resetGame();
}
public void resetGame() {
WIDTH = Gdx.graphics.getWidth();
HEIGHT = Gdx.graphics.getHeight();
width_spacing = Game.WIDTH / 24;
height_spacing = Game.HEIGHT / 14;
stage.clear();
skin = new Skin(Gdx.files.internal("data2/uiskin.json"));
prepareInputHandlers();
prepare_stage();
}
public void addPlayer() {
Texture texture = new Texture("player.png");
player = new Player(texture);
player.setPosition(Game.WIDTH / 2, Game.HEIGHT * 2 / 3);
stage.addActor(player);
}
#Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (delta > 1 / 60f) {
player.setX(player.getX() + (4 * delta));
camera.position.set(player.getX(), camera.position.y, 0);
}
update();
stage.act(delta);
stage.draw();
}
private void update() {
camera.update();
}
private void prepareInputHandlers() {
inputHandler2 = new PlayerInputHandler(player, Input.Keys.LEFT, Input.Keys.RIGHT, Input.Keys.UP, Input.Keys.DOWN);
}
#Override
public void dispose() {
sound.dispose();
player.getTexture().dispose();
}
public void prepare_stage() {
addPlayer();
player.setWidth(64);
player.setHeight(64);
}
#Override
public void resume() {
// TODO Auto-generated method stub
}
#Override
public void hide() {
// TODO Auto-generated method stub
}
#Override
public void pause() {
// TODO Auto-generated method stub
}
}
Player.java class
import com.badlogic.gdx.graphics.Texture;
public class Player extends com.badlogic.gdx.scenes.scene2d.ui.Image{
private Texture texture;
public Player(Texture texture) {
super(texture);
}
public Texture getTexture() {
return texture;
}
#Override
public void act(float delta) {
super.act(delta);
}
}
try to use stage's camera instead of creating your own. Change your GameScreen constructor like this:
public GameScreen(Game game)
{
stage = new Stage(new FitViewport(Game.WIDTH, Game.HEIGHT));
camera = (OrthographicCamera) stage.getCamera();
Gdx.input.setInputProcessor(stage);
}
then in the render method set the camera position just like
camera.position.set(player.getX(), camera.position.y, 0);
before camera.update() call
There is also something strange in your code - why you have camera.position.set() and setting player x in this condition:
if (delta > 1 / 60f) //??
{
player.setX(player.getX() + (4 * delta));
camera.position.set(player.getX(), camera.position.y, 0);
}
I'm pretty sure you don't need this - but even if you need the
camera.position.set(player.getX(), camera.position.y, 0);
should be out of if statement because what is happening now is that even if you are changing your player position (using keybord through PlayerInputHandler object) you **don't update camera position. Try to remove if statement or at least do something like
public void render(float delta)
{
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (delta > 1 / 60f)
{
player.setX(player.getX() + (100 * delta));
}
camera.position.set(player.getX(), camera.position.y, 0);
update();
stage.act(delta);
stage.draw();
}
last thing is that if your stage is empty (excluding player) when camera will start to follow player - you will see as player not moving at all :) Add someting more to stage
I am new in libGDX and making a simple game like bomber-man.I want to move a sprite in accordance with my touch. for example , if i touch below the image it have to move downside,if i touch in right side of image or sprite it should move in right direction. this should be happened continuously i.e when i touch screen any where,sprite should move continuously and when i release the touch,it should stop there.
Thanks in advance..
here is my code
package com.kamal.bomberman;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.input.GestureDetector.GestureListener;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.sun.org.apache.bcel.internal.generic.RETURN;
public class Bomberman implements ApplicationListener {
private SpriteBatch batch;
private Texture textureBg,textureBman;
private TextureRegion regionBg , regionBman;
float x = 5 , y = 630 ;
#Override
public void create() {
textureBg = new Texture(Gdx.files.internal("data/bg.png"));
textureBman = new Texture(Gdx.files.internal("data/bm.png"));
regionBman = new TextureRegion(textureBman,0,0,256,512);
regionBg = new TextureRegion(textureBg,6,5,256,192);
batch = new SpriteBatch();
}
#Override
public void dispose() {
}
#Override
public void render() {
batch.begin();
batch.draw(regionBg,0,0,800,1280);
batch.draw(textureBman,x,y,300,300);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
I am developing a scene manager with AndEngine and am getting a GLMatrixStackOverflowExceptionerror:
E/AndroidRuntime(1906): FATAL EXCEPTION: GLThread 130
E/AndroidRuntime(1906): org.andengine.opengl.util.GLMatrixStack$GLMatrixStackOverflowException
E/AndroidRuntime(1906): at org.andengine.opengl.util.GLMatrixStack.glPushMatrix(GLMatrixStack.java:94)
E/AndroidRuntime(1906): at org.andengine.opengl.util.GLState.pushProjectionGLMatrix(GLState.java:574)
E/AndroidRuntime(1906): at org.andengine.entity.scene.Scene.onManagedDraw(Scene.java:243)
E/AndroidRuntime(1906): at org.andengine.entity.Entity.onDraw(Entity.java:1348)
E/AndroidRuntime(1906): at org.andengine.entity.Entity.onManagedDraw(Entity.java:1585)
E/AndroidRuntime(1906): at org.andengine.entity.scene.Scene.onManagedDraw(Scene.java:259)
2 Classes below: GameActivity and SceneManager
The problem occurs in GameActivity on this line:
sceneManager.setCurrentScene(SceneType.MENU); // ???
What am I doing wrong?
package com.example;
import java.io.IOException;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import android.util.Log;
import com.example.scenemanager.SceneManager;
import com.example.scenemanager.SceneManager.SceneType;
/**
* Example of using a SceneManager with GLES2 AnchorCenter
*/
public class GameActivity extends BaseGameActivity {
Scene mScene;
protected static final int CAMERA_WIDTH = 800;
protected static final int CAMERA_HEIGHT = 480;
BitmapTextureAtlas playerTexture;
ITextureRegion playerTextureRegion;
SceneManager sceneManager;
Camera mCamera;
#Override
public EngineOptions onCreateEngineOptions() {
mCamera = new Camera (0,0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = (new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT) , mCamera));
return engineOptions;
}
#Override
public void onCreateResources(
OnCreateResourcesCallback pOnCreateResourcesCallback)
throws IOException {
// mEngine is from superclass
sceneManager = new SceneManager(this, mEngine, mCamera);
sceneManager.loadSplashSceneResources();
// let the game engine know we are done loading the resources we need
pOnCreateResourcesCallback.onCreateResourcesFinished();
}
#Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
throws IOException {
pOnCreateSceneCallback.onCreateSceneFinished(sceneManager.createSplashScene());
}
#Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback)
throws IOException {
mEngine.registerUpdateHandler(new TimerHandler(3.0f, new ITimerCallback() { // 3 seconds to load all stuff
#Override
public void onTimePassed(TimerHandler pTimerHandler) {
Log.d("onPopulateScene()", "onTimePassed()");
// unregister so as only do this once
mEngine.unregisterUpdateHandler(pTimerHandler);
// done displaying splash, so go to menu
sceneManager.loadMenuSceneResources();
sceneManager.createMenuScene();
mScene.registerUpdateHandler(new IUpdateHandler() {
#Override
public void reset() { }
#Override
public void onUpdate(final float pSecondsElapsed) {
Log.d("onPopulateScene()", "onUpdate()");
sceneManager.setCurrentScene(SceneType.MENU); // ???
}
});
}
}));
pOnPopulateSceneCallback.onPopulateSceneFinished();
}
}
package com.example.scenemanager;
import org.andengine.engine.Engine;
import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
public class SceneManager {
private SceneType currentScene;
private BaseGameActivity activity;
private Engine engine;
private Camera camera;
private Scene splashScene;
private Scene menuScene;
private Scene gameScene;
private BitmapTextureAtlas splashTextureAtlas;
private TextureRegion splashTextureRegion;
private BitmapTextureAtlas menuTextureAtlas;
private TextureRegion menuTextureRegion;
private float xPosition;
private float yPosition;
public enum SceneType
{
SPLASH,
MENU,
GAME
}
/**
* constructor
*
* #param baseGameActivity
* #param engine
* #param camera
*/
public SceneManager(BaseGameActivity baseGameActivity, Engine engine, Camera camera) {
this.activity = baseGameActivity;
this.engine = engine;
this.camera = camera;
xPosition = camera.getWidth()/2;
yPosition = camera.getHeight()/2;
}
public void loadSplashSceneResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
splashTextureAtlas = new BitmapTextureAtlas(this.activity.getTextureManager(),256, 256);
splashTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashTextureAtlas, this.activity, "splash.png", 0, 0);
splashTextureAtlas.load();
}
public void loadMenuSceneResources() {
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
menuTextureAtlas = new BitmapTextureAtlas(this.activity.getTextureManager(),256, 256);
menuTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(menuTextureAtlas, this.activity, "menu.png", 0, 0);
menuTextureAtlas.load();
}
public void loadGameSceneResources() {
}
public Scene createSplashScene() {
// Set background color and add the splash image
splashScene = new Scene();
splashScene.setBackground(new Background(0, 1, 0)); // green
Sprite splash = new Sprite(0, 0, splashTextureRegion, activity.getVertexBufferObjectManager());
splash.setPosition(xPosition, yPosition);
splashScene.attachChild(splash);
return splashScene;
}
public Scene createMenuScene() {
menuScene = new Scene();
menuScene.setBackground(new Background(0,0,0));
Sprite sprite = new Sprite (0, 0, menuTextureRegion, engine .getVertexBufferObjectManager());
sprite.setPosition(xPosition, yPosition);
menuScene.attachChild(menuScene);
return menuScene;
}
public void createGameScene() {
//Create the Main Game Scene and set background colour to blue
gameScene = new Scene();
gameScene.setBackground(new Background(0, 0, 1));
}
public SceneType getCurrentScene() {
return currentScene;
}
public void setCurrentScene(SceneType scene) {
if (scene != currentScene) {
currentScene = scene;
switch (scene)
{
case SPLASH:
break;
case MENU:
engine.setScene(menuScene);
break;
case GAME:
engine.setScene(gameScene);
break;
default:
break;
}
}
}
}
I have been trying to implement libgdx without a core project in a new eclipse project, but I keep getting:
The method initialize(ApplicationListener,
AndroidApplicationConfiguration) in the type AndroidApplication is not
applicable for the arguments (AndroidApplication,
AndroidApplicationConfiguration)
The code I use is quite simple at the moment:
package com.debels.androidapplication;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import android.os.Bundle;
public class MainActivity extends AndroidApplication{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
initialize(new AndroidApplication(), cfg);
}
}
and
package com.debels.androidapplication;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
//import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
//import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.debels.androidapplication.screens.MainMenuScreen;
public class AndroidApplication extends Game{
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
private FPSLogger fps;
#Override
public void create(){
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
//texture = new Texture(Gdx.files.internal("data/libgdx.png"));
//texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
//TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
/*sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);*/
setScreen(new MainMenuScreen(this));
fps = new FPSLogger();
}
#Override
public void dispose() {
super.dispose();
}
#Override
public void render() {
super.render();
/*fps.log();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();*/
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void pause() {
super.pause();
}
#Override
public void resume() {
super.resume();
}
}
Thats because you are sending a (gdx)AndroidApplication instance to the ini instead of a (gdx)AndroidApplicationListener (which is any class that extends Game or implements ApplicationListener).
You get all confused because you named that class AndroidApplication...
Change this:
initialize(new AndroidApplication(), cfg);
to this:
initialize(new com.debels.androidapplication.AndroidApplication(), cfg);
Or better yet, change the name of that class.
Also dont forget to copy the gdx.jar to the android project libs folder.
I am developing a game for android. Between stages, i want to show a part of a map with a route, and move it from a city to city (stage to stage).
First i want to do it on my phone, this a Samsung Galaxy Y, 240x320 Qvga ldpi.
So i have the map file in jpg format. This picture is 2463x602, this is a world map.
I did it, everything is done except one thing. This "animation" is slow for me.
When i start with this, i thought, it will be so fast, and i will handle the speed with a Thread.sleep(); but the matter is, it is not fast.
How can i make it more faster this?
Here is my code:
package hu.mycompany.myproject;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MapActivity extends Activity {
DrawView drawView;
SoundPool soundPool;
int soundId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundId = soundPool.load(this, R.raw.airplane, 1);
drawView = new DrawView(this);
setContentView(drawView);
}
public void playSound() {
soundPool.play(soundId, 1f, 1f, 1, 0, 1f);
}
#Override
public void onResume() {
super.onResume();
playSound();
drawView.resume();
}
#Override
public void onPause() {
super.onPause();
drawView.pause();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_map, menu);
return true;
}
public class DrawView extends SurfaceView implements Runnable {
Bitmap gameMap = null;
Thread gameLoop = null;
SurfaceHolder surface;
Rect rect;
volatile boolean running;
volatile boolean moved;
public DrawView(Context context) {
super(context);
moved = false;
surface = getHolder();
gameMap = BitmapFactory.decodeResource(getResources(),
R.drawable.map);
}
public void resume() {
running = true;
gameLoop = new Thread(this);
gameLoop.start();
}
public void pause() {
running = false;
while (true) {
try {
gameLoop.join();
} catch (InterruptedException e) {
}
}
}
#Override
public void run() {
Rect canvasSize = new Rect(0, 0, 240, 320);
Paint paint = new Paint();
paint.setAntiAlias(true);
while (running) {
if (!surface.getSurface().isValid()) {
continue;
}
if (!moved) {
int i;
for (i = 80; i <= 830; i++) {
Canvas canvas = surface.lockCanvas();
rect = new Rect(i, 250, (i + 240), 570);
canvas.drawBitmap(gameMap, rect, canvasSize, paint);
surface.unlockCanvasAndPost(canvas);
}
moved = true;
}
}
}
}
}
1-try to reduce picture size by exporting it save for web in photoshop and select png as extension type
2- You should create two class one for handling thread and other for rendering and game logic. The thread will manage it by locking canvas when system is rendering. Take a look at this link
http://android-er.blogspot.com/2010/05/android-surfaceview.html