Search code examples
javaandroidontouchlistenerontouch

Java Android : onTouchListener release?


Java/android noob here, I have a problem where using onTouchListener I change the variable x to x+1 and that works fine, but when I release I would like the x variable to decrease in value until it hits zero, like so:

    Imageview image = (ImageView) findViewById(R.id.fgView);

    image.setOnTouchListener(new View.OnTouchListener()
    {

        public boolean onTouch(View v, MotionEvent event)
        {switch (event.getAction() & MotionEvent.ACTION_MASK)
            {

            case MotionEvent.ACTION_DOWN:

                    mode=1;

                break;
            case MotionEvent.ACTION_UP:

                mode=2;

                break;
            case MotionEvent.ACTION_POINTER_UP:

                mode2=2;

                break;
            case MotionEvent.ACTION_MOVE:

                mode=1;

                break;
            case MotionEvent.ACTION_POINTER_DOWN:

                mode2=1;

                break;
            }

        }
       if(mode==1){
       x++;
       }

       if(mode==2){
       x * 0.9
       }

       //mode2 is filler

     }

Now that works fine, my problem comes when I want my x to continue to decrease after release, but since it's under setOnTouchListener then it doesn't work. Maybe something like this:

     public boolean onTouchFalse(View v){
          if(x>0){
              x * 0.9
          }
     }

or even something like

     public boolean image.isOnScreen{
         if(x>0){
            x = x * 0.9
         }
     }

tl;dr: I'm looking for some kind of listener that I could use to depreciate my x.

Is there a listener or some class i'm missing? Thanks for any and all help!


Solution

  •     final Handler handler = new Handler();
    
    
        image.post( new Runnable() {
            @Override
            public void run() {
    
                x--;
                handler.postDelayed(this, 100);
    
            }
        });
    

    ** Note that the 100 is how long in between each subtraction (in millisecs)