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. Mobile Developer Survey

A survey for mobile application developers has been posted at
http://openandroids.com . Take the
and be entered to win an Amazon voucher ...

The link to the survey is: 
http://openandroids.com/2008/06/09/mobile-application-developer-survey-have-your-say/
--~--~---------~--~----~------------~-------~--~----~

2. Drawing shape as ImageVIew

Hi
I hope somebody can help me with drawing an image using XML
declaration.
I need to draw a part of a "chat bubble", it's top and it's bottom. To
understand what i mean you can check out an example delivered with
android emulator  API Demos -> Graphics - > Drawable -> ShapeDrawable
The last one bubble in this example is what i need to be constructed
with head image(ImageView), TextView and bottom image(ImageView).
This is a bottom element:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
type="rectangle">
    <gradient android:startColor="#FFE1E1E1"
android:endColor="#FFFFFFFF"
            android:angle="270"/>
    <corners android:bottomLeftRadius="7dp"
                android:bottomRightRadius="7dp"
                android:topLeftRadius="1dp"
                android:topRightRadius="1dp"
                 />
</shape>

Top is the same, with only reverted values for angle radiuses.
So the problem this shape is drawn with a small unwanted rounding on
top of a rectangle.
Replacing topLeftRadius and topRightRadius values with 0dp doesn't
help, shapes are not drawn at all in this case :(
--~--~---------~--~----~------------~-------~--~----~

3. One of the best free sites - 7 exclusive, never seen sites where you can join for free and download lot of high quality dvd movies. No Credit Card Required

4. How to get constant waypoints for my locationbased service

5. Compairing EditText fields with if..else

6. Masked EditText

7. How to pass data between activities?