PageLoadingView.java 3.36 KB
package com.wd.common.progress;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.airbnb.lottie.LottieAnimationView;
import com.wd.kittools.density.DensityUtils;
import com.wd.foundation.wdkit.constant.Constants;
import com.wd.foundation.wdkit.utils.AnimUtil;
import com.wd.fastcoding.base.R;
import com.wd.foundation.wdkit.utils.UiUtils;

/**
 * 第一次 视频详情 请求接口 loading 效果
 * @author xujiawei
 */
public class PageLoadingView extends LinearLayout {


    /**
     * 上下文
     */
    private final Context context;

    /**
     * 延迟0.5秒展示loading,如果0.5秒内结束了,则不展示loading
     * */
    private boolean needShow;
    private Handler handler;

    /**
     * 动画父布局
     */
    private LinearLayout llAnimationView;

    /**
     * 动画布局
     */
    private LottieAnimationView animationView;



    /**
     * 动画文件
     */
    private String interactCode;

    public PageLoadingView(Context context) {
        this(context, null);
    }

    public PageLoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PageLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    private void init() {
        View view = LayoutInflater.from(context).inflate(R.layout.small_white_black_loading_view, this, true);
        llAnimationView = view.findViewById(R.id.llAnimationView);
        animationView = view.findViewById(R.id.animation_view);
        handler = new Handler(Looper.getMainLooper());
    }

    /**
     * 设置动画文件
     * @param interactCode 动画文件
     */
    public void setInteractCode(String interactCode) {
        this.interactCode = interactCode;
        // 如果时人民日报扫光动画,宽高设置为167*60
        if ("video_loading.json".equalsIgnoreCase(interactCode)) {
            // 动画父布局
            UiUtils.setWH(llAnimationView, DensityUtils.INSTANCE.dip2px(context, 167), DensityUtils.INSTANCE.dip2px(context, 100));
            // 动画布局
            UiUtils.setWH(animationView, DensityUtils.INSTANCE.dip2px(context, 167), DensityUtils.INSTANCE.dip2px(context, 60));
        }
    }

    /**
     * loading
     */
    public void showLoading() {

        //需要展示loading
        needShow = true;
        //延时展示loading
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(needShow){
                    needShow = false;
                    if (TextUtils.isEmpty(interactCode)) {
                        AnimUtil.showLocalLottieEffects(animationView,"refreshing_common_loading.json", true);
                    }else {
                        AnimUtil.showLocalLottieEffects(animationView, interactCode, true);
                    }
                }
            }
        }, Constants.MIN_DELAY_TIME_SHOWLOADING);
    }

    public void stopLoading() {

        // 已经结束,不需要再展示loading
        needShow = false;
        animationView.pauseAnimation();
    }

}