Вот что я использую, я беру значок приложения и поворачиваю его в середине экрана, чтобы показать, как прогресс, это выглядит лучше, чем просто отображение индикатора выполнения. Если вы все еще хотите прогрессбар, просто замените ImageView
на ProgressBar
import android.animation.ObjectAnimator
import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.Gravity
import android.view.ViewGroup
import android.view.Window
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
class MyProgressDialog : Dialog {
private var dialog: Dialog? = null
var imageView: ImageView? = null
internal set
internal var textView: TextView? = null
constructor(context: Context) : super(context) {
init(context)
}
constructor(context: Context, themeResId: Int) : super(context, themeResId) {
init(context)
}
protected constructor(context: Context, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener?) : super(context, cancelable, cancelListener) {
init(context)
}
fun init(context: Context) {
dialog = this
dialog!!.window!!.requestFeature(Window.FEATURE_NO_TITLE)
dialog!!.setCancelable(false)
val relativeLayout = RelativeLayout(context)
val layoutParams = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
relativeLayout.layoutParams = layoutParams
val layoutParams_for_linear = RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
layoutParams_for_linear.addRule(RelativeLayout.CENTER_IN_PARENT)
val linearLayout = LinearLayout(context)
linearLayout.layoutParams = layoutParams_for_linear
linearLayout.orientation = LinearLayout.VERTICAL
relativeLayout.addView(linearLayout)
val layoutParams_Linear = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
layoutParams_Linear.gravity = Gravity.CENTER
imageView = ImageView(context)
imageView!!.setImageResource(R.mipmap.ic_launcher)
imageView!!.layoutParams = layoutParams_Linear
linearLayout.addView(imageView)
textView = TextView(context)
textView!!.layoutParams = layoutParams_for_linear
textView!!.setTextColor(Color.parseColor("#ffffff"))
textView!!.textSize = 18f
linearLayout.addView(textView)
val animation = ObjectAnimator.ofFloat(imageView, "rotationY", 0.0f, 360f)
animation.duration = 2500
animation.repeatCount = ObjectAnimator.INFINITE
animation.interpolator = AccelerateDecelerateInterpolator()
animation.start()
dialog!!.window!!.setContentView(relativeLayout, layoutParams)
dialog!!.window!!.setBackgroundDrawable(
ColorDrawable(android.graphics.Color.TRANSPARENT))
}
fun setProgressMessage(message: String): Dialog {
if (textView != null)
textView!!.text = message
return this
}
fun setProgressMessageSize(size: Int): Dialog {
if (textView != null)
textView!!.textSize = size.toFloat()
return this
}
fun setProgressMessageColour(colour: Int): Dialog {
if (textView != null)
textView!!.setTextColor(colour)
return this
}
fun setIcon(resId: Int): Dialog {
if (imageView != null) {
imageView!!.setImageResource(resId)
}
return this
}
}
Чтобы показать и скрыть Progress, я использую эти методы
private var dialog: Dialog? = null
fun showProgress(context: Context?): Dialog? {
if (context is Activity && (dialog == null || !dialog!!.isShowing)) {
try {
dialog = MyProgressDialog(context)
dialog?.show()
} catch (e: Exception) {
e.printStackTrace()
}
}
return dialog
}
fun dismissProgress() {
try {
if (dialog != null && dialog!!.isShowing) {
dialog?.dismiss()
}
} catch (e: Exception) {
e.printStackTrace()
}
dialog = null
}