Problems with onKeyDown after resume

by Brian Durney » Wed, 17 Dec 2008 02:34:43 GMT


Sponsored Links
 Hello,
I have written a simple application that draws an image (Drawable) on
the screen.  When the user presses a key, the image is supposed to
move.  When I first start the application from Eclipse, the keys work
fine.  However, if I go to another screen in the emulator and then go
back to my application (with a long press of the Home button) then the
keys don't work any more.  The same problem occurs if I go to the
applications menu and click on the icon for my application--the key
events don't work.

I have included the code for my view below.  The only other file in
the application is an Activity that has a very simple onCreate method
to create an instance of this view and call setContentView().
Thanks,
Brian Durney

public class DrawView extends View {
        Bitmap img;
        Drawable botDraw;
        float x = 35, y = 100;

        public DrawView(Context context) {
                super(context);
        setFocusable(true);  // MIGHT BE NECESSARY FOR KEY EVENTS

        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        img = BitmapFactory.decodeResource(context.getResources(),
R.drawable.bot2);
        botDraw =  context.getResources().getDrawable
(R.drawable.bot1);
        }

        public boolean onKeyDown(int keyCode, KeyEvent msg) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_W:
                        y -= 5;
                        invalidate();
                        return true;
                case KeyEvent.KEYCODE_S:
                        y += 5;
                        invalidate();
                        return true;
                default:
                        return super.onKeyDown(keyCode, msg);
                }
    }

        public void onDraw(Canvas canvas) {
                canvas.drawColor(0xFFC0C090);
                Paint paint = new Paint();
                paint.setColor(Color.GREEN);
                paint.setAntiAlias(true);
                canvas.drawCircle(35.0f, 50.0f, 20.0f, paint);
                canvas.drawBitmap(img, x, y, null);
                canvas.save();
                canvas.rotate(45, 180, 80);  // ANGLE IN DEGREES, PIVOT X, 
PIVOT Y
                botDraw.setBounds(150, 50, 210, 110);
                botDraw.draw(canvas);
                canvas.restore();
        }
}



--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. G1, Android and BT headset?

Hi,
what feature am I lacking with my G1, 1.5 software that stops me
listening to audio via my plantronics BT headset?

This used to work on my Blackberry curve with the same headset, I
receive my voicemail as was attachments in email, when I play
them they open in the music player and play through the rear speaker.
How do I get the audio via the bluetooth headset?

With the Blackberry when the BT headset was paired it would send the
audio to the headset.


Thanks
Miek

--~--~---------~--~----~------------~-------~--~----~

2. How to use ActivityGroup?

Hi,
anyone can point me to a tutorial about ActivityGroup or give me some
hints on how to use it?
I looked at the documentation but I wasn't able to understand how it
works...
--~--~---------~--~----~------------~-------~--~----~

3. How to prevent a webview to be reloaded when the screen orientation changes ?

4. HTC Magic - Crippled Features on Retail Devices

5. Is cupcake on the UK G1 just plain unreliable?

6. Dailouge box disappears after flipping the device

7. How to prevent a webview to be reloaded when the screen orientation changes ?