网络加载时需要一段时间,这段时间可以加载一个加载中对话框,这里定义了两个,一个是加载普通图片做成的桢动画,一个是git动画
(1)CustomProgressDialog.java
/************************************************************************************** * [Project] * MyProgressDialog * [Package] * com.lxd.widgets * [FileName] * CustomProgressDialog.java * [Copyright] * Copyright 2012 LXD All Rights Reserved. * [History] * Version Date Author Record *-------------------------------------------------------------------------------------- * 1.0.0 2012-4-27 lxd ([email protected]) Create **************************************************************************************/ package com.lxd.widgets; import com.lxd.activity.R; 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; /******************************************************************** * [Summary] * TODO 请在此处简要描述此类所实现的功能。因为这项注释主要是为了在IDE环境中生成tip帮助,务必简明扼要 * [Remarks] * TODO 请在此处详细描述类的功能、调用方法、注意事项、以及与其它类的关系. *******************************************************************/ public class CustomProgressDialog extends Dialog { private Context context = null; private static CustomProgressDialog customProgressDialog = null; public CustomProgressDialog(Context context){ super(context); this.context = context; //外部可以点击消失 setCanceledOnTouchOutside(true); } public CustomProgressDialog(Context context, int theme) { super(context, theme); //外部可以点击消失 setCanceledOnTouchOutside(true); } public static CustomProgressDialog createDialog(Context context){ customProgressDialog = new CustomProgressDialog(context,R.style.CustomProgressDialog); customProgressDialog.setContentView(R.layout.customprogressdialog); customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER; 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 CustomProgressDialog setTitile(String strTitle){ return customProgressDialog; } /** * * [Summary] * setMessage 提示内容 * @param strMessage * @return * */ public CustomProgressDialog setMessage(String strMessage){ TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.id_tv_loadingmsg); if (tvMsg != null){ tvMsg.setText(strMessage); } return customProgressDialog; } }
(2)CustomProgressDialogWithGif.java
/************************************************************************************** * [Project] * MyProgressDialog * [Package] * com.lxd.widgets * [FileName] * CustomProgressDialog.java * [Copyright] * Copyright 2012 LXD All Rights Reserved. * [History] * Version Date Author Record *-------------------------------------------------------------------------------------- * 1.0.0 2012-4-27 lxd ([email protected]) Create **************************************************************************************/ package com.lxd.widgets; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.view.Gravity; import android.widget.TextView; import com.lxd.activity.R; import com.lxd.view.gifview.GifView; /******************************************************************** * [Summary] * TODO 请在此处简要描述此类所实现的功能。因为这项注释主要是为了在IDE环境中生成tip帮助,务必简明扼要 * [Remarks] * TODO 请在此处详细描述类的功能、调用方法、注意事项、以及与其它类的关系. *******************************************************************/ /** * 加入gif动画 * * @author chenwenbiao * @date 2014-4-24 下午5:57:16 * @version V1.0 */ public class CustomProgressDialogWithGif extends Dialog{ private GifView gifView = null; private TextView tvMsg = null; public CustomProgressDialogWithGif(Context context , String strMessage){ this(context,R.style.CustomProgressDialog , strMessage); } public CustomProgressDialogWithGif(Context context, int theme , String strMessage) { super(context, theme); setContentView(R.layout.customprogressdialog_with_gif); getWindow().getAttributes().gravity = Gravity.CENTER; gifView = (GifView)findViewById(R.id.loading_anim_gif); tvMsg = (TextView)findViewById(R.id.loading_tips_tv); if (strMessage != null){ tvMsg.setText(strMessage); } setOnDismissListener(new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { destroyGif(); } }); gifView.setGifImage(R.drawable.loading4); gifView.setLoopAnimation(); } /** * 记得要消毁gifview,不然程序会卡死 */ private void destroyGif() { if (gifView != null) { gifView.destroy(); } gifView = null; } }
(3)MainActivity.java
package com.lxd.activity; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import com.lxd.widgets.CustomProgressDialog; import com.lxd.widgets.CustomProgressDialogWithGif; public class MainActivity extends Activity { private String TAG = getClass().getName(); // private CustomProgressDialogWithGif gifProgressDialog = null; private CustomProgressDialog imageProgressDialog = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏 setContentView(R.layout.main); View gifBtn = findViewById(R.id.gif_custom_preogeress_dialog_btn); gifBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { /** * gif图片占用内存大,需要及时释放,这里到的时候就重新生成,对话框一关闭,自动调用 */ CustomProgressDialogWithGif gifProgressDialog = new CustomProgressDialogWithGif(MainActivity.this , "正在加载中..."); gifProgressDialog.show(); } }); View imageBtn = findViewById(R.id.image_custom_preogeress_dialog_btn); imageProgressDialog = CustomProgressDialog.createDialog(this); imageProgressDialog.setMessage("正在加载中..."); imageBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { imageProgressDialog.show(); } }); } }
这里值得注意是gif图片加载要及时消毁,要用就new出来。
styles.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 自定义弹窗的样式 --> <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">@color/transparent</item> <item name="android:windowNoTitle">true</item> </style> </resources>
效果图:
(1)主界面
(2)普通图片主界
(3)gif动画界面