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

时间:2015-08-03 06:44:43

标签: android thread-safety imageswitcher

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

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

1 个答案:

答案 0 :(得分:0)

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

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

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