Android自定义progressDialog实现载入动画
2014-07-14 21:42
344 查看
Android开发中,某些耗时操作一般会使用进度条之类的来等待加载,一般有两种:progressbar和progressDialog,区别在于前者是一个控件,后者是对话框。由于一些需求在弹出进度条时不希望用户 能够操作其他控件,所以只能使用progressDialog,这个时候有遇到了一个问题,我不想要系统progressDialog的黑色框框,这就需要我们自定义的progressDialog。
下面就是我自己写的一个自定义progressDialog。亲测通过!!!
1. 要在Android开发工程的res-drawable下放置动画的loading图片,比如:
2. 定义progressDialog的风格,设置风格为背景透明transparent,在工程values目录下的styles.xml中编写
4.在res-anim目录下编写loading.xml动画,需要我们事先放入到drawable中的动画图片
5.LoadingProgressDialog.java文件,这个是就是我们最终需要使用的progressDialog了。
至此,自定义的ProgressDialog就已经完成了,可以改变自己的loading图片来达到自己喜欢的加载动画的效果。
下面就是我自己写的一个自定义progressDialog。亲测通过!!!
1. 要在Android开发工程的res-drawable下放置动画的loading图片,比如:
2. 定义progressDialog的风格,设置风格为背景透明transparent,在工程values目录下的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="myProgressDialog" parent="@style/CustomDialog"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> </style> </resources>3. layout目录下编写loadingprogress.xml文件,定义自己的布局,一个进度条和一串显示的内容
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/loading_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@anim/loading" android:contentDescription="@null" /> <TextView android:id="@+id/progress_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/loading_iv" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:gravity="center" android:textColor="#000" /> </RelativeLayout>
4.在res-anim目录下编写loading.xml动画,需要我们事先放入到drawable中的动画图片
<?xml version="1.0" encoding="UTF-8"?> <animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android"> <item android:duration="150" android:drawable="@drawable/ic_loading_1" /> <item android:duration="150" android:drawable="@drawable/ic_loading_2" /> <item android:duration="150" android:drawable="@drawable/ic_loading_3" /> <item android:duration="150" android:drawable="@drawable/ic_loading_4" /> <item android:duration="150" android:drawable="@drawable/ic_loading_5" /> <item android:duration="150" android:drawable="@drawable/ic_loading_6" /> <item android:duration="150" android:drawable="@drawable/ic_loading_7" /> <item android:duration="150" android:drawable="@drawable/ic_loading_8" /> </animation-list>
5.LoadingProgressDialog.java文件,这个是就是我们最终需要使用的progressDialog了。
/** * * 页面加载动画类 * * @author WTY * */ public class LoadingProgressDialog extends Dialog { private Context context = null; private static LoadingProgressDialog customProgressDialog = null; public LoadingProgressDialog(Context context) { super(context); this.context = context; } public LoadingProgressDialog(Context context, int theme) { super(context, theme); } public static LoadingProgressDialog createDialog(Context context) { customProgressDialog = new LoadingProgressDialog(context,R.style.myProgressDialog); customProgressDialog.setContentView(R.layout.loadingprogress); customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER; return customProgressDialog; } public void onWindowFocusChanged(boolean hasFocus) { if (customProgressDialog == null) { return; } ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.loading_iv); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground(); animationDrawable.start(); } /** * * setTitile 标题 * * @param strTitle * @return * */ public LoadingProgressDialog setTitile(String strTitle) { return customProgressDialog; } /** * * setMessage 提示内容 * * @param strMessage * @return * */ public LoadingProgressDialog setMessage(String strMessage) { TextView tvMsg = (TextView) customProgressDialog.findViewById(R.id.progress_tv); if (tvMsg != null) { tvMsg.setText(strMessage); } return customProgressDialog; } }6. 编写加载页面的java文件Frgment_MainSearch.java,当搜索时在页面数据加载成功前需要显示上述编写的自定义加载动画
public class Fragment_MainSearch extends Fragment { private View mSearchView; private ImageButton mSearchBtn; private LoadingProgressDialog mProgress = null; private AsyncSearchDishTask mTask; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) return null; mSearchView = (View) inflater.inflate(R.layout.fragment_main_search,container, false); initView(); setListener(); return mSearchView; } private void initView() { mSearchBtn = (ImageButton) mSearchView.findViewById(R.id.search_submit); } private void setListener() { mSearchBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { new AsyncSearchDishTask().execute(); } }); } public class AsyncSearchDishTask extends AsyncTask<String, Integer, Object> { protected Object doInBackground(String... params) { //后续代码 } protected void onPostExecute(String result) { stopProgressDialog(); //后续代码 } protected void onPreExecute() { super.onPreExecute(); startProgressDialog(); } } private void startProgressDialog() { if (mProgress == null) { mProgress = LoadingProgressDialog.createDialog(getActivity()); mProgress.setMessage("努力加载中..."); } mProgress.show(); } private void stopProgressDialog() { if (mProgress != null) { mProgress.dismiss(); mProgress = null; } } }
至此,自定义的ProgressDialog就已经完成了,可以改变自己的loading图片来达到自己喜欢的加载动画的效果。
相关文章推荐
- Android自定义progressDialog实现 loading 载入中 动画效果
- Android自定义progressDialog实现loading载中动画效果
- 重写android.app.ProgressDialog实现自定义进度条弹出框布局
- Android 之 自定义progressdialog的实现
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu
- 【android】自定义ProgressDialog实现暂时隐藏进度值并显示等待状态(附源码下载)
- android 自定义progressDialog实现
- Android 自定义progressDialog实现
- Android 自定义progressDialog实现
- android 自定义progressDialog实现
- android 自定义ListView实现元素前移后退的动画特效
- 【多级树形菜单-dialog自定义动画弹出方式-手势监听】android实现treeView
- 在Android动画基础上实现自定义的动画效果
- Android中动画实现单击按钮控制开屏关屏效果(系统animation实现、scrollTo实现、自定义scrollTo实现)
- android 自定义progressDialog实现
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu
- android中自定义progressDialog实现
- Android UI设计之<二>自定义SwitchButton开关,实现类似IOS中UISwitch的动画效果
- Android自定义加载动画的实现