admin
2021-05-08 f93ff67ed4681f416a653370aa1e7995a56940ef
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.tejia.lijin.app.util;
 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
 
import com.tejia.lijin.app.R;
 
import java.text.DecimalFormat;
 
/**
 * 限时秒杀进度条
 */
public class SaleProgressView extends View {
    //商品总数
    private int totalCount;
    //当前卖出数
    private int currentCount;
    //动画需要的
    private int progressCount;
    //售出比例
    private float scale;
    //边框颜色
    private int sideColor;
    //文字颜色
//    private int textColor;
    //边框粗细
    private float sideWidth;
    //边框所在的矩形
    private Paint sidePaint;
    //背景矩形
    private RectF bgRectF;
    private float radius;
    private int width;
    private int height;
    private PorterDuffXfermode mPorterDuffXfermode;//设置混合模式
    private Paint srcPaint;
    private Bitmap fgSrc;
    private Bitmap bgSrc;
 
//    private String nearOverText;//即将售罄
//    private String overText;//已售罄
//    private float textSize;
 
    //    private Paint textPaint;
//    private float nearOverTextWidth;
//    private float overTextWidth;
    private float baseLineY;
    private Bitmap bgBitmap;
    private boolean isNeedAnim;
 
    public SaleProgressView(Context context) {
        this(context, null);
    }
 
    public SaleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
        initPaint();
    }
 
    private void initAttrs(Context context, AttributeSet attrs) {
//        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SaleProgressView);
//        sideColor = ta.getColor(R.styleable.SaleProgressView_sideColor, 0xffff3c32);
//        --textColor = ta.getColor(R.styleable.SaleProgressView_textColor, 0xffff3c32);
//        sideWidth = ta.getDimension(R.styleable.SaleProgressView_sideWidth, dp2px(2));
//        --overText = ta.getString(R.styleable.SaleProgressView_overText);//已售罄
//        --nearOverText = ta.getString(R.styleable.SaleProgressView_nearOverText);//即将售罄
//        --textSize = ta.getDimension(R.styleable.SaleProgressView_textSize, sp2px(16));
//        isNeedAnim = ta.getBoolean(R.styleable.SaleProgressView_isNeedAnim, true);
//        ta.recycle();
    }
 
    private void initPaint() {
        sidePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        sidePaint.setStyle(Paint.Style.STROKE);
        sidePaint.setStrokeWidth(sideWidth);
        sidePaint.setColor(sideColor);
 
        srcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 
//        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        textPaint.setStyle(Paint.Style.FILL);
//        textPaint.setTextSize(textSize);
 
        mPorterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);
//        nearOverTextWidth = textPaint.measureText(nearOverText);
//        overTextWidth = textPaint.measureText(overText);
    }
 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        //获取View的宽高
        width = getMeasuredWidth();
        height = getMeasuredHeight();
 
        //圆角半径
        radius = height / 2.0f;
        //留出一定的间隙,避免边框被切掉一部分
        if (bgRectF == null) {
            bgRectF = new RectF(sideWidth, sideWidth, width - sideWidth, height - sideWidth);
        }
 
//        if (baseLineY == 0.0f) {
//            Paint.FontMetricsInt fm = textPaint.getFontMetricsInt();
//            baseLineY = height / 2 - (fm.descent / 2 + fm.ascent / 2);
//        }
    }
 
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!isNeedAnim) {
            progressCount = currentCount;
        }
 
        if (totalCount == 0) {
            scale = 0.0f;
        } else {
            scale = Float.parseFloat(new DecimalFormat("0.00").format((float) progressCount / (float) totalCount));
        }
 
        drawSide(canvas);
        drawBg(canvas);
        drawFg(canvas);
//        drawText(canvas);
    }
 
    //绘制背景边框
    private void drawSide(Canvas canvas) {
        canvas.drawRoundRect(bgRectF, radius, radius, sidePaint);
    }
 
    //绘制背景
    private void drawBg(Canvas canvas) {
        if (bgBitmap == null) {
            bgBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        }
        Canvas bgCanvas = new Canvas(bgBitmap);
        if (bgSrc == null) {
            bgSrc = BitmapFactory.decodeResource(getResources(), R.drawable.ic_spike);
        }
        bgCanvas.drawRoundRect(bgRectF, radius, radius, srcPaint);
 
        srcPaint.setXfermode(mPorterDuffXfermode);
        bgCanvas.drawBitmap(bgSrc, null, bgRectF, srcPaint);
 
        canvas.drawBitmap(bgBitmap, 0, 0, null);
        srcPaint.setXfermode(null);
    }
 
    //绘制进度条
    private void drawFg(Canvas canvas) {
        if (scale == 0.0f) {
            return;
        }
        Bitmap fgBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas fgCanvas = new Canvas(fgBitmap);
        if (fgSrc == null) {
            fgSrc = BitmapFactory.decodeResource(getResources(), R.drawable.ic_spike);
        }
        fgCanvas.drawRoundRect(
                new RectF(sideWidth, sideWidth, (width - sideWidth) * scale, height - sideWidth),
                radius, radius, srcPaint);
 
        srcPaint.setXfermode(mPorterDuffXfermode);
        fgCanvas.drawBitmap(fgSrc, null, bgRectF, srcPaint);
 
        canvas.drawBitmap(fgBitmap, 0, 0, null);
        srcPaint.setXfermode(null);
    }
 
    public void setTotalAndCurrentCount(int totalCount, int currentCount) {
        this.totalCount = totalCount;
        if (currentCount > totalCount) {
            currentCount = totalCount;
        }
        this.currentCount = currentCount;
        postInvalidate();//重新绘制
    }
}