Show multiple images in a imageView one after another with left to right flip effect repeatedly

后端 未结 6 1316
天命终不由人
天命终不由人 2021-02-09 20:16

Suppose I have multiple images in drawable folder(ex-8 images). I want to show all these images in a imageView one after another with left to right flip effect repeatedly(

6条回答
  •  孤街浪徒
    2021-02-09 20:58

    Use custom function rotate image using handler for interval to change image,here i change image vice verse direction :

        private ImageView image1;
        private int[] imageArray;
        private int currentIndex;
        private int startIndex;
        private int endIndex;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            image1 = (ImageView)findViewById(R.id.imageView1);
            imageArray = new int[8];
            imageArray[0] = R.drawable.one;
            imageArray[1] = R.drawable.two;
            imageArray[2] = R.drawable.three;
            imageArray[3] = R.drawable.four;
            imageArray[4] = R.drawable.five;
            imageArray[5] = R.drawable.six;
            imageArray[6] = R.drawable.seven;
            imageArray[7] = R.drawable.eight;
    
            startIndex = 0;
            endIndex = 7;
            nextImage();
    
    
        }
    
        public void nextImage(){
            image1.setImageResource(imageArray[currentIndex]);
            Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
            image1.startAnimation(rotateimage);
            currentIndex++;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(currentIndex>endIndex){
                        currentIndex--;
                        previousImage();
                    }else{
                        nextImage();
                    }
    
                }
            },1000); // here 1000(1 second) interval to change from current  to next image  
    
        }
        public void previousImage(){
            image1.setImageResource(imageArray[currentIndex]);
            Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);
            image1.startAnimation(rotateimage);
            currentIndex--;
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(currentIndex

提交回复
热议问题