как хранить несколько изображений в Array и на одном ImageView, используя залп - PullRequest
0 голосов
/ 28 марта 2019

Я просто хочу поместить несколько фотографий в одну imageView, используя volley, который будет меняться / мигать автоматически каждые 3 секунды. это не ViewPager/slider.

int[] imageArray;
ImageView blinkImage;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_home, container,false);
    blinkImage=(ImageView)view.findViewById(R.id.hardImage);
    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        int i = 0;
        public void run() {
            blinkImage.setImageResource(imageArray[i]);
            i++;
            if (i > imageArray.length - 1) { i = 0; }handler.postDelayed(this,3000);
        }
    };
    handler.postDelayed(runnable,200);

        return view;
}

1 Ответ

1 голос
/ 28 марта 2019

используйте функцию rotateImage с обработчиком, чтобы определить интервал

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<startIndex){
                    currentIndex++;
                    nextImage();
                }else{
                    previousImage(); // here 1000(1 second) interval to change from current  to previous image 
                }
            }
        },1000);

    }

, или вы можете создать собственную анимацию кадра, например,

AnimationDrawable animation = new AnimationDrawable();
    animation.addFrame(getResources().getDrawable(R.drawable.image1), 100);
    animation.addFrame(getResources().getDrawable(R.drawable.image2), 500);
    animation.addFrame(getResources().getDrawable(R.drawable.image3), 300);
    animation.setOneShot(false);

    ImageView imageAnim =  (ImageView) findViewById(R.id.img);
    imageAnim.setBackgroundDrawable(animation);

    // start the animation!
    animation.start()
...