加快替换图像
Speed up replacing images
我需要每 200 毫秒在 ImageSwitcher(或任何其他最好的小部件)中更改图像。我尝试使用 AsyncTask 并在 onProgressUpdate()
方法中更新图像源。我也试过 runOnUiThread()
但两种方式都很慢。如何更快地显示图像?谢谢
更新
图库中大约有 15-20 张图片。每张图片的大小在 150-200kb 之间 - 分辨率为 1080x1920。图片切换器
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>
我需要每 200 毫秒在 ImageSwitcher(或任何其他最好的小部件)中更改图像。我尝试使用 AsyncTask 并在 onProgressUpdate()
方法中更新图像源。我也试过 runOnUiThread()
但两种方式都很慢。如何更快地显示图像?谢谢
更新
图库中大约有 15-20 张图片。每张图片的大小在 150-200kb 之间 - 分辨率为 1080x1920。图片切换器
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>