SImple Draw Program --Advice Needed

by joshbeck » Sun, 02 Nov 2008 17:14:32 GMT


Sponsored Links
 I watered this code down as much as possible for easy reading.
Here is what it does:

It draws a bitmap image to the screen. --darkcleanlinux
(This part works.)

It's the OnTouchEven(MotionEvent event) function that doesn't work.

-I'm not sure I'm using the mCanvas object correctly.

I would think that it would place a big dot on the screen when
touched.
Instead, it crashes.


Any advice for a newb?

Thanks

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



SImple Draw Program --Advice Needed

by joshbeck » Sun, 02 Nov 2008 17:15:29 GMT


 Here's the code:

Thank You!

package com.android.picturetest;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;

public class BitmapMesh extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new SampleView(this));

    }

        private static class SampleView extends View {
        private  Canvas mCanvas;
        private final Paint mPaint;
        private Bitmap mBitmap;



        public SampleView(Context context) {

                super(context);
                 mPaint = new Paint();
                 mPaint.setAntiAlias(true);
             mPaint.setARGB(255, 255, 255, 255);
             mBitmap = BitmapFactory.decodeResource(getResources(),
 
R.drawable.darkcleanlinux);


                }

        @Override protected void onDraw(Canvas canvas) {

            canvas.drawBitmap(mBitmap, 0, 0, null);

        }

       @Override public boolean onTouchEvent(MotionEvent event) {

            draw2(event.getX(),event.getY());
            return true;
        }

       private void draw2(float X, float Y) {


            mCanvas.drawCircle(X, Y, 200, mPaint);

        }

    }

}
--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


SImple Draw Program --Advice Needed

by Steve Oldmeadow » Sun, 02 Nov 2008 17:40:25 GMT


 I'd say it crashes because mCanvas is null when you call draw2.  How
did you expect mCanvas to be instantiated?

Even so - the correct approach is to call invalidate on your view and
use the canvas object that gets passed in to the onDraw.  So in your
onTouchEvent you probably want to store the x and y coordinates and
set a flag to indicate the user has touched the screen.  Then call
invalidate on your view - this will cause onDraw to be called at some
later time.  Then in your onDraw you can check your flag and if it is
set then draw the circle at the coordinates you stored in the
onTouchEvent.
--~--~---------~--~----~------------~-------~--~----~



SImple Draw Program --Advice Needed

by joshbeck » Sun, 02 Nov 2008 23:04:42 GMT


 Thanks a million. I'll try this today and report back.

I appreciate it.

When you say 'set a flag.' Do you mean like an int? 0 or 1 kind of
thing?

Thanks again,
Pros



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



SImple Draw Program --Advice Needed

by joshbeck » Sun, 02 Nov 2008 23:06:16 GMT


 


One more thing:
If I set a flag in OnTouchEvent that is something like this
int myflag = 1;

What is the best way to make it global so that onDraw will recognize
it?


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



SImple Draw Program --Advice Needed

by joshbeck » Sun, 02 Nov 2008 23:23:54 GMT


 




Ok, this keeps the background and draws a circle on top of it when I
touch the screen.
Question: How do you think I might get it to keep the old circles when
I draw new ones.
Right now when I click the canvas is completely redrawn and the first
circle I created is lost.

Thanks, that helped a lot.




import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;

public class BitmapMesh extends Activity {
        SampleView mView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = new SampleView(this);
        setContentView(mView);

    }

        private static class SampleView extends View {
        private  Canvas mCanvas;
        private final Paint mPaint;
        private Bitmap mBitmap;
        public float XX;
        public float YY;
        public float ZZ;

        public SampleView(Context context) {

                super(context);
                 ZZ = 0;

                 mPaint = new Paint();
                 mPaint.setAntiAlias(true);
             mPaint.setARGB(255, 255, 255, 255);
             mBitmap = BitmapFactory.decodeResource(getResources(),
 
R.drawable.darkcleanlinux);


                }

        @Override protected void onDraw(Canvas canvas) {
            mCanvas = canvas;
            if(ZZ != 0){
                canvas.drawBitmap(mBitmap, 0, 0, null);
                canvas.drawCircle(XX, YY, 20, mPaint);
            }
            else
            canvas.drawBitmap(mBitmap, 0, 0, null);

        }

       @Override public boolean onTouchEvent(MotionEvent event) {
            invalidate();

            XX= event.getX();
            YY= event.getY();
            ZZ = 1;
            return true;
        }

       private void draw2(float X, float Y) {


            mCanvas.drawCircle(X, Y, 200, mPaint);

        }

    }

}
--~--~---------~--~----~------------~-------~--~----~



SImple Draw Program --Advice Needed

by Steve Oldmeadow » Mon, 03 Nov 2008 08:18:54 GMT


 


You are losing the old circles because (I assume) you are clearing the
screen when you draw the mBitmap.  However, that is a pretty normal
thing to do in your onDraw.  If you want to draw the previous circles
you need to remember their coordinates in some kind of data structure
and then iterate over that structure and draw all the circles.

Have a look here:  
 http://java.sun.com/docs/books/tutorial/collections/intro/index.html 
for some details about collections in Java.  Something like ArrayList
will work for you and be easy to implement.  If you move to this
approach then in the onDraw you add a new object (maybe a Point) to
the ArrayList and then in the onDraw iterate over your ArrayList and
draw all the circles.  In this case you won't need your ZZ flag any
more but for future reference a boolean would have been a better data
type for your flag.


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



