从左到右滑动,从屏幕左侧平稳打开相机。怎么做到这一点?

时间:2018-02-15 07:17:54

标签: android android-camera android-gesture

我正在开发一款相机,其中包括从左到右的手势滑动,它会打开相机。 但是,相机不能正常工作,而且它正从屏幕右侧打开。

我应该怎么做才能从左边打开它还顺利?

以下是我正在使用的代码,

class SwipeGestureListener extends GestureDetector.SimpleOnGestureListener implements
        View.OnTouchListener {
    Context context;
    GestureDetector gDetector;
    static final int SWIPE_MIN_DISTANCE = 120;
    static final int SWIPE_MAX_OFF_PATH = 250;
    static final int SWIPE_THRESHOLD_VELOCITY = 200;

    public SwipeGestureListener() {
        super();
    }

    public SwipeGestureListener(Context context) {
        this(context, null);
    }

    public SwipeGestureListener(Context context, GestureDetector gDetector) {

        if (gDetector == null)
            gDetector = new GestureDetector(context, this);

        this.context = context;
        this.gDetector = gDetector;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                           float velocityY) {

        try{
            if (Math.abs(velocityX) < SWIPE_THRESHOLD_VELOCITY) {
                return false;
            }
            if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
                Toast.makeText(HomeActivity.this,
                        "swipe RightToLeft ", Toast.LENGTH_SHORT).show();
            } else if ((e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE&&e2.getY()-e1.getY()<100)&&e1.getX()<30) {
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    askForcameraPermission();
                } else {
                    startCamera();
                }
            }
        }catch(Exception e){

        }

        return super.onFling(e1, e2, velocityX, velocityY);

    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        return gDetector.onTouchEvent(event);
    }

    public GestureDetector getDetector() {
        return gDetector;
    }

}

class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
    private static final int SWIPE_MIN_DISTANCE = 20;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 10;
    @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) {

            } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    askForcameraPermission();
                } else {
                    startCamera();
                }
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }

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

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }
}

我的参考是Instagram应用程序,它从左到右滑动打开相机。

0 个答案:

没有答案