我需要每200ms更改ImageSwitcher(或任何其他更好的小部件)中的图像。我尝试使用AsyncTask并在onProgressUpdate()
方法中更新图像源。我也试过runOnUiThread()
,但两种方式都很慢。如何更快地显示图像?感谢。
更新
图库有大约15-20张图片。每幅图像的大小在150-200kb之间 - 分辨率1080x1920。 ImageSwitcher
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.test_layout, container, false);
imageSwitcher = (ImageSwitcher) view.findViewById(R.id.is_loginSwitcher);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(getActivity().getApplicationContext());
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
return imageView;
}
});
return view;
}
private class SwitchImages extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... values) {
int position = 0;
while (position < gallery.length - 1) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
publishProgress(position);
position++;
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
imageSwitcher.setImageResource(gallery[values[0]]);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageSwitcher
android:id="@+id/is_loginSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>