How to swipe images

前端 未结 7 1190
孤城傲影
孤城傲影 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:25

    You can do like this way also, without using view flipper

    
     
    
    
    
     
    

    Activity Class

     public class MainActivity extends Activity  {
    
     private Integer[] mImageIds = { R.drawable.img1, R.drawable.img2,R.drawable.img3 };
    
    private static final String DEBUG_TAG = "MainActivity ";
    ImageView imageView;
    float startXValue = 1;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.gallery);
        imageView = (ImageView) findViewById(R.id.image_place_holder);
        imageView.setImageResource(mImageIds[num]);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {    
        float endXValue = 0;
        float x1 = event.getAxisValue(MotionEvent.AXIS_X);
        int action = MotionEventCompat.getActionMasked(event);
        switch (action) {
            case (MotionEvent.ACTION_DOWN):
                startXValue = event.getAxisValue(MotionEvent.AXIS_X);
    
                return true;
    
            case (MotionEvent.ACTION_UP):
                endXValue = event.getAxisValue(MotionEvent.AXIS_X);
                if (endXValue > startXValue) {
                    if (endXValue - startXValue > 100) {
                    System.out.println("Left-Right");
                    imageView.setImageResource(mImageIds[2]);
                    }
                }else {
                    if (startXValue -endXValue> 100) {
                    System.out.println("Right-Left");
                    imageView.setImageResource(mImageIds[0]);
    
                         }
                             }
            return true;
    
    
               default:
            return super.onTouchEvent(event);
         }
    
    }
    
    }
    

提交回复
热议问题