一、 Animation动画实现
1、首先准备好动图资源,每帧一张图片,放到drawable下
2、编写帧动画的xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true"> <item android:drawable="@drawable/loading_01" android:duration="100" /> <item android:drawable="@drawable/loading_02" android:duration="100" /> <item android:drawable="@drawable/loading_03" android:duration="100" /> <item android:drawable="@drawable/loading_04" android:duration="100" /> <item android:drawable="@drawable/loading_05" android:duration="100" /> <item android:drawable="@drawable/loading_06" android:duration="100" /> <item android:drawable="@drawable/loading_07" android:duration="100" /> <item android:drawable="@drawable/loading_08" android:duration="100" /> <item android:drawable="@drawable/loading_09" android:duration="100" /> <item android:drawable="@drawable/loading_10" android:duration="100" /> <item android:drawable="@drawable/loading_11" android:duration="100" /> <item android:drawable="@drawable/loading_12" android:duration="100" /> </animation-list>
3、调用动画
xml调用:
<View android:id="@+id/loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/login_loading"/>
View view = findViewById(R.drawable.loading); AnimationDrawable animation = (AnimationDrawable)view.getBackground(); <pre name="code" class="java">animation<span style="font-family: Arial, Helvetica, sans-serif;">.start();</span>
代码调用:
View view = ... AnimationDrawable animation = (AnimationDrawable) getResources().getDrawable(R.drawable.loading); view.setBackgroundDrawable(animation); animation.start();
二、Volley加载图片动画实现
Volley默认的有一个方法
ImageLoader.getImageListener(iv, R.drawable.default_error, R.drawable.default_image);
来装载ImageView,第一个参数为ImageView控件,第二个参数为加载错误的图片,第三个参数为默认图片
里面的源码是这样的:
public static ImageListener getImageListener(final ImageView view, final int defaultImageResId, final int errorImageResId) { return new ImageListener() { @Override public void onErrorResponse(VolleyError error) { if (errorImageResId != 0) { view.setImageResource(errorImageResId); } } @Override public void onResponse(ImageContainer response, boolean isImmediate) { if (response.getBitmap() != null) { view.setImageBitmap(response.getBitmap()); } else if (defaultImageResId != 0) { view.setImageResource(defaultImageResId); } } }; }
如果图片正在获取懂,则先默认显示默认图,而要想在加载的时候显示动画,我想到的只能是自己重写listener了:
String url = "..." final ImageView iv = new ImageView(context); AnimationDrawable animation = (AnimationDrawable)context.getResources().getDrawable(R.drawable.loading); iv.setImageDrawable(animation); // 前景图为动画 animation.start(); ImageLoader.ImageListener listener = new ImageLoader.ImageListener() { @Override public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) { if(response.getBitmap()!=null){ iv.setImageBitmap(response.getBitmap()); }else{ iv.setBackgroundResource(R.drawable.default_image); //背景图为默认的一张图 } } @Override public void onErrorResponse(VolleyError error) { iv.setImageResource(R.drawable.default_error); } }; mImageLoader.get(url,listener);
用上了上面的Animation动画