Other Threads

1. How do I get pen type events in my app?

So the driver guy tells me that I have an input device that is capable
of inputting the "pen" type.  This is custom hardware, of course.

He says he is calling:

input_report_key(dev, BTN_TOOL_PEN, 1);
input_report_key(dev, BTN_TOOL_{*filter*},1);

and also reporting the X and Y coordinates with ABS_X and ABS_Y.

I can get the ACTION_DOWN and ACTION_UP and (x,y) in my app with
motionEvents.  For the life of me I cannot figure out how to get the
pen "type" events.  Is that a keyEvent?  I tried to listen to
keyEvents but I never hear anything about pen type...

Please help.  Thanks.

-- 

2. Fwd: Problem in Handler

plz tell ,me how to handle key dispatch timed out error in android. I have
developed a small quiz application. If i double click my app two times then
it shows *key dispatch timed out error* .
Plz help me if any one knows

---------- Forwarded message ----------
From: yogi <yogi.ru...@gmail.com>
Date: Thu, Feb 3, 2011 at 9:46 AM
Subject: [android-developers] Problem in Handler
To: Android Developers <android-developers@googlegroups.com>


Hi all,
I have created to thread in an application, and from these threads i
am sending a value.
But the App has started but after 5-6 seconds it crashes and with the
following log error.


02-03 09:40:19.598: ERROR/AndroidRuntime(1684): FATAL EXCEPTION: main
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):
android.util.AndroidRuntimeException: { what=1000 when=2469427 obj=4 }
This message is already in use.
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.MessageQueue.enqueueMessage(MessageQueue.java:171)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.Handler.sendMessageAtTime(Handler.java:457)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.Handler.sendMessageDelayed(Handler.java:430)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.Handler.sendEmptyMessageDelayed(Handler.java:394)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.Handler.sendEmptyMessage(Handler.java:379)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.ViewRoot.scheduleTraversals(ViewRoot.java:652)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.ViewRoot.requestLayout(ViewRoot.java:596)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.View.requestLayout(View.java:8125)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.View.requestLayout(View.java:8125)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.View.requestLayout(View.java:8125)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.View.requestLayout(View.java:8125)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.view.View.requestLayout(View.java:8125)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.widget.TextView.checkForRelayout(TextView.java:5378)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.widget.TextView.setText(TextView.java:2688)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.widget.TextView.setText(TextView.java:2556)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.widget.TextView.setText(TextView.java:2531)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
com.demo.app.handlerapp$3.handleMessage(handlerapp.java:95)
02-03 09:40:19.598: ERROR/AndroidRuntime(1684):     at
android.os.Handler.dispatchMessage(Handler.java:99)

Please refer to the following code which i have written.

package com.demo.app;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class handlerapp extends Activity implements OnClickListener{
   /** Called when the activity is first created. */
       @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       button=(Button)findViewById(R.id.Button01);
       tw=(TextView)findViewById(R.id.TextView01);
       tw.setText("click button to start");
       button.setOnClickListener(this);
  }
       @Override
       public void onClick(View v) {
               // TODO Auto-generated method stub
       switch(v.getId()){
       case R.id.Button01:
               Toast.makeText(getApplicationContext(), "clicked", 2).show();
               if(!th.isAlive())
           th.start();
               if(!th1.isAlive())
                       th1.start();
               break;
       }

       }
       Thread th= new Thread(new Runnable(){

               @Override
               public void run() {
                       // TODO Auto-generated method stub
                       Message msg= handler.obtainMessage();
                       for(int i=2;i<1000;i++)
                       {
                               try {
                                       Thread.sleep(5000);
                               } catch (InterruptedException e) {
                                       // TODO Auto-generated catch block
                                       e.printStackTrace();
                               }
                       msg.obj= i;
                       msg.what=2;
                       handler.sendMessage(msg);
                       }

               }

       });
       Thread th1= new Thread(new Runnable(){
               @Override
               public void run() {
                       // TODO Auto-generated method stub
                       Message msg= handler.obtainMessage();
                       for(int i=2;i<1000;i++)
                       {
                               try {
                                       Thread.sleep(3000);
                               } catch (InterruptedException e) {
                                       // TODO Auto-generated catch block
                                       e.printStackTrace();
                               }
                       msg.obj= i;
                       msg.what=1;
                       handler.sendMessage(msg);
                       }

               }

       });

       Handler handler = new Handler(){
               @Override
               public void handleMessage(Message msg) {
                       // TODO Auto-generated method stub
                       super.handleMessage(msg);
                       switch(msg.what){
                       case 1:
                               Toast.makeText(getApplicationContext(),
msg.obj.toString(),
1).show();
                               break;
                       case 2:

                               tw.setText(msg.obj.toString());
                               removeMessages(msg.what, msg.obj);
                       break;
                       }
               }

   };


       Button button;
       TextView tw;
}



Thanks in Advance
Regards
Yogi

--




-- 
Regards,
Roshan

-- 

3. basic problem with the Handler.

4. Android copy protection

5. Android-Specific RSS Library

6. BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION intent Extra does not extend 120 sec discovery interval

7. NFC... TAG_DISCOVORED intent is only send via startActivity()