如何从服务器在 ImageSwitcher 中加载图像?

How to load images in an ImageSwitcher from server?

我正在尝试从 Http 服务器在图像切换器中加载图像。我没有找到像 setImageBitmap 这样的函数。所以我尝试使用 setImageURI() ,但它没有被加载。 我想每 3 秒切换一次图像。这是代码。当我 运行 代码图像未加载时。应用程序也变得疯狂。

 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);

你可以做的是首先获取这些图像并将其存储在位图的数组列表中,你可以切换这些图像

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);
}

我正在使用淡入淡出动画,您可以根据自己的需要进行设置。