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

发布于 2024-12-11 16:51:27 字数 267 浏览 1 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧街凉风 2024-12-18 16:51:27

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

}

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;
}

}
Hello爱情风 2024-12-18 16:51:27
  1. 通过 SDK Manager 获取最新版本的兼容性包(或访问此处
  2. 将 JAR 作为 lib 安装在您的项目中
  3. 利用 ViewPager,它是一个像 AdapterView 一样的 ViewGroup,并为您完成所有滑动手势管理工作

另外,这是来自 Android 开发者博客的示例这可能会让你开始。

华泰

  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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文