Android自定义加载框效果

本文实例为大家分享了Android自定义加载框效果的具体代码,供大家参考,具体内容如下

效果图

Android自定义加载框效果_第1张图片

菊花图标(mipmap-xxhdpi)

Android自定义加载框效果_第2张图片

加载框圆角背景drawable



    
    

布局文件




    

    

    

        

        
    

LoadingDialog.kt

package com.lzk.libcommon.widget

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.animation.LinearInterpolator
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.blankj.utilcode.util.LogUtils
import com.lzk.libcommon.R
import com.lzk.libcommon.databinding.ViewLoadingDialogBinding

/**
 * @Author: LiaoZhongKai
 * @Date: 2021/7/14 9:04
 * @Description: 加载框
 */
class LoadingDialog: DialogFragment() {

    private var mTips: String? = null
    private lateinit var mLoadingDialogBinding: ViewLoadingDialogBinding
    private lateinit var mAnimation: ObjectAnimator

    override fun onStart() {
        super.onStart()
        //去掉DialogFragment外部的背景色
        dialog?.window?.apply {
            attributes = attributes.apply {
            //======================这里设置背景阴影透明度===============
            //======================0是全透明===========================
                dimAmount = 0.0f
            }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mLoadingDialogBinding = DataBindingUtil.inflate(inflater, R.layout.view_loading_dialog,container,false)

        dialog?.apply {
            requestWindowFeature(Window.FEATURE_NO_TITLE)
            setCanceledOnTouchOutside(false)
            window?.apply {
                //去掉DialogFragment内部的背景色
                setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
                //去掉Padding
                decorView.setPadding(0,0,0,0)
            }
        }
        return mLoadingDialogBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        rotate()
        setTips(mTips)
    }

    private fun rotate(){
        mAnimation = ObjectAnimator.ofFloat(mLoadingDialogBinding.iv,"rotation",360f).apply {
            repeatCount = ObjectAnimator.INFINITE
            repeatMode = ValueAnimator.RESTART
            interpolator = LinearInterpolator()
            duration = 1000
        }
        mAnimation.start()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mAnimation.cancel()
    }

    //显示
    fun showDialog(fragmentManager: FragmentManager, msg: String? = null){
        mTips = msg
        if (isVisible){
            dismiss()
        }
        show(fragmentManager,"")
    }
    //隐藏
    fun dismissDialog(){
        if (isAdded){
            dismiss()
            mTips = null
        }
    }

    /**
     * 设置加载提示文字
     */
    private fun setTips(msg: String?): LoadingDialog{
        mLoadingDialogBinding.tv.visibility = if (msg.isNullOrEmpty()) View.GONE else View.VISIBLE
        if (!msg.isNullOrEmpty()){
            mLoadingDialogBinding.tv.text = msg
        }
        return this
    }
}

基类封装

abstract class BaseVMActivity : AppCompatActivity(){
 private var mLoadingDialog: LoadingDialog? = null

   override fun onDestroy() {
        super.onDestroy()
        mLoadingDialog?.dismissDialog()
        mLoadingDialog = null
    }


 /**
     * 显示加载弹框
     */
    fun showLoadingDialog(msg: String? = null){
        mLoadingDialog = LoadingDialog()
        mLoadingDialog!!.showDialog(supportFragmentManager,msg)
    }

    /**
     * 隐藏加载弹框
     */
    fun dismissLoadingDialog(){
        mLoadingDialog?.dismissDialog()
    }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android自定义加载框效果)