当前位置: 代码迷 >> Android >> 为什么GestureDetector的onFling函数执行不到解决方案
  详细解决方案

为什么GestureDetector的onFling函数执行不到解决方案

热度:79   发布时间:2016-05-01 22:01:51.0
为什么GestureDetector的onFling函数执行不到
问题1:测试手势操作,在imageview上滑动鼠标,但是onFling函数执行不到,没有log输出。
根据网上说的,也设置了mImageView.setLongClickable(true),但还是执行不到。
 
问题2:就是在点击主activity中的一个图片后,进入全屏显示模式,此时点击放大后,在点击上下左右键,则画面向下移动一个通知栏的高度,有时候不点击放大也会出现此问题。

问题3:还有就是在放大后,点击上下左右键,有时候可以移动图片,有时候又不能移动图片。这个有是什么原因?

代码如下:
1.
Java code
public class DisplayImage extends Activity implements OnTouchListener, OnGestureListener  {    private static final String TAG = "DisplayImage";    private static final int FLING_MIN_DISTANCE = 50;    private static final int FLING_MIN_VELOCITY = 100;        private GestureDetector mGestureDetector;        /* 相关变量声明 */    private ImageView mImageView;    private Button mButton01;    private Button mButton02;    private FrameLayout layout1;    private LinearLayout layoutImage;    private Bitmap bmp;    private int id=0;    private int displayWidth;    private int displayHeight;    private float scaleWidth=1;    private float scaleHeight=1;        /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE); // 隐藏标题        // 设置全屏Flag标识        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                             WindowManager.LayoutParams.FLAG_FULLSCREEN);        /* 加载display.xml Layout */        setContentView(R.layout.display);                /* 取得屏幕分辨率大小 */        DisplayMetrics dm=new DisplayMetrics();        getWindowManager().getDefaultDisplay().getMetrics(dm);        displayWidth=dm.widthPixels;        displayHeight=dm.heightPixels;                 /* 初始化相关变量 */        Bundle bundle = this.getIntent().getExtras();        Integer imageId = bundle.getInt("imageId");        Log.i(TAG, "onCreate, imageId = " + imageId);                               bmp = BitmapFactory.decodeResource(getResources(), imageId);         mImageView = (ImageView)findViewById(R.id.myImageView);        mImageView.setImageBitmap(bmp);        mImageView.setOnTouchListener(this);        mImageView.setLongClickable(true);                 mGestureDetector = new GestureDetector(this);                 layout1 = (FrameLayout)findViewById(R.id.layout1);        layoutImage = (LinearLayout)findViewById(R.id.layoutImage);        mButton01 = (Button)findViewById(R.id.myButton1);        mButton02 = (Button)findViewById(R.id.myButton2);                 /* 缩小按钮onClickListener */        mButton01.setOnClickListener(new Button.OnClickListener() {            @Override            public void onClick(View v) {                small();             }        });                /* 放大按钮onClickListener */        mButton02.setOnClickListener(new Button.OnClickListener() {            @Override                   public void onClick(View v) {                big();            }         });    }          // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发    @Override    public boolean onDown(MotionEvent e) {        // TODO Auto-generated method stub//        Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();            Log.i(TAG, "onDown...");                return false;    }    /* 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN,      * 多个ACTION_MOVE, 1个ACTION_UP触发     * 参数解释:      * e1:第1个ACTION_DOWN MotionEvent      * e2:最后一个ACTION_MOVE MotionEvent      * velocityX:X轴上的移动速度,像素/秒      * velocityY:Y轴上的移动速度,像素/秒      * 触发条件 :      * X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒     * @see android.view.GestureDetector$OnGestureListener#onFling(android.view.MotionEvent, android.view.MotionEvent, float, float)     */    @Override    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,            float velocityY) {        // TODO Auto-generated method stub        Log.i(TAG, "onFling... e1.getX() = " + e1.getX() + ", e2.getX() = " + e2.getX());                if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {                // Fling left              Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();             } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE                     && Math.abs(velocityX) > FLING_MIN_VELOCITY) {                 // Fling right              Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();             }                  return false;    }    // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发     @Override    public void onLongPress(MotionEvent e) {        // TODO Auto-generated method stub        Log.i(TAG, "onLongPress...");            }    // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发    @Override    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,            float distanceY) {        // TODO Auto-generated method stub        Log.i(TAG, "onScroll...");                return false;    }    // 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发    // 注意和onDown()的区别,强调的是没有松开或者拖动的状态    @Override    public void onShowPress(MotionEvent e) {        // TODO Auto-generated method stub        Log.i(TAG, "onShowPress...");            }    // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发    @Override    public boolean onSingleTapUp(MotionEvent e) {        // TODO Auto-generated method stub        Log.i(TAG, "onSingleTapUp...");                return false;    }    @Override    public boolean onTouch(View v, MotionEvent event) {        // TODO Auto-generated method stub        Log.i(TAG, "onTouch...");                // Set button visible        mButton01.setVisibility(View.VISIBLE);        mButton02.setVisibility(View.VISIBLE);                return  mGestureDetector.onTouchEvent(event);        }        @Override    public boolean onKeyDown(int keyCode, KeyEvent event) {        // TODO Auto-generated method stub        super.onKeyDown(keyCode, event);                Log.i(TAG, "onKeyDown...");        // Set button visible        mButton01.setVisibility(View.VISIBLE);        mButton02.setVisibility(View.VISIBLE);                return true;    }    /* 图片缩小的method */    private void small()    {        int bmpWidth=bmp.getWidth();         int bmpHeight=bmp.getHeight();                Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);                /* 设置图片缩小的比例 */        double scale=0.8;        /* 计算出这次要缩小的比例 */         scaleWidth=(float) (scaleWidth*scale);         scaleHeight=(float) (scaleHeight*scale);         /* 产生reSize后的Bitmap对象 */        Matrix matrix = new Matrix();        matrix.postScale(scaleWidth, scaleHeight);        Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,                 bmpHeight,matrix,true);                 if(id==0)      {            /* 如果是第一次按,就删除原来默认的ImageView */            layoutImage.removeView(mImageView);        } else {            /* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */            layoutImage.removeView((ImageView)findViewById(id));        }                 /* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */        id++;        ImageView imageView = new ImageView(this);        imageView.setId(id);        imageView.setImageBitmap(resizeBmp);        imageView.setOnTouchListener(this);        imageView.setLongClickable(true);        layoutImage.addView(imageView);        Log.i(TAG, "imageView.getWidth() = " + imageView.getWidth()                + ", imageView.getHeight() = " + imageView.getHeight());        setContentView(layout1);        /* 因为图片放到最大时放大按钮会disable,所以在缩小时把它重设为enable */         mButton02.setEnabled(true);        mButton02.setTextColor(Color.MAGENTA);    }        /* 图片放大的method */    private void big() {        int bmpWidth=bmp.getWidth();        int bmpHeight=bmp.getHeight();                Log.i(TAG, "bmpWidth = " + bmpWidth + ", bmpHeight = " + bmpHeight);                /* 设置图片放大的比例 */        double scale=1.25;        /* 计算这次要放大的比例 */        scaleWidth=(float)(scaleWidth*scale);        scaleHeight=(float)(scaleHeight*scale);        /* 产生reSize后的Bitmap对象 */        Matrix matrix = new Matrix();        matrix.postScale(scaleWidth, scaleHeight);        Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth,                 bmpHeight,matrix,true);                if(id==0) {            /* 如果是第一次按,就删除原来设置的ImageView */            layoutImage.removeView(mImageView);        } else {            /* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */             layoutImage.removeView((ImageView)findViewById(id));        }                /* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */        id++;        ImageView imageView = new ImageView(this);        imageView.setId(id);        imageView.setImageBitmap(resizeBmp);        imageView.setOnTouchListener(this);        imageView.setLongClickable(true);        layoutImage.addView(imageView);        setContentView(layout1);        /* 如果再放大会超过屏幕大小,就把Button disable */        if( scaleWidth * scale * bmpWidth > bmpWidth * 2 ||            scaleHeight * scale * bmpHeight > bmpWidth * 2 ||            scaleWidth * scale * bmpWidth > displayWidth * 3 ||            scaleHeight * scale * bmpHeight > displayHeight * 3) {                mButton02.setEnabled(false);                mButton02.setTextColor(Color.GRAY);            } else {                mButton02.setEnabled(true);                mButton02.setTextColor(Color.MAGENTA);            }        }}
  相关解决方案