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. Scale a background image or drawable?

Hi,

On a layout I want to scale the background image (keeping it's aspect
ratio) to the space allocated when the page gets created. Anyone have
any idea how to do this?

I am using layout.setBackgroundDrawable() and am using a
BitmapDrawable to setGravity for clipping and filling, but don't see
any option for scaling.

Sam
--~--~---------~--~----~------------~-------~--~----~

2. a problem about ALSA - Not building

Hi Guys, couldn't not notice that you are trying to make Alsa work.

 

I have done:

 

1.

 

git clone git://android.git.kernel.org/platform/hardware/alsa_sound.git

git clone git://android.git.kernel.org/platform/external/alsa-lib.git

git clone git://android.git.kernel.org/platform/external/alsa-utils.git

 

2.

 

change the BoardConfig.mk

 

#HAVE_HTC_AUDIO_DRIVER := true --> or without the comment and FALSE

#BOARD_USES_GENERIC_AUDIO := true

BOARD_USES_ALSA_AUDIO := true

BUILD_WITH_ALSA_UTILS := true

 

When I try to build Android it gives errors and doesn't continue. The error
I get is:

 

http://pastebin.com/m7eded520

 

Any help on this subject would be appreciated.

  _____  

From: android-porting@googlegroups.com
[mailto:android-port...@googlegroups.com] On Behalf Of pavan savoy
Sent: Tuesday, August 18, 2009 7:18 AM
To: android-porting@googlegroups.com
Subject: [android-porting] Re: a problem about ALSA,Permission denied

 

the permission denied is on the /dev/snd/pcmC0D0p kind of a device - for
which you need to give the 777 or change owner to media after boot.







The ALSA problem troubled me for a few days, I found some infos from
here,and I modified BoardConfig.mk exactly as all guys said below

3. Segmentation Fault

4. ALSA on Android 1.5

5. I need your help

6. ADP 1 - Planning to buy ... but ... Voice Recognition , Does it work & How ?

7. Compile fails when using a method that is Since: API Level 3