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

Drag an ImageView horizontally to go to another Activity

我正在使用滑动 class 转到第二个 activity

Main.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);


        }


        });
        }

OnSwipeTouchListener.java

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() {
}

}

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.joker.charsoo.Main"
 >

<view
    android:id="@+id/view1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="ScrollView" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:orientation="vertical" >

<ImageView
    android:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="117dp"
    android:layout_marginTop="88dp"
    android:src="@drawable/ic_launcher" />

</LinearLayout>

当我滑动到顶部时,它将转到第二个 activity 但 imageView 没有移动!它很丑,它必须移动! 我想水平拖动图像然后转到第二个 activity.

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