Search code examples
androidthread-safetyimageswitcher

How to load images in an ImageSwitcher from server?


I m trying to load images in an image switcher from Http server. I didnot find any function like setImageBitmap. So I tried using setImageURI() , but its not getting loaded. I am tring to switch image after every 3 sec. This is the code. When i m running the codes image is not getting loaded. And app is also getting crased.

 String arr[]={"http://192.168.1.7/photos/dummy/1.jpg","http://192.168.1.7/photos/dummy/2.jpg","http://192.168.1.7/photos/dummy/3.jpg"}


 dailyWear = (ImageSwitcher) getActivity().findViewById(R.id.imageDailyWear);
dailyWear.setFactory(new ViewSwitcher.ViewFactory() {
        @Override
        public View makeView() {
            ImageView myView = new ImageView(getActivity());
            myView.setScaleType(ImageView.ScaleType.FIT_XY);
            myView.setLayoutParams(new ImageSwitcher.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
            return myView;
        }
    });
    dailyWear.setInAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_in_left));
    dailyWear.setOutAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.slide_out_right));

 final Handler handler = new Handler();
    final Runnable r = new Runnable() {

        int i=0;
        public void run() {
            weddingWear.setImageURI(Uri.parse(arr[i));

            i++;
            if (i >= arr.length()-1)
                i = 0;

            handler.postDelayed(this, 3000);
        }
    };

    handler.postDelayed(r, 1000);

Solution

  • what you can do is first get these images and store it in arraylist of bitmap that you can switch these images

    private Context mContext;
    private int index = 0;
    private final int interval = 3000;
    private final int DURATION=1500;
    
    
      public void animate_Images_In_Top_View_After_Every_Three_Seconds(
            ImageSwitcher imageSwitcher, final ArrayList<Bitmap> _Images_List) {
    
        android.view.animation.Animation aniIn = AnimationUtils.loadAnimation(mContext,
                android.R.anim.fade_in);
        aniIn.setDuration(DURATION);
        android.view.animation.Animation aniOut = AnimationUtils.loadAnimation(mContext,
                android.R.anim.fade_out);
        aniOut.setDuration(DURATION);
    
        final ImageSwitcher _ImageSwitcher = imageSwitcher;
        _ImageSwitcher.setInAnimation(aniIn);
        _ImageSwitcher.setOutAnimation(aniOut);
        _ImageSwitcher.setFactory((android.widget.ViewSwitcher.ViewFactory) mContext);
        _ImageSwitcher.setImageDrawable(new BitmapDrawable(_Images_List.get(index)));
        final Handler handler = new Handler();
        Runnable runnable = new Runnable() {
    
            @Override
            public void run() {
    
                    index++;
                    index = index % _Images_List.size();
              //      Log.d("Intro Screen", "Change Image " + index);
                    _ImageSwitcher.setImageDrawable(new BitmapDrawable(_Images_List.get(index)));
                    handler.postDelayed(this, interval);
    
            }
        };
        handler.postDelayed(runnable, interval);
    }
    

    and i am using fade in and out animation you can set to your own need.