import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
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.rongke.mike.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,0xffffff);
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(12));
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);
//这里是为了演示动画方便,实际开发中进度只会增加
if(progressCount!=currentCount){
if(progressCount<currentCount){
progressCount+=10;
}else{
progressCount--;
}
postInvalidate();
}
}
//绘制背景边框
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.mipmap.wihte);
}
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.mipmap.purple_primary);
}
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);
}
//绘制文字信息
private void drawText(Canvas canvas) {
String scaleText = new DecimalFormat("#%").format(scale);
String saleText = String.format("已抢%s件", progressCount);
float scaleTextWidth = textPaint.measureText(scaleText);
Bitmap textBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas textCanvas = new Canvas(textBitmap);
textPaint.setColor(textColor);
if (scale < 0.8f) {
textCanvas.drawText(saleText, dp2px(10), baseLineY, textPaint);
textCanvas.drawText(scaleText, width - scaleTextWidth - dp2px(10), baseLineY, textPaint);
} else if (scale < 1.0f) {
textCanvas.drawText(nearOverText, width / 2 - nearOverTextWidth / 2, baseLineY, textPaint);
textCanvas.drawText(scaleText, width - scaleTextWidth - dp2px(10), baseLineY, textPaint);
} else {
textCanvas.drawText(overText, width / 2 - overTextWidth / 2, baseLineY, textPaint);
}
textPaint.setXfermode(mPorterDuffXfermode);
textPaint.setColor(Color.WHITE);
textCanvas.drawRoundRect(
new RectF(sideWidth, sideWidth, (width - sideWidth) * scale, height - sideWidth),
radius, radius, textPaint);
canvas.drawBitmap(textBitmap, 0, 0, null);
textPaint.setXfermode(null);
}
private int dp2px(float dpValue) {
float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
private int sp2px(float spValue) {
float scale = getContext().getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * scale + 0.5f);
}
public void setTotalAndCurrentCount(int totalCount, int currentCount) {
this.totalCount = totalCount;
if (currentCount > totalCount) {
currentCount = totalCount;
}
this.currentCount = currentCount;
postInvalidate();
}
}
在values里面创建attr
<declare-styleable name="SaleProgressView">
<attr name="sideColor" format="color" />
<attr name="textColor" format="color" />
<attr name="sideWidth" format="dimension" />
<attr name="overText" format="string" />
<attr name="nearOverText" format="string" />
<attr name="textSize" format="dimension" />
<attr name="isNeedAnim" format="boolean" />
</declare-styleable>
在xml里面调用
<com.rongke.mike.customview.SaleProgressView
android:id="@+id/siscount_sale"
android:layout_width="match_parent"
android:layout_marginLeft="@dimen/dp_10"
android:layout_marginRight="@dimen/dp_10"
android:layout_marginTop="@dimen/dp_10"
android:layout_below="@id/discount_price"
android:layout_toEndOf="@id/discount_imageone"
android:layout_height="@dimen/dp_20">
</com.rongke.mike.customview.SaleProgressView>
赋值
第一个参数是总额 第二个是已经买了多少
如果报错 可以调换一下参数
SaleProgressView saleProgressView = helper.getView(R.id.siscount_sale);
Integer integer = Integer.valueOf(item.getNum());
Integer integer1 = Integer.valueOf(item.getYishou());
saleProgressView.setTotalAndCurrentCount(integer,integer1);