我正在尝试使用ImageSwitcher移动到下一张图像。
单击“下一步”按钮时效果很好,但是我无法使用它与使用的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;
}
}
}
答案 0 :(得分:1)
正如Varun所说,你必须将GestureListener设置为视图。您还需要覆盖GestureDetector类上的onSingleTapConfirmed和onDown方法:
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;
}
}