Я пытаюсь реализовать ImageSwitcher для перехода к следующему изображению при броске.
Хорошо работает при нажатии на кнопку «Далее», но я не могу заставить его работать с fling GestureDetector.
В основном я реализовал тот же код в onFling, что и в onClick.
onClick работает, onFling - нет.
Вот код.
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 getQuestionImageDrawableXML(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;
}
}
}