Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to move an imageview to right get activity in android?

I have implemented an application with image view in my application i would like to implement when user move the image right side then i would like to get the next acitvity. How can i write an event or any other code for move the image just right side?

I have used am imageView code as:

   (ImageView)findViewById(R.id.slide) //event for move right here

     Intent it = new Intent(Slide.this,NextActivity.class);
     startActivity(it);  

Please any body help me...

like image 768
prasad.gai Avatar asked Dec 01 '25 06:12

prasad.gai


1 Answers

So, if i understood you correctly, you want to move an ImageView across the screen, drag it, and when you hit a certain spot with it you want to start another activity?

If so, you could try and use the code below...which let's you drag your imageview across the screen

Just add an onTouchListener to the imageView you're trying to slide (move/drag)

yourSlideImageViewObject.setOnTouchListener(new View.OnTouchListener() 
{
          @Override
          public boolean onTouch(View v, MotionEvent event) 
          {
                onSlideTouch(v, event);
                return false;
          }
});

Then add this code to handle the Touch events

public void onSlideTouch( View view, MotionEvent event )
{
    //When the user pushes down on an ImageView
    if ( event.getAction() == MotionEvent.ACTION_DOWN )
    {
       inDragMode = true; //Set a variable so we know we started draggin the imageView
       //Set the selected ImageView X and Y exact position
       selectedImageViewX = Math.abs((int)event.getRawX()-((ImageView)view).getLeft());
       selectedImageViewY = Math.abs((int)event.getRawY()-((ImageView)view).getTop());
       //Bring the imageView in front
       ((ImageView)view).bringToFront();
    }

    //When the user let's the ImageView go (raises finger)
    if ( event.getAction() == MotionEvent.ACTION_UP )
    {
       inDragMode = false; //Reset the variable which let's us know we're not in drag mode anymore
    }

    //When the user keeps his finger on te screen and drags it (slides it)
    if ( event.getAction() == MotionEvent.ACTION_MOVE )
    {
        //If we've started draggin the imageView
        if ( inDragMode )
        {
            //Get the imageView object
            ImageView slide = (ImageView)findViewById(R.id.slide);
            //Get a parameters object (THIS EXAMPLE IS FOR A RELATIVE LAYOUT)
            RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams)slide.getLayoutParams();
            //Change the position of the imageview accordingly
            params.setMargins((int)event.getRawX()-selectedImageViewX, (int)event.getRawY()-selectedImageViewY, 0, 0);
            //Set the new params
            slide.setLayoutParams(params);

            //If we hit a limit with our imageView position
            if( slideImageView_position is inside an interval )
            {
                //Open another activity
                Intent it = new Intent(Slide.this,NextActivity.class);
                startActivity(it);
            }
        }
    }

}

The above should drag your imageview accross the screen.

You can also implement in the MotionEvent.ACTION_MOVE event .... to limit the movement of the ImageView accross the screen. So check the new coordinates of your finger across the screen and check if they are out of bounds. If so, then do nothing. If they aren't out of bounds, then change the left and upper margins of the ImaveView.

Also, on the MotionEvent.ACTION_UP event, check to see if the left margin of the ImaveView is over a predefined limit (which means the user slided the imageView over you intended position). If so, you can continue doing whatever you want, which means starting an activity. OR, if you don't want to open the activity while the user is dragging, not only after he has removed his finger from the imageView, then just do that if () i've written under the MotionEvent.ACTION_MOVE event. Insert there the interval you want, so when the imageView is dragged inside that interval, the activity starts.

If you're confused, please ask :) It's very late here, and i'm tired, so i might have not explained it properly :D

like image 175
AndreiBogdan Avatar answered Dec 03 '25 21:12

AndreiBogdan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!