Abhishek Sharma
Abhishek Sharma

Reputation: 619

full-screen image browser called from GridView

I have a GridView activity consisting of a single GridView that displays upto 100 image thumbnails from an sdcard location. I am creating another activity which is called when any one of the thumbnail in the grid is touched. This other activity should display the selected image in full-screen (can do this by passing the image uri to new activity). However, I would like to be able to swipe left/right from the full-screen view of the selected image to go to the next one instead of going back to the gridview activity and selecting another image for full-screen display.

How can I achieve this image browser activity that pulls images from the sdcard location for full-screen browsing using swipe gesture?

Upvotes: 1

Views: 3078

Answers (2)

Reno
Reno

Reputation: 33792

You may use a ViewFlipper

public class ImageViewTest extends Activity {

    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private GestureDetector gestureDetector;
    View.OnTouchListener gestureListener;

    private Animation slideLeftIn;
    private Animation slideLeftOut;
    private Animation slideRightIn;
    private Animation slideRightOut;
    private ViewFlipper viewFlipper;

    String message = "Initial Message"; 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Set up viewflipper
        viewFlipper = new ViewFlipper(this);       
        ImageView i = new ImageView(this);
        i.setImageResource(R.drawable.sample_1);
        ImageView i2 = new ImageView(this);
        i2.setImageResource(R.drawable.sample_2);
        viewFlipper.addView(i);
        viewFlipper.addView(i2);

        //set up animations
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);

        //put up a brownie as a starter
        setContentView(viewFlipper);

        gestureDetector = new GestureDetector(new MyGestureDetector());
    }

    public class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Log.v(LOGID,"right to left swipe detected");
                    viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                    viewFlipper.showNext();
                    setContentView(viewFlipper);

                } // left to right swipe 
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Log.v(LOGID,"left to right swipe detected");                    
                    viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                    viewFlipper.showPrevious();
                    setContentView(viewFlipper);

                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }

@Override
public boolean onTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event))
return true;
else
return false;
}

}

Upvotes: 2

devunwired
devunwired

Reputation: 63293

  1. Grab the latest version of the Compatibility Package through the SDK Manager (or go here)
  2. Install the JAR as a lib in your project
  3. Utilize ViewPager, which is a ViewGroup that acts like an AdapterView and does all the swipe gesture management work for you

Also, here is an example from the Android Developer Blog that may get you started.

HTH

Upvotes: 2

Related Questions