本文实例为大家分享了Android实现多张图片合成加载动画的具体代码,供大家参考,具体内容如下
1、自定义ProgressDialog
public class MyProgressDialog extends ProgressDialog {
private int procressLoadRes;
private ImageView mImageView;
private AnimationDrawable animationDrawable;
public MyProgressDialog(Context context,int procressLoadRes) {
super(context);
this.procressLoadRes = procressLoadRes;
}
public MyProgressDialog(Context context, int theme, int procressLoadRes) {
super(context, theme);
this.procressLoadRes = procressLoadRes;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_progress);
mImageView = (ImageView) findViewById(R.id.image);
//设置动画背景
mImageView.setBackgroundResource(procressLoadRes);
//获取动画对象,必须在上一步之后
animationDrawable = (AnimationDrawable) mImageView.getBackground();
mImageView.post(new Runnable() {
@Override
public void run() {
//启动动画
animationDrawable.start();
}
});
}
}
2、MyProgressDialog对应的布局layout_progress.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@null">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3、使用自定义的MyProgressDialog
public class MainActivity extends AppCompatActivity {
private Button button;
private MyProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
//传入style和anim文件
progressDialog = new MyProgressDialog(this,R.style.dialog,R.anim.loading);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressDialog.show();
}
});
}
}
4、使用时需要一个style文件和一个anim文件
style.xml
<style name="dialog" parent="@android:style/Theme.Dialog">
<!--边框-->
<item name="android:windowFrame">@null</item>
<!--是否浮现在activity之上-->
<item name="android:windowIsFloating">true</item>
<!--半透明-->
<item name="android:windowIsTranslucent">false</item>
<!--无标题-->
<item name="android:windowNoTitle">true</item>
<!--背景透明去掉背景色边框也就去掉了 -->
<item name="android:windowBackground">@color/transparent</item>
<!--模糊-->
<item name="android:backgroundDimEnabled">false</item>
</style>
anim文件夹下的loading.xml文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/loading1" android:duration="100"/>
<item android:drawable="@mipmap/loading2" android:duration="100"/>
<item android:drawable="@mipmap/loading3" android:duration="100"/>
<item android:drawable="@mipmap/loading4" android:duration="100"/>
<item android:drawable="@mipmap/loading5" android:duration="100"/>
<item android:drawable="@mipmap/loading6" android:duration="100"/>
<item android:drawable="@mipmap/loading7" android:duration="100"/>
<item android:drawable="@mipmap/loading8" android:duration="100"/>
<item android:drawable="@mipmap/loading9" android:duration="100"/>
<item android:drawable="@mipmap/loading10" android:duration="100"/>
<item android:drawable="@mipmap/loading11" android:duration="100"/>
<item android:drawable="@mipmap/loading12" android:duration="100"/>
<item android:drawable="@mipmap/loading13" android:duration="100"/>
<item android:drawable="@mipmap/loading14" android:duration="100"/>
<item android:drawable="@mipmap/loading15" android:duration="100"/>
<item android:drawable="@mipmap/loading16" android:duration="100"/>
<item android:drawable="@mipmap/loading17" android:duration="100"/>
<item android:drawable="@mipmap/loading18" android:duration="100"/>
<item android:drawable="@mipmap/loading19" android:duration="100"/>
<item android:drawable="@mipmap/loading20" android:duration="100"/>
<item android:drawable="@mipmap/loading21" android:duration="100"/>
<item android:drawable="@mipmap/loading22" android:duration="100"/>
<item android:drawable="@mipmap/loading23" android:duration="100"/>
<item android:drawable="@mipmap/loading24" android:duration="100"/>
<item android:drawable="@mipmap/loading25" android:duration="100"/>
<item android:drawable="@mipmap/loading26" android:duration="100"/>
<item android:drawable="@mipmap/loading27" android:duration="100"/>
<item android:drawable="@mipmap/loading30" android:duration="100"/>
<item android:drawable="@mipmap/loading31" android:duration="100"/>
<item android:drawable="@mipmap/loading32" android:duration="100"/>
<item android:drawable="@mipmap/loading33" android:duration="100"/>
<item android:drawable="@mipmap/loading34" android:duration="100"/>
</animation-list>
在color.xml文件中添加
<color name="transparent">#00ffffff</color>