Android 手势检测器不工作

Android 手势检测器不工作,android,gesture,Android,Gesture,我正在开发一个应用程序,我想在其中实现手势检测器。当用户双击时,它将显示下一幅图像,当用户单击时,它将显示描述。。。。。。。 我的代码如下 public class FirstImage extends ImageView implements OnTouchListener { double pixel=0; MotionEvent event; TextView k; int rid; GestureDetector gd; Bitm

我正在开发一个应用程序,我想在其中实现手势检测器。当用户双击时,它将显示下一幅图像,当用户单击时,它将显示描述。。。。。。。 我的代码如下

public class FirstImage extends ImageView implements  OnTouchListener  {

    double pixel=0;
    MotionEvent event;
    TextView k;
    int rid;
     GestureDetector gd; 
    Bitmap image,bitmap;
    String huma ="Human";
    String info="human";
    String infoR = "Ribosome";
    String infom = "Mitochondria";
    String infoc="Cytoplasm";
     float x = 0; //init value 
     float y = 0; //init value
     Animation animationFadeIn;
    int t=0;
    int i=1;
    private GestureDetector gestureDetector= new GestureDetector((OnGestureListener) this);

    public FirstImage(Context context) { 
        super(context); 



        } 
    public FirstImage(Context context, AttributeSet attrs) { 
        super(context, attrs);

        } 


    public void changeImage(int id,int Image_slide){
                    if(Image_slide==1){
                        this.setImageResource(id);
                        final Animation animationslidein=AnimationUtils.loadAnimation(getContext(), R.anim.slidein);
                        this.startAnimation(animationslidein);
                        rid=id;

                        this.setOnTouchListener((OnTouchListener) gestureDetector);
                        Log.w("debug","which is null"+gestureDetector );

                    }else if(Image_slide==2){
                        this.setImageResource(id);

                    final Animation animationslideout=AnimationUtils.loadAnimation(getContext(), R.anim.slideout);
                    this.startAnimation(animationslideout);
                    rid=id;
                    this.setOnTouchListener((OnTouchListener) gestureDetector);
                    }else
                    {
                        this.setImageResource(id);
                        rid=id;
                        final Animation animationzoomin=AnimationUtils.loadAnimation(getContext(), R.anim.zoomin);
                        this.startAnimation(animationzoomin);
                        this.setOnTouchListener((OnTouchListener) gestureDetector); 
                    }




    }

private void doubletap(float x, float y) {
        // TODO Auto-generated method stub
        if((x==(this.getWidth()))&&(y==(this.getHeight()))){
            int Image_slide=0;
            if(rid==R.drawable.human)
                this.changeImage(R.drawable.hand, Image_slide);
        }
public boolean onTouch(View v, MotionEvent me) {
    return gestureDetector.onTouchEvent(me);

}
public class MyGestureDetector extends SimpleOnGestureListener {
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        x=e.getX();
        y= e.getY();

        pageinfo(x,y);
        return true;
    }
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        return false;
    }
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub
        x=e.getX();
        y= e.getY();
        pageinfo(x,y);
    }
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub
        x=e.getX();
        y= e.getY();
        pageinfo(x,y);
    }
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        x=e.getX();
        y= e.getY();
        pageinfo(x,y);
        return true;
    }
    public boolean onDoubleTap(MotionEvent e) {

        x=e.getX();
        y= e.getY();
        doubletap(x,y);
        return true;
    }
}
}
有人能帮我解决代码中的问题吗。。。。。。为什么它不起作用。。。。。
}首先创建一个手势检测器和一个监听器。然后将其绑定到扩展GestureDetector.SimpleOnGestureListener的类

private GestureDetector detector;  
@Override  
public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  detector= new GestureDetector(this, new MyGesturesListener());  
}
然后您可以实现不同手势的方法:

class MyGesturesListener extends GestureDetector.SimpleOnGestureListener{  
  @Override  
  public boolean onSingleTapUp(MotionEvent ev) {  
    return true;  
  }  
  @Override  
  public void onShowPress(MotionEvent ev) {  
  }  
  @Override  
  public void onLongPress(MotionEvent ev) {  
  }  
  @Override  
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
    return true;  
  }  
  @Override  
  public boolean onDown(MotionEvent ev) {  
    return true;  
  }  
  @Override  
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
    return true;  
  }  
}  

首先创建一个手势检测器和一个监听器。然后将其绑定到扩展GestureDetector.SimpleOnGestureListener的类

private GestureDetector detector;  
@Override  
public void onCreate(Bundle savedInstanceState) {  
  super.onCreate(savedInstanceState);  
  detector= new GestureDetector(this, new MyGesturesListener());  
}
然后您可以实现不同手势的方法:

class MyGesturesListener extends GestureDetector.SimpleOnGestureListener{  
  @Override  
  public boolean onSingleTapUp(MotionEvent ev) {  
    return true;  
  }  
  @Override  
  public void onShowPress(MotionEvent ev) {  
  }  
  @Override  
  public void onLongPress(MotionEvent ev) {  
  }  
  @Override  
  public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
    return true;  
  }  
  @Override  
  public boolean onDown(MotionEvent ev) {  
    return true;  
  }  
  @Override  
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
    return true;  
  }  
}  

我发现这是一个框架布局。希望能有帮助。我发现这是一个框架布局。希望能有帮助。