从GridView调用的全屏图像浏览器

时间:2011-10-24 21:35:50

标签: android android-gridview

我有一个GridView活动,由单个GridView组成,可以从SD卡位置显示多达100个图像缩略图。我正在创建另一个活动,当触摸网格中的任何一个缩略图时调用该活动。此其他活动应以全屏显示所选图像(可以通过将图像uri传递给新活动来完成此操作)。但是,我希望能够从所选图像的全屏视图向左/向右滑动以转到下一个,而不是返回到gridview活动并选择另一个图像进行全屏显示。

如何使用滑动手势实现从SD卡位置提取图像以进行全屏浏览的图像浏览器活动?

2 个答案:

答案 0 :(得分:2)

您可以使用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;
}

}

答案 1 :(得分:2)

  1. 通过SDK Manager(或转到here
  2. 获取最新版本的兼容包
  3. 在项目中将JAR安装为lib
  4. 利用ViewPager,这是一个类似于AdapterView的ViewGroup,可以为您完成所有滑动手势管理
  5. 此外,来自Android开发人员博客的here is an example可能会帮助您入门。

    HTH