我正在使用Volley库中的NetworkImageView来加载图片。
但是当图片从URL加载需要一些时间时,我希望在图片容器中显示一个旋转/移动的加载符号。
我试图使用setDefaultImageResId()函数设置一个loader.gif图像。但我认为gif格式在Android中不是原生支持的。
请给予建议。谢谢。
我正在使用Volley库中的NetworkImageView来加载图片。
但是当图片从URL加载需要一些时间时,我希望在图片容器中显示一个旋转/移动的加载符号。
我试图使用setDefaultImageResId()函数设置一个loader.gif图像。但我认为gif格式在Android中不是原生支持的。
请给予建议。谢谢。
NetworkImageView
和 ProgressBar
放在一个 FrameLayout
或 RelativeLayout
中,就像这样:<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ProgressBar
style="?android:attr/progressBarStyle"
android:layout_width="..."
android:layout_height="..." />
<com.android.volley.toolbox.NetworkImageView
android:layout_width="..."
android:layout_height="..."
/>
</FrameLayout>
请注意顺序很重要,确保你将NetworkImageView
放在ProgressBar
后面,这样在加载图像后NetworkImageView
会停留在ProgressBar
的上方。
希望能帮到你!
// ** code in init the progress wheel **
imageLoader.get(url, new ImageListener() {
@Override
public void onResponse(ImageContainer response, boolean isImmediate) {
if (response != null) {
Bitmap bitmap = response.getBitmap();
if (bitmap != null) {
// ** code to turn off the progress wheel **
// ** code to use the bitmap in your imageview **
}
@Override
public void onErrorResponse(VolleyError error) {
// ** code to handle errors **
}
});
NetworkImageView
是一个简单的ImageView
,它使用你提供的ImageLoader
从你给定的URL加载图像。这是我建议的一种语法糖,但你无法控制侦听器。你可以按照我建议的方式或通过编辑源代码并将你的逻辑添加到那里的侦听器来实现。无论哪种方式,你都需要一个钩子进入ImageListener
,别无选择。 - Itai Hanski我使用动画可绘制对象完成了这个操作。以下是步骤:
对可绘制对象ic_sync_drawable.xml进行动画处理:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/selected"
android:oneshot="false">
<item
android:drawable="@drawable/ic_popup_sync_1"
android:duration="50" />
<item
android:drawable="@drawable/ic_popup_sync_2"
android:duration="50" />
<item
android:drawable="@drawable/ic_popup_sync_3"
android:duration="50" />
<item
android:drawable="@drawable/ic_popup_sync_4"
android:duration="50" />
<item
android:drawable="@drawable/ic_popup_sync_5"
android:duration="50" />
<item
android:drawable="@drawable/ic_popup_sync_6"
android:duration="50" />
</animation-list>
现在在Java代码中我正在使用ImageView:
Drawable drawable = getContext().getResources().getDrawable(R.drawable.ic_sync_drawable);
ImageView imageView = new ImageView(getContext());
imageView.setImageDrawable(drawable);
imageLoader.get(imageurl, ImageLoader.getImageListener(imageView, 0, 0));
ViewFlipper.addView(imageView);
//start animation
AnimationDrawable frameAnimation = (AnimationDrawable) drawable;
frameAnimation.start();