用帧动画做加载动画
程序员文章站
2022-03-25 23:35:03
...
首先,上anim的列表文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@mipmap/jiazai_a" android:duration="200" />
<item android:drawable="@mipmap/jiazai_b" android:duration="200" />
<item android:drawable="@mipmap/jiazai_c" android:duration="200" />
<item android:drawable="@mipmap/jiazai_d" android:duration="200" />
<item android:drawable="@mipmap/jiazai_e" android:duration="200" />
<item android:drawable="@mipmap/jiazai_f" android:duration="200" />
<item android:drawable="@mipmap/jiazai_g" android:duration="200" />
<item android:drawable="@mipmap/jiazai_h" android:duration="200" />
</animation-list>
接下来,相关的工具类
package com.hipad.ht.portal.ui.view;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;
import com.hipad.ht.portal.R;
public class LoadingAinm {
private static AnimationDrawable animationDrawable;
public static void initLodingView(View view) {
ImageView loadingImageView = (ImageView) view.findViewById(R.id.img_loading);
loadingImageView.setBackgroundResource(R.drawable.anim_loading);
animationDrawable = (AnimationDrawable) loadingImageView.getBackground();
loadingImageView.post(new Runnable() {
@Override
public void run() {
animationDrawable.start();
}
});
}
public static void hideView(View view) {
if(view==null||animationDrawable==null)
return;
animationDrawable.stop();
animationDrawable=null;
}
public static void initLoding(Activity activity) {
ImageView loadingImageView = (ImageView) activity.findViewById(R.id.img_loading);
loadingImageView.setBackgroundResource(R.drawable.anim_loading);
final AnimationDrawable animationDrawable = (AnimationDrawable) loadingImageView.getBackground();
loadingImageView.post(new Runnable() {
@Override
public void run() {
animationDrawable.start();
}
});
}
}
最后只要在想调用的地方调用就可以了
上面的方式有时候会因为各种原因失效,现在贴出第二种方式,在需要的地方添加上布局文件
<FrameLayout
android:id="@+id/first_loading_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/pb"
android:layout_gravity="center"
style="@style/myProgressBarStyleLarge"
android:layout_width="66dp"
android:layout_height="11dp" />
</FrameLayout>
在文中需要的地方直接调用
first_loading_content = rootView.findViewById(R.id.first_loading_content);
first_loading_content.setVisibility(View.VISIBLE);
Style文件中配置
<style name="myProgressBarStyleLarge" >
<item name="android:indeterminateDrawable">@drawable/anim_loading</item>
<item name="android:minWidth">40dip</item>
<item name="android:maxWidth">40dip</item>
<item name="android:minHeight">40dip</item>
<item name="android:maxHeight">40dip</item>
</style>