前段時間做項目要求添加一個好看的加載動畫,找了很多,都沒有實作功能,後來還是自己找到的方法,具體代碼實作如下:
先看看效果圖
自定義一個loading類:
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;
import com.ycsoft.android.moviebar.R;
public class CustomeProgressDialog extends Dialog {
private static CustomeProgressDialog customProgressDialog = null;
public CustomeProgressDialog(Context context) {
super(context);
}
public CustomeProgressDialog(Context context, int theme) {
super(context, theme);
}
public static CustomeProgressDialog createDialog(Context context) {
customProgressDialog = new CustomeProgressDialog(context,
R.style.CustomProgressDialog);
customProgressDialog.setContentView(R.layout.customprogressdialog);
customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
customProgressDialog.setCanceledOnTouchOutside(false);
return customProgressDialog;
}
public void onWindowFocusChanged(boolean hasFocus) {
if (customProgressDialog == null) {
return;
}
ImageView imageView = (ImageView) customProgressDialog
.findViewById(R.id.loadingImageView);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView
.getBackground();
animationDrawable.start();
}
/**
*
* [Summary] setTitile 标題
*
* @param strTitle
* @return
*
*/
public CustomeProgressDialog setTitile(String strTitle) {
return customProgressDialog;
}
/**
*
* [Summary] setMessage 提示内容
*
* @param strMessage
* @return
*
*/
public CustomeProgressDialog setMessage(String strMessage) {
TextView tvMsg = (TextView) customProgressDialog
.findViewById(R.id.id_tv_loadingmsg);
if (tvMsg != null) {
tvMsg.setText(strMessage);
}
return customProgressDialog;
}
}
customprogressdialog.xml布局代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/loadingImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/progress_round"/>
<TextView
android:id="@+id/id_tv_loadingmsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="20dp"/>
</LinearLayout>
CustomProgressDialog style代碼如下:
<style name="CustomDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
<style name="CustomProgressDialog" parent="@style/CustomDialog">
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
</style>
progress_round.xml 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/progress_1" android:duration="200"/>
<item android:drawable="@drawable/progress_2" android:duration="200"/>
<item android:drawable="@drawable/progress_3" android:duration="200"/>
<item android:drawable="@drawable/progress_4" android:duration="200"/>
<item android:drawable="@drawable/progress_5" android:duration="200"/>
<item android:drawable="@drawable/progress_6" android:duration="200"/>
<item android:drawable="@drawable/progress_7" android:duration="200"/>
<item android:drawable="@drawable/progress_8" android:duration="60"/>
</animation-list>
圖檔下載下傳位址圖檔下載下傳位址
CustomeProgressDialog的使用方法
private CustomeProgressDialog customeProgressDialog;
customeProgressDialog = CustomeProgressDialog.createDialog(mContext);
//顯示加載動畫
customeProgressDialog.show();
//加載動畫消失
if (customeProgressDialog != null
&& customeProgressDialog.isShowing()) {
customeProgressDialog.dismiss();
}