wxr
2022-11-23 1e7b3abd15d37f6c6bc97ac14922457b9604c275
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
package com.mm.android.deviceaddmodule.views;
 
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;
 
@SuppressLint("AppCompatCustomView")
public class DrawableCenterTextView extends TextView {
    public DrawableCenterTextView(Context context) {
        super(context);
    }
 
    public DrawableCenterTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
 
    public DrawableCenterTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        Drawable[] drawables = getCompoundDrawables();
        if (drawables == null) {
            super.onDraw(canvas);
        }
        for (int i = 0; i < drawables.length; i++) {
            Drawable drawable = drawables[i];
            if (drawable != null) {
                if (i == 0 || i == 2) {
                    int drawableWidth = drawable.getIntrinsicWidth();
                    int drawablePadding = getCompoundDrawablePadding();
                    int textWidth = (int) getPaint().measureText(getText().toString().trim());
                    int bodyWidth = drawableWidth + drawablePadding + textWidth;
                    canvas.save();
                    canvas.translate((getWidth() - bodyWidth) / 2, 0);
                } else if (i == 1 || i == 3) {
                    int drawableHeight = drawable.getIntrinsicHeight();
                    int drawablePadding = getCompoundDrawablePadding();
                    int textHeight = (int) getPaint().measureText(getText().toString().trim());
                    int bodyHeight = textHeight + drawablePadding + drawableHeight;
                    canvas.save();
                    canvas.translate(0, (getHeight() - bodyHeight) / 2);
                }
            }
        }
        super.onDraw(canvas);
    }
}