UploadProgressLoading.kt 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package com.cooleshow.base.widgets
  2. import android.app.Dialog
  3. import android.content.Context
  4. import android.graphics.drawable.AnimationDrawable
  5. import android.view.Gravity
  6. import android.widget.ImageView
  7. import android.widget.TextView
  8. import com.cooleshow.base.R
  9. import com.cooleshow.base.utils.LogUtils
  10. import com.cooleshow.base.utils.UiUtils
  11. import kotlinx.coroutines.Dispatchers
  12. import kotlinx.coroutines.GlobalScope
  13. import kotlinx.coroutines.launch
  14. import kotlinx.coroutines.withContext
  15. /**
  16. * 上传进度条
  17. */
  18. class UploadProgressLoading private constructor(context: Context, theme: Int) :
  19. Dialog(context, theme) {
  20. companion object {
  21. private lateinit var mDialog: UploadProgressLoading
  22. private var animDrawable: AnimationDrawable? = null
  23. private var tvProgress: TextView? = null;
  24. /*
  25. 创建加载对话框
  26. */
  27. fun create(context: Context): UploadProgressLoading {
  28. LogUtils.i("pq", "thread:" + Thread.currentThread().name);
  29. //样式引入
  30. mDialog = UploadProgressLoading(context, R.style.LightProgressDialog)
  31. //设置布局
  32. mDialog.setContentView(R.layout.upload_progress_dialog)
  33. mDialog.setCancelable(false)
  34. mDialog.setCanceledOnTouchOutside(false)
  35. mDialog.window?.attributes?.gravity = Gravity.CENTER
  36. val lp = mDialog.window?.attributes
  37. lp?.dimAmount = 0.2f
  38. //设置属性
  39. mDialog.window?.attributes = lp
  40. //获取动画视图
  41. val loadingView = mDialog.findViewById<ImageView>(R.id.iv_loading)
  42. tvProgress = mDialog.findViewById<TextView>(R.id.tv_progress)
  43. animDrawable = loadingView.background as AnimationDrawable
  44. return mDialog
  45. }
  46. }
  47. fun setProgress(progress: Double) {
  48. GlobalScope.launch(Dispatchers.Main) {
  49. tvProgress?.let {
  50. var result: String = UiUtils.convertDouble(progress);
  51. tvProgress?.text = "$result%"
  52. }
  53. }
  54. }
  55. fun setProgress(progress: Double, tip: String) {
  56. GlobalScope.launch(Dispatchers.Main) {
  57. tvProgress?.let {
  58. var result: String = UiUtils.convertDouble(progress);
  59. tvProgress?.text = "$tip$result%"
  60. }
  61. }
  62. }
  63. /*
  64. 显示加载对话框,动画开始
  65. */
  66. fun showLoading() {
  67. GlobalScope.launch(Dispatchers.Main) {
  68. super.show()
  69. animDrawable?.start()
  70. }
  71. }
  72. /*
  73. 隐藏加载对话框,动画停止
  74. */
  75. fun hideLoading() {
  76. GlobalScope.launch(Dispatchers.Main) {
  77. super.dismiss()
  78. animDrawable?.stop()
  79. }
  80. }
  81. fun stopAnim() {
  82. animDrawable?.stop()
  83. }
  84. }