当我在android中向左或向右滑动图像时获取下一个图像

时间:2014-08-13 04:19:10

标签: android image slider swipe

我正在使用此链接http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/中的代码制作图片滑块。

//This method is called each time user swipes the screen
@Override
public Object instantiateItem(ViewGroup container, final int position)
  {
   TouchImageView imgDisplay;
   imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);

   imgDisplay.setOnTouchListener(new OnTouchListener() {

        float x1, x2, y1, y2;
        String direction;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {

            case (MotionEvent.ACTION_DOWN):
                x1 = event.getX();
                y1 = event.getY();
                break;
            case (MotionEvent.ACTION_MOVE): {
                x2 = event.getX();
                y2 = event.getY();
                float differenceInX = x2 - x1;
                float differenceInY = y2 - y1;

                // Use dx and dy to determine the direction
                if (Math.abs(differenceInX) > Math.abs(differenceInY)) {
                    if (differenceInX > 0) {
                        System.out.println("SWIPING RIGHT");
                            Toast.makeText(_activity, "Right",
                                    Toast.LENGTH_SHORT).show();

                     } 
                   else 
                     {
                        System.out.println("SWIPING LEFT "+position);
                            Toast.makeText(_activity, "Left",
                                    Toast.LENGTH_SHORT).show();
                     }
                  }
               }

            }
            return false;
        }

    });

     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inPreferredConfig = Bitmap.Config.ARGB_8888;
      //_imagePaths is an arraylist that stores all the image paths.
     Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position),options);
     imgDisplay.setImageBitmap(bitmap);
  }

我必须添加以下功能:

如果我将第一张图片向左滑动,我会得到第二张图片。但是,如果我将第一张图像向右滑动,我也必须获得第二张图像。

我会说得更清楚。

如果我将第二张图像滑到左侧,我会得到第三张图像。

并且如果我将第二张图像滑动到右侧而不是第一张图像而不是第一张图像。

目标是每次都能获得下一张图像,无论是向左侧还是右侧滑动图像。

0 个答案:

没有答案