kimi
2021-02-18 749a5510a9f014446a3cd6ba57b3cb0cc8148dc1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.mylhyl.circledialog.view;
 
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
 
import androidx.annotation.Nullable;
 
import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieDrawable;
import com.mylhyl.circledialog.internal.BackgroundHelper;
import com.mylhyl.circledialog.internal.CircleParams;
import com.mylhyl.circledialog.internal.Controller;
import com.mylhyl.circledialog.params.DialogParams;
import com.mylhyl.circledialog.params.LottieParams;
import com.mylhyl.circledialog.view.listener.OnCreateLottieListener;
 
/**
 * Created by hupei on 2018/7/7.
 */
 
final class BodyLottieView extends LinearLayout {
    private LottieAnimationView mLottieAnimationView;
    private TextView mTextView;
    private DialogParams mDialogParams;
    private LottieParams mLottieParams;
    private OnCreateLottieListener mOnCreateLottieListener;
 
    public BodyLottieView(Context context, CircleParams circleParams) {
        super(context);
        init(circleParams);
    }
 
    private void init(CircleParams circleParams) {
 
        this.mDialogParams = circleParams.dialogParams;
        this.mLottieParams = circleParams.lottieParams;
        this.mOnCreateLottieListener = circleParams.circleListeners.createLottieListener;
 
        setOrientation(LinearLayout.VERTICAL);
 
        // 如果没有背景色,则使用默认色
        int backgroundColor = mLottieParams.backgroundColor != 0 ?
                mLottieParams.backgroundColor : mDialogParams.backgroundColor;
        BackgroundHelper.handleBodyBackground(this, backgroundColor, circleParams);
 
        createLottieView();
        createText();
 
        if (mOnCreateLottieListener != null) {
            mOnCreateLottieListener.onCreateLottieView(mLottieAnimationView, mTextView);
        }
    }
 
    private void createLottieView() {
        mLottieAnimationView = new LottieAnimationView(getContext());
        int lottieWidth = Controller.dp2px(getContext(), mLottieParams.lottieWidth);
        int lottieHeight = Controller.dp2px(getContext(), mLottieParams.lottieHeight);
        LayoutParams layoutParams = new LayoutParams(lottieWidth <= 0 ? LayoutParams.WRAP_CONTENT : lottieWidth,
                lottieHeight <= 0 ? LayoutParams.WRAP_CONTENT : lottieHeight);
        int[] margins = mLottieParams.margins;
        if (margins != null)
            layoutParams.setMargins(Controller.dp2px(getContext(), margins[0]),
                    Controller.dp2px(getContext(), margins[1]),
                    Controller.dp2px(getContext(), margins[2]),
                    Controller.dp2px(getContext(), margins[3]));
        layoutParams.gravity = Gravity.CENTER;
 
        if (mLottieParams.animationResId != 0) {
            mLottieAnimationView.setAnimation(mLottieParams.animationResId);
        }
        if (!TextUtils.isEmpty(mLottieParams.animationFileName)) {
            mLottieAnimationView.setAnimation(mLottieParams.animationFileName);
        }
        if (!TextUtils.isEmpty(mLottieParams.imageAssetsFolder)) {
            mLottieAnimationView.setImageAssetsFolder(mLottieParams.imageAssetsFolder);
        }
        if (mLottieParams.autoPlay) {
            mLottieAnimationView.playAnimation();
        }
        if (mLottieParams.loop) {
            mLottieAnimationView.setRepeatCount(LottieDrawable.INFINITE);
        }
        addView(mLottieAnimationView, layoutParams);
    }
 
    @Nullable
    private void createText() {
        //构建文本
        if (!TextUtils.isEmpty(mLottieParams.text)) {
            mTextView = new TextView(getContext());
            LayoutParams textLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            textLayoutParams.gravity = Gravity.CENTER;
            int[] textMargins = mLottieParams.textMargins;
            if (textMargins != null) {
                textLayoutParams.setMargins(Controller.dp2px(getContext(), textMargins[0]),
                        Controller.dp2px(getContext(), textMargins[1]),
                        Controller.dp2px(getContext(), textMargins[2]),
                        Controller.dp2px(getContext(), textMargins[3]));
            }
            if (mDialogParams.typeface != null) {
                mTextView.setTypeface(mDialogParams.typeface);
            }
            mTextView.setText(mLottieParams.text);
            mTextView.setTextSize(mLottieParams.textSize);
            mTextView.setTextColor(mLottieParams.textColor);
            mTextView.setTypeface(mTextView.getTypeface(), mLottieParams.styleText);
            int[] textPadding = mLottieParams.textPadding;
            if (textPadding != null) {
                mTextView.setPadding(Controller.dp2px(getContext(), textPadding[0]),
                        Controller.dp2px(getContext(), textPadding[1]),
                        Controller.dp2px(getContext(), textPadding[2]),
                        Controller.dp2px(getContext(), textPadding[3]));
            }
            addView(mTextView, textLayoutParams);
        }
    }
 
    public void refreshText() {
        if (mLottieParams == null) {
            return;
        }
        if (mLottieAnimationView != null) {
            if (mLottieParams.animationResId != 0) {
                mLottieAnimationView.setAnimation(mLottieParams.animationResId);
            }
            if (!TextUtils.isEmpty(mLottieParams.animationFileName)) {
                mLottieAnimationView.setAnimation(mLottieParams.animationFileName);
            }
            if (!TextUtils.isEmpty(mLottieParams.imageAssetsFolder)) {
                mLottieAnimationView.setImageAssetsFolder(mLottieParams.imageAssetsFolder);
            }
            mLottieAnimationView.playAnimation();
        }
 
        if (mTextView != null && !TextUtils.isEmpty(mLottieParams.text)) {
            mTextView.setText(mLottieParams.text);
        }
    }
}