/ / Como carregar imagens em um ImageSwitcher do servidor? - android, thread-safety, imageswitcher

Como carregar imagens em um ImageSwitcher do servidor? - android, segurança de thread, imageswitcher

Estou tentando carregar imagens em um switcher de imagensdo servidor Http. Não encontrei nenhuma função como setImageBitmap. Tentei usar setImageURI (), mas não foi carregado. Estou tentando mudar de imagem a cada 3 segundos. Este é o código. Quando estou executando, a imagem de códigos não está sendo carregada. E o aplicativo também está ficando danificado.

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

Respostas:

0 para resposta № 1

o que você pode fazer é primeiro pegar essas imagens e armazená-las em uma lista de bitmap para que você possa alternar essas imagens

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

e estou usando a animação de aparecimento e desaparecimento gradual que você pode definir de acordo com sua necessidade.