开发者

ImageSwitcher with GestureDetector "fling" / "swipe"

I'm trying to implement a ImageSwitcher to move to next image on fling.

It works well when clicking on the "Next" button, but I can't get it to work with the fling GestureDetector.

Basically I've implemented the same code in onFling as in onClick.

onClick works, onFling doesn't.

Here is the code.

public class FlipActivity extends Activity {

private int index;

private Button nextButton;

private Animation in;
private Animation out;

private ImageSwitcher questionImageSwitcher;

private Drawable image;

private String subjectArray[] = { "1", "2", "3", "4"};

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;

@Override
// =============================================================================
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.story);

    index = 0;

    in = AnimationUtils.loadAnimation(this,
            android.R.anim.fade_in);

    out = AnimationUtils.loadAnimation(this,
            android.R.anim.fade_out);

    // Set up Image Switcher
    questionImageSwitcher = (ImageSwitcher) findViewById(R.id.ImageSwitcher_QuestionImage);
    questionImageSwitcher.setInAnimation(in);
    questionImageSwitcher.setOutAnimation(out);
    questionImageSwitcher.setFactory(new MyImageSwitcherFactory());


    image = getQuestionImageDrawableXML(subjectArray[index]);

    questionImageSwitcher.setImageDrawable(image);      

    // Gesture detection
    gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;      
            }
            return false;
        }
    };


    nextButton = (Button) findViewById(R.id.next);
    nextButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            index = index + 1;

                image = getQuestionImageDrawableXML(subjectArray[index]);
                questionImageSwitcher.setImageDrawable(image);

        }
    });

} // end onCreate

private class MyImageSwitcherFactory implements ViewSwitcher.ViewFactory {
    public View makeView() {
        ImageView imageView = new ImageView(FlipActivity.this);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        return imageView;
    }
}

private Drawable getQu开发者_如何学JAVAestionImageDrawableXML(String questionNumber) {

    String uri = "img" + questionNumber;

    int imageResource = getResources().getIdentifier(uri, "drawable",
            getPackageName());

    Drawable image = getResources().getDrawable(imageResource);
    return image;
}


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) {

                questionImageSwitcher.setInAnimation(in);
                questionImageSwitcher.setOutAnimation(out);
                questionImageSwitcher.setFactory(new MyImageSwitcherFactory());


                index = index + 1;

                image = getQuestionImageDrawableXML(subjectArray[index]);
                    questionImageSwitcher.setImageDrawable(image);

            }  

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


As Varun said you must set the GestureListener to the view. You also need to override onSingleTapConfirmed and onDown methods on your GestureDetector Class:

class MyGestureDetector extends SimpleOnGestureListener {

   @Override
   public boolean onSingleTapConfirmed(MotionEvent e) 
   {            
    return super.onSingleTapConfirmed(e);
   }

   @Override
   public boolean onDown(MotionEvent e) 
   {            
    return true;
   }

   @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) {

            questionImageSwitcher.setInAnimation(in);
            questionImageSwitcher.setOutAnimation(out);
            questionImageSwitcher.setFactory(new MyImageSwitcherFactory());


            index = index + 1;

            image = getQuestionImageDrawableXML(subjectArray[index]);
                questionImageSwitcher.setImageDrawable(image);

           }  

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