onTouchEvent <second time touched?

Put your problem here if it does not fit any of the other categories.

onTouchEvent <second time touched?

Postby freeskier » Sat Jul 03, 2010 7:56 am

Is there anyway I can make the event different for the second time the screen is touched? I want to make it so an image is drawn where you touch and then when you touch again a second image is drawn. I have it figured out except how to make a different image come up the second time.

Do I use another onTouchEvent?
freeskier
Freshman
Freshman
 
Posts: 2
Joined: Fri Jul 02, 2010 11:26 pm

Top

Re: onTouchEvent <second time touched?

Postby mr_muskuraha » Sat Jul 03, 2010 10:46 am

beside touch event you will have to override graphics onDraw method and some custom adapters aswell

if you want to draw an image where you click. again and again. what i understood

if i am right then let me know for further details.
mr_muskuraha
Senior Developer
Senior Developer
 
Posts: 142
Joined: Wed Jan 13, 2010 8:49 am

Re: onTouchEvent <second time touched?

Postby anarche » Sun Jul 04, 2010 4:05 am

Yep, change listeners.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: onTouchEvent <second time touched?

Postby mr_muskuraha » Mon Jul 05, 2010 8:08 am

example what you need.


Code: Select all
public class Tutorial2D extends Activity {
   
    @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           requestWindowFeature(Window.FEATURE_NO_TITLE);
           setContentView(new Panel(this));
          
          
          
       }

   
   
   
   
   
   
    class Panel extends  SurfaceView implements SurfaceHolder.Callback {
         
       private TutorialThread _thread;
       private int _x = 20;
       private int _y = 20;

       public Panel(Context context) {
              super(context);
              getHolder().addCallback(this);
              _thread = new TutorialThread(getHolder(), this);
              setFocusable(true);

          }
      
          @Override
          public void onDraw(Canvas canvas) {
             
                 Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
                
                 
                 canvas.drawColor(Color.BLACK);
                 canvas.drawBitmap(_scratch, _x, _y, null);
                

          }

         @Override
         public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
               int arg3) {
            // TODO Auto-generated method stub
            
         }

         @Override
         public void surfaceCreated(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
             _thread.setRunning(true);
                _thread.start();

         }

         @Override
         public void surfaceDestroyed(SurfaceHolder arg0) {
            // TODO Auto-generated method stub
             boolean retry = true;
                _thread.setRunning(false);
                while (retry) {
                    try {
                        _thread.join();
                        retry = false;
                    } catch (InterruptedException e) {
                        // we will try it again and again...
                    }

         }

    }
         
         
         
          @Override
          public boolean onTouchEvent(MotionEvent event) {
              _x = (int) event.getX();
              _y = (int) event.getY();
              return true;
          }
         
         
   
    }
   
   
    class TutorialThread extends Thread {
          private SurfaceHolder _surfaceHolder;
          private Panel _panel;
          private boolean _run = false;
      
          public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
              _surfaceHolder = surfaceHolder;
              _panel = panel;
          }
      
          public void setRunning(boolean run) {
              _run = run;
          }
      
          @Override
          public void run() {
              Canvas c;
              while (_run) {
                  c = null;
                  try {
                      c = _surfaceHolder.lockCanvas(null);
                      synchronized (_surfaceHolder) {
                          _panel.onDraw(c);
                      }
                  } 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 (c != null) {
                          _surfaceHolder.unlockCanvasAndPost(c);
                      }
                  }
              }
          }
    }

}



where ever you click on screen it will drawbitmap there.
mr_muskuraha
Senior Developer
Senior Developer
 
Posts: 142
Joined: Wed Jan 13, 2010 8:49 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 19 guests