On Jul 7, 7:07 pm, Dianne Hackborn <[email protected]> wrote:
> This isn't an intrinsic problem -- apps like YouTube have a surface view and
> show the menu fine. And there isn't enough information here to provide much
> help.
Yes, several other examples I've looked at, including the LunarLander
game does this, so it was my expectation too that this should work.
I've compared just about everything I can think of with the
LunarLander sourcecode, but nothing of consequence has popped up yet.
I've made one more observation: onWindowFocusChanged in my SurfaceView
gets called only when the MENU button works, or in other words, only
after having selected my game/app from the press-and-hold-home-button
menu. If I then hit one of the DPAD buttons, which my SurfaceView
listens to, onWindowFocusChanged will not get called no more when I
hit the MENU button.
It's a bit hard to explain this problem, and not obvious what info is
needed to answer/discuss the problem. I will paste the sourcecode
underneath, hoping it won't cause dismay. It's kinda greasy, but
that's not what I'm seeking advice on right now ;)
MySurfaceView.java - the SurfaceView
rename.java - the default activity
************************MySurfaceView.java:
public class MySurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
private SurfaceHolder holder;
private MySurfaceViewThread mySurfaceViewThread;
private boolean hasSurface;
private Bitmap bit;
private Bitmap bitUser;
private Bitmap bitShadow;
private Bitmap wall;
private Bitmap face;
private Game game;
Bitmap bitResized;
public MySurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
setFocusable(true);
//setFocusableInTouchMode(true);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
Log.d(this.getClass().getName(), "changed focus");
}
private void initialize() {
// Create a new SurfaceHolder and assign this class as its
callback.
holder = getHolder();
holder.addCallback(this);
hasSurface = false;
mySurfaceViewThread = new MySurfaceViewThread(holder, this);
BitmapDrawable d = (BitmapDrawable)getResources().getDrawable
(R.drawable.bit);
bit = d.getBitmap();
BitmapDrawable s = (BitmapDrawable)getResources().getDrawable
(R.drawable.face);
face = s.getBitmap();
int width = face.getWidth();
int height = face.getHeight();
int newWidth = 240;
int newHeight = 343;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
bitResized = Bitmap.createBitmap(face, 0, 0,
width, height, matrix, true);
BitmapDrawable e = (BitmapDrawable)getResources().getDrawable
(R.drawable.bituser);
bitUser = e.getBitmap();
BitmapDrawable f = (BitmapDrawable)getResources().getDrawable
(R.drawable.wall);
wall = f.getBitmap();
BitmapDrawable g = (BitmapDrawable)getResources().getDrawable
(R.drawable.bitshadow);
bitShadow = g.getBitmap();
game = new Game();
}
public void resume() {
// Create and start the graphics update thread.
if (mySurfaceViewThread == null) {
mySurfaceViewThread = new MySurfaceViewThread(holder,
this);
if (hasSurface == true)
mySurfaceViewThread.start();
}
}
public void pause() {
// Kill the graphics update thread
if (mySurfaceViewThread != null) {
mySurfaceViewThread.requestExitAndWait();
mySurfaceViewThread = null;
}
}
public void surfaceCreated(SurfaceHolder holder) {
hasSurface = true;
if (mySurfaceViewThread != null)
mySurfaceViewThread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
hasSurface = false;
pause();
}
public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
if (mySurfaceViewThread != null)
mySurfaceViewThread.onWindowResize(w, h);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent msg) {
synchronized (holder){
switch(keyCode){
case 19: game.moveUp();
Log.d(this.getClass().getName(), "opp");
break;
case 20: game.moveDown();break;
case 23: game.shoot(); break;
case 21: game.rotate();break;
case 22: game.rotate();break;
}
return true;
}
}
class MySurfaceViewThread extends Thread {
private boolean done;
MySurfaceViewThread(SurfaceHolder surfaceHolder, MySurfaceView
mySurfaceView) {
super();
done = false;
}
@Override
public void run() {
Canvas canvas;
//SurfaceHolder surfaceHolder = holder;
// Repeat the drawing loop until the thread is stopped.
while (!game.getGameOver()) {
// Lock the surface and return the canvas to
draw onto.
canvas = null;
try {
canvas = holder.lockCanvas(null);
synchronized (holder) {
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawBitmap(wall, 20, 0,
new Paint());
drawCPUPieces(canvas);
drawUserPiece(canvas);
drawShadowPiece(canvas);
drawUserPieceQueue(canvas);
drawGrid(canvas);
drawPoints(canvas);
}
} finally {
// do this in a finally so that if an exception is
thrown
// during the above, we don't leave the Surface in
an
// inconsistent state
if (canvas != null) {
holder.unlockCanvasAndPost(canvas);
}
}
/*Canvas canvas = surfaceHolder.lockCanvas();
canvas.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawBitmap(wall, 20, 0, new Paint());
drawCPUPieces(canvas);
drawUserPiece(canvas);
drawShadowPiece(canvas);
drawUserPieceQueue(canvas);
drawGrid(canvas);
drawPoints(canvas);
//canvas.drawBitmap(bitResized, 0, 150, new
Paint());
// Unlock the canvas and render the current
image.
surfaceHolder.unlockCanvasAndPost(canvas);*/
}
}
private void drawUserPieceQueue(Canvas canvas){
int pixels = 0;
for(PieceUser userPiece : game.getUserPieceQueue()){
boolean[][] userPieceArray =
userPiece.getPiece();
for(int i=0; i<userPieceArray.length; i++){
int x = (i *
MuzzleConstants.BIT_BITMAP_SIZE) + pixels;
int y = 150;
for(int j=0;
j<userPieceArray[0].length; j++){
if(userPieceArray[i][j]){
canvas.drawBitmap(bitUser, x, y, new Paint());
}
y+=MuzzleConstants.BIT_BITMAP_SIZE;
}
}
pixels += 24;
}
}
private void drawPoints(Canvas canvas){
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawText("Points: " +
Integer.toString(game.getPoints()),
5f, (float)((MuzzleConstants.HEIGHT-1)*10)+14, paint);
}
private void drawShadowPiece(Canvas canvas){
for(Position p : game.getShadowPiece()){
canvas.drawBitmap(bitShadow, p.getX()*10,
p.getY()*10, new Paint
());
}
}
private void drawGrid(Canvas canvas){
Paint paint = new Paint();
paint.setColor(Color.BLUE);
for(int x=0; x<MuzzleConstants.WIDTH; x++){
canvas.drawLine(x*MuzzleConstants.BIT_BITMAP_SIZE, 0,
x*MuzzleConstants.BIT_BITMAP_SIZE,
MuzzleConstants.HEIGHT*MuzzleConstants.BIT_BITMAP_SIZE, paint);
for(int y=0; y<MuzzleConstants.HEIGHT; y++){
canvas.drawLine(x*MuzzleConstants.BIT_BITMAP_SIZE,
y*MuzzleConstants.BIT_BITMAP_SIZE, (x*10)+10, y*10, paint);
}
}
}
private void drawUserPiece(Canvas canvas){
PieceUser userPiece = game.getUserPiece();
boolean[][] userPieceArray = userPiece.getPiece();
for(int i=0; i<userPieceArray.length; i++){
int x = i * MuzzleConstants.BIT_BITMAP_SIZE;
int y = userPiece.getPosition() *
MuzzleConstants.BIT_BITMAP_SIZE;
for(int j=0; j<userPieceArray[0].length; j++){
if(userPieceArray[i][j]){
canvas.drawBitmap(bitUser, x,
y, new Paint());
}
y+=MuzzleConstants.BIT_BITMAP_SIZE;
}
}
}
private void drawCPUPieces(Canvas canvas){
for(PieceCPU cpuPiece : game.getCPUPieces()){
int x =
cpuPiece.getPositionX()*MuzzleConstants.BIT_BITMAP_SIZE;
for(boolean[] bar : cpuPiece.getBars()){
int y =
cpuPiece.getPositionY()*MuzzleConstants.BIT_BITMAP_SIZE;
for(int i=0; i<bar.length; i++){
if(bar[i]){
canvas.drawBitmap(bit,
x, y, new Paint());
}
y+=MuzzleConstants.BIT_BITMAP_SIZE;
}
x-=MuzzleConstants.BIT_BITMAP_SIZE;
}
}
}
public void requestExitAndWait() {
// Mark this thread as complete and combine into
// the main application thread.
done = true;
try {
join();
} catch (InterruptedException ex) { }
}
public void onWindowResize(int w, int h) {
// Deal with a change in the available surface size.
}
}
}
************************rename.java:
public class rename extends Activity {
private static final int MENU_START = 6;
private static final int MENU_STOP = 7;
private MySurfaceView msv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// turn off the window's title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// tell system to use the layout defined in our XML file
setContentView(R.layout.muzzle_layout);
msv = (MySurfaceView)findViewById(R.id.muzzle); //new
MySurfaceView
(this);
//MySurfaceView msv = new MySurfaceView(this);
//setContentView(msv);
}
/**
* Notification that something is about to happen, to give the
Activity a
* chance to save state.
*
* @param outState a Bundle into which this Activity should save
its state
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
// just have the View's thread save its state into our Bundle
super.onSaveInstanceState(outState);
Log.w(this.getClass().getName(), "SIS called");
}
/**
* Invoked during init to give the Activity a chance to set up its
Menu.
*
* @param menu the Menu to which entries may be added
* @return true
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add("kuk");
menu.add("fitta");
return result;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()) {
case 1:
//fill in some actions here
break;
case 2:
//fill in some actions here
break;
}
return false;
}
@Override
protected void onPause() {
super.onPause();
msv.pause();
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---