水平拖动ImageView以转到另一个Activity

时间:2015-04-24 11:46:14

标签: java android swipe drag

我正在使用滑动课程进入第二项活动

Main.Java

hdfs dfs -cat

OnSwipeTouchListener.java

public class Main extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    View v = findViewById(R.id.main);


    v.setOnTouchListener(new OnSwipeTouchListener(this) {
        public void onSwipeTop() {

            Intent i = new Intent(Main.this, Second.class);
            startActivity(i);

        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);

                v.startDrag(null, shadowBuilder, v, 0);
                v.setVisibility(View.INVISIBLE);
            }
            return gestureDetector.onTouchEvent(event);


        }


        });
        }

}

main.xml中

public abstract class OnSwipeTouchListener implements OnTouchListener {

protected final GestureDetector gestureDetector;

public OnSwipeTouchListener (Context ctx){
    gestureDetector = new GestureDetector(ctx, new GestureListener());
}

private final class GestureListener extends SimpleOnGestureListener {

    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

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

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        boolean result = false;
        try {
            float diffY = e2.getY() - e1.getY();
            float diffX = e2.getX() - e1.getX();
            if (Math.abs(diffX) > Math.abs(diffY)) {
                if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffX > 0) {
                        onSwipeRight();
                    } else {
                        onSwipeLeft();
                    }
                }
                result = true;
            } 
            else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                    if (diffY > 0) {
                        onSwipeBottom();
                    } else {
                        onSwipeTop();
                    }
                }
                result = true;

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return result;
    }
}

public void onSwipeRight() {

}

public void onSwipeLeft() {
}

public void onSwipeTop() {
}

public void onSwipeBottom() {
}

当我向上滑动时,它将转到第二个活动,但imageView不会移动!它很难看,它必须移动! 我想水平拖动图像然后转到第二个活动。

1 个答案:

答案 0 :(得分:0)

你可以使用imgView.setTranslation(e1.getX()); 将图像从右向左水平移动。 PS。最低API等级为11。 或者您可以将此教程用于图像滑块: http://www.androidhive.info/2013/09/android-fullscreen-image-slider-with-swipe-and-pinch-zoom-gestures/ 希望这会对你有所帮助