自定义 非常好用水平进度条工具类
来源:互联网 发布:sql注入语句示例大全 编辑:程序博客网 时间:2024/05/18 11:39
public class DialogUtils { private static final Logger logger = LoggerFactory.getLogger(DialogUtils.class); public static void indeterminate(Context context, Handler handler, String message, final Runnable runnable, OnDismissListener dismissListener) { try { indeterminateInternal(context, handler, message, runnable, dismissListener, true); } catch (Exception e) { } } public static void indeterminate(Context context, Handler handler, String message, final Runnable runnable, OnDismissListener dismissListener, boolean cancelable) { try { indeterminateInternal(context, handler, message, runnable, dismissListener, cancelable); } catch (Exception e) { } } private static ProgressDialog createProgressDialog(Context context, String message) { ProgressDialog dialog = new ProgressDialog(context); dialog.setIndeterminate(false); dialog.setMessage(message); return dialog; } private static void indeterminateInternal(Context context, final Handler handler, String message, final Runnable runnable, OnDismissListener dismissListener, boolean cancelable) { final ProgressDialog dialog = createProgressDialog(context, message); dialog.setCancelable(cancelable); if (dismissListener != null) { dialog.setOnDismissListener(dismissListener); } dialog.show(); new Thread() { @Override public void run() { runnable.run(); handler.post(new Runnable() { @Override public void run() { try { dialog.dismiss(); } catch (Exception e) { } } }); } }.start(); } /** * 创建加载对话框 * @param context * @param msg * @return */ public static LoadingDialog createLoadingDialog(Context context, String msg) { LoadingDialog loadingDialog = new LoadingDialog(context); loadingDialog.setMsg(msg); return loadingDialog; } public static String getProcessName(Context context) { ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) { if (processInfo.pid == android.os.Process.myPid()) { return processInfo.processName; } } return ""; } public static class LoadingDialog extends Dialog { final ClipDrawable drawable; Handler handler; //有进度 ImageView postionImage; //背景 ImageView bgImage; //无进度 ImageView moveImage; //进度提示 TextView tvPostion; private TextView tipTextView; //进度条类型 True 有进度 False 无进度 private boolean isPostion = false; //进度条最大值 private int maxPostion = 100; //当前进度 private int curPostion; public LoadingDialog(Context context) { super(context, com.pemt.android.R.style.loading_dialog); LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(com.pemt.android.R.layout.loading_dialog, null); LinearLayout layout = (LinearLayout) v.findViewById(com.pemt.android.R.id.ll_main); bgImage = (ImageView) v.findViewById(com.pemt.android.R.id.iv_progressbar_bg); postionImage = (ImageView) v.findViewById(com.pemt.android.R.id.iv_progressbar_site); moveImage = (ImageView) v.findViewById(com.pemt.android.R.id.iv_progressbar_move); tvPostion = (TextView) v.findViewById(com.pemt.android.R.id.tv_postion); //获取图片所显示的ClipDrawble对象 drawable = (ClipDrawable) postionImage.getBackground(); handler = new Handler() { public void handleMessage(Message msg) { if (msg.what == 0x1233) { //修改ClipDrawable的level值 drawable.setLevel(msg.arg1); } } }; tipTextView = (TextView) v.findViewById(com.pemt.android.R.id.tv_msg); tipTextView.setText("");// 设置加载信息 setCancelable(false);// 不可以用“返回键”取消 setContentView(layout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局 } private void showPostion(int level) { if (level > 1000) level = 1000; if (level < 0) level = 0; drawable.setLevel(level); } @Override public void dismiss() { handler.post(new Runnable() { @Override public void run() { //关闭动画 moveImage.clearAnimation(); } }); super.dismiss(); } @Override public void show() { if (isShowing()) { return; } super.show(); if (isPostion) { drawable.setLevel(0); moveImage.setVisibility(View.GONE); postionImage.setVisibility(View.VISIBLE); tvPostion.setVisibility(View.VISIBLE); } else { moveImage.setVisibility(View.VISIBLE); postionImage.setVisibility(View.GONE); tvPostion.setVisibility(View.GONE); assert bgImage.getViewTreeObserver() != null; bgImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { slideview(moveImage, 3, bgImage.getWidth() - moveImage.getWidth() - 4); if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { bgImage.getViewTreeObserver().removeOnGlobalLayoutListener(this); } else { bgImage.getViewTreeObserver().removeGlobalOnLayoutListener(this); } } }); } } //设置进度条类型 public void setProgressType(Boolean isPostion) { this.isPostion = isPostion; } //设置进度条的最大值 public Boolean setMaxPostion(int postion) { if (postion < 1) { postion = 100; this.maxPostion = postion; return false; } this.maxPostion = postion; setPostion(curPostion); return true; } //设置进度条的当前位置 public void setPostion(int postion) { this.curPostion = postion; int num = postion * 10000 / maxPostion; if (num > 10000) num = 10000; if (num < 0) num = 0; Message msg = new Message(); msg.what = 0x1233; msg.arg1 = num; //发送消息,通知应用修改ClipDrawable对象的level值 handler.sendMessage(msg); num = num / 100; tvPostion.setText(num + "%"); } //动画设置 public void slideview(final ImageView imageView, final float p1, final float p2) { TranslateAnimation animation = new TranslateAnimation(p1, p2, 0, 0); //添加了这行代码的作用时,view移动的时候 会有弹性效果 //animation.setInterpolator(new OvershootInterpolator()); //设置动画持续 animation.setDuration(4500); animation.setStartOffset(0); animation.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { slideview(moveImage, 3, bgImage.getWidth() - moveImage.getWidth() - 4); } }); imageView.startAnimation(animation); } public void setMsg(String msg) { tipTextView.setText(msg); } }}
用法:
例:
public class LoginActivity extends Activity { private static final Logger logger = LoggerFactory.getLogger(LoginActivity.class); private DialogUtils.LoadingDialog loadingDialog; private volatile Future future = null; private SharedPreferences pref; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); pref = PreferenceManager.getDefaultSharedPreferences(this); loadingDialog = DialogUtils.createLoadingDialog(this, "获取用户名...");//自定义进度条 loadingDialog.setCancelable(true); loadingDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { if (future != null && !future.isDone() && !future.isCancelled()) { future.cancel(true); } } }); loadingDialog.show(); }// @UiThread void showDialogMsg(String msg) { loadingDialog.setMsg(msg); if (!isFinishing() && !loadingDialog.isShowing()) { loadingDialog.show(); } } void dismissDialog() { if (!isFinishing() && loadingDialog != null && loadingDialog.isShowing()) { loadingDialog.dismiss(); } }}
阅读全文
0 0
- 自定义 非常好用水平进度条工具类
- 自定义水平进度条
- android 自定义水平进度条
- 自定义水平进度条
- Android自定义水平进度条
- 自定义水平进度条
- 简单的自定义水平进度条
- 自定义水平进度条Horizontal ProgressBar
- 自定义水平进度条(颜色)
- Android自定义水平进度条控件
- 自定义进度条(水平,圆形)
- Android自定义水平渐变进度条
- 自定义进度条(水平进度条和圆形进度条)
- 时间格式化工具类,非常好用
- 自定义水平进度条样式:黑色虚线
- Android ProgressBar高级自定义水平/圆形进度条
- android自定义view实现水平进度条
- Progress水平进度条的自定义样式
- PPT控件Spire.Presentation 教程:在PPT中设置表行高度和列宽度
- Linux下编译原理
- spring boot中使用generator插件自动生成代码
- 一个很基础的surfaceView使用
- Python接口测试之moco(八)
- 自定义 非常好用水平进度条工具类
- 排序
- 算法学习记录七(C++)--->二分法找有序旋转数组最小值
- Floyd+Bellman-ford求正环 hdu1317 XYZZY
- Lucene(三)索引域选项
- 二叉树
- MongoDB 数据同步至 ElasticSearch (Mongo-connector)
- SpringMvc/SpringBoot HTTP通信加解密
- iOS中自定义button、button属性、swift