How to swipe images

前端 未结 7 1189
孤城傲影
孤城傲影 2021-02-05 14:34

I am trying to implement a simple gallery of images in which I have an image to be displayed at a time on the device screen. When we swipe the screen from left to right it shoul

7条回答
  •  青春惊慌失措
    2021-02-05 15:30

    You can do like that,It surly works without any view

    public class Swipe extends Activity implements OnClickListener {
    
    private static final int SWIPE_MIN_DISTANCE = 120;
    //private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    final GestureDetector gdt = new GestureDetector(new GestureListener());
    private GestureDetector gestureDetector;
    ImageView img;
    
    
    View.OnTouchListener gestureListener;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.swipe);
        img = (ImageView)findViewById(R.id.imageView1swipe);
    
        img.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(final View view, final MotionEvent event) {
                // TODO Auto-generated method stub
                gdt.onTouchEvent(event);
                //Log.i("Hello my Log 1","How dfgfd are you");
                return true;
            }
        });
    

    and declare this method in your .java

    private class GestureListener extends SimpleOnGestureListener {
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                Log.i("Hello my Log 2","How dfgfd are you");
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
    
                    Log.i("dfsd", "Right to left");
                    return false; // Right to left
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    return false; // Left to right
                }
    
                if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                    return false; // Bottom to top
                } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
                    return false; // Top to bottom
                }
    
                return false;
            }'
    

    May be this will help you for arranging all the swiping of Images

提交回复
热议问题