как сохранить хранилище нескольких изображений в массиве и на одном ImageView с помощью volley

#android #json #imageview #android-volley #response

#Android #json #просмотр изображений #android-volley #ответ

Вопрос:

Я просто хочу поместить несколько фотографий в одну, 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:

используйте функцию 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()
  

Комментарии:

1. Tanvir, Но в этом я не хочу быть обработчиком использования, я хочу работать с volley, получать изображения с помощью API получения сервера.

2. Parveen в этом случае вы можете использовать второй подход. просто добавьте изображения, возвращенные вашим volley, в метод animation.addFrame(). это все, что вам нужно сделать.

3. Танвирчоудхури — не могли бы вы поделиться со мной каким-нибудь кодом для этого.

4. Попробуйте использовать это в методе post execute, где вы получаете изображение из volley AnimationDrawable animation = new AnimationDrawable(); Runnable runnable = new Runnable() { int i = 0; public void run() { animation.addFrame(imageArray[i]), 200); ImageView imageAnim = (ImageView) findViewById(R.id.img); imageAnim. setBackgroundDrawable(анимация); animation.start() i ; if (i > imageArray.length — 1) { i = 0; } handler.postDelayed(this,3000); } }; handler.postDelayed(runnable,200);