自定义显示价格的PriceView

转载请标明出处:

先看一下我们要做的效果:

价格分成了3部分,前面是一个¥,中间的整数部分,后面的小数部分,中间还带一个删除线。

参考:,我们这个其实更简单,,自定义一个view,然后把三个部分和删除线分别画出来就可以了。

PriceView.java:

public class PriceView extends View{private String value = null;private int moneySize = -1;private int intSize = -1;private int decimalSize = -1;private String money = "¥";private String decimalPart = "";private String intPart = "";private int moneyStart = 0;private int intStart = 0;private int decimalStart = 0;private int textColor = 0;private boolean strike = false;private boolean withEndZero = true;private Paint mPaint;private Rect mTextBound = new Rect();private int totalWidth = 0;private int maxHeight = 0;public PriceView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}public PriceView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public PriceView(Context context) {this(context, null);}private void init(Context context, AttributeSet attrs){mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);getProperties(context, attrs);calcTextDimens();}private void calcTextDimens() {totalWidth = 0;maxHeight = 0;//把text分成三个部分if(value == null || value.length() <= 0){return;}String arr[] = value.split("\\.");intPart = arr[0];if(intPart.length() > 0 && intPart.charAt(0) == '¥'){intPart = intPart.substring(1);}decimalPart = arr.length > 1? arr[1]:"";if(decimalPart != null){if(!withEndZero){decimalPart = decimalPart.replaceAll("0{1,}$", "");}if(decimalPart != null && decimalPart.length() > 0){decimalPart = "."+decimalPart;}}//处理¥int moneyWidth = process(money, moneySize);moneyStart = getPaddingLeft();//处理整数部分int intWidth = process(intPart, intSize);intStart = moneyStart + moneyWidth;//处理小数部分process(decimalPart, decimalSize);decimalStart = intStart + intWidth;totalWidth += getPaddingLeft() + getPaddingRight();maxHeight += getPaddingTop() + getPaddingBottom();}private int process(String text, int textSize){if(text == null || text.length() <= 0){return 0;}mPaint.setTextSize(textSize);int textWidth = (int) mPaint.measureText(text);mPaint.getTextBounds(text, 0, text.length(), mTextBound);totalWidth += textWidth;maxHeight = mTextBound.height() > maxHeight? mTextBound.height() : maxHeight;return textWidth;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){int width = measureWidth(widthMeasureSpec);int height = measureHeight(heightMeasureSpec);setMeasuredDimension(width, height);}protected void onDraw(Canvas canvas){super.onDraw(canvas);mPaint.setColor(textColor);//画中间的删除线if(strike){//mPaint.setFlags(Paint.STRIKE_THRU_TEXT_FLAG);是不可以的,为什么不可以可以自己试一下float startX = getPaddingLeft();float startY = (getMeasuredHeight() – getPaddingBottom() – getPaddingTop()) /2 + getPaddingTop();float stopX = getMeasuredWidth() – getPaddingRight();float stopY = startY;canvas.drawLine(startX, startY , stopX, stopY, mPaint);}//画¥mPaint.setTextSize(moneySize);canvas.drawText(money, moneyStart, getMeasuredHeight() – getPaddingBottom(), mPaint);//画整数部分mPaint.setTextSize(intSize);canvas.drawText(intPart, intStart, getMeasuredHeight() – getPaddingBottom(), mPaint);//画小数部分mPaint.setTextSize(decimalSize);canvas.drawText(decimalPart, decimalStart, getMeasuredHeight() – getPaddingBottom(), mPaint);}private int measureWidth(int measureSpec){int mode = MeasureSpec.getMode(measureSpec);int val = MeasureSpec.getSize(measureSpec);int result = 0;switch (mode){case MeasureSpec.EXACTLY:result = val;break;case MeasureSpec.AT_MOST:case MeasureSpec.UNSPECIFIED:result = totalWidth;break;}return result;}private int measureHeight(int measureSpec){int mode = MeasureSpec.getMode(measureSpec);int val = MeasureSpec.getSize(measureSpec);int result = 0;switch (mode){case MeasureSpec.EXACTLY:result = val;break;case MeasureSpec.AT_MOST:case MeasureSpec.UNSPECIFIED:result = maxHeight;break;}return result;}private void getProperties(Context context, AttributeSet attrs){//自定义的属性TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CartPriceValue);int textSize = a.getDimensionPixelSize(R.styleable.CartPriceValue_textSize, 14);String value = a.getString(R.styleable.CartPriceValue_value);int textColor = a.getColor(R.styleable.CartPriceValue_textColor, 0xffffff);int moneySize = a.getDimensionPixelSize(R.styleable.CartPriceValue_moneySize, textSize);int intSize = a.getDimensionPixelSize(R.styleable.CartPriceValue_intSize, textSize);int decimalSize = a.getDimensionPixelSize(R.styleable.CartPriceValue_decimalSize, textSize);boolean strike = a.getBoolean(R.styleable.CartPriceValue_strike, false);boolean withEndZero = a.getBoolean(R.styleable.CartPriceValue_withEndZero, true);this.value = value;this.textColor = textColor;this.moneySize = moneySize;this.intSize = intSize;this.decimalSize = decimalSize;this.strike = strike;this.withEndZero = withEndZero;a.recycle();}public void setText(String text){this.value = text;calcTextDimens();}}attr.xml:<?xml version="1.0" encoding="utf-8"?><resources><declare-styleable name="CartPriceValue"><attr name="value" format="reference|string" /><attr name="textColor" format="reference|color" /><attr name="textSize" format="reference|dimension" /><attr name="moneySize" format="reference|dimension" /><attr name="intSize" format="reference|dimension" /><attr name="decimalSize" format="reference|dimension" /><attr name="strike" format="boolean" /><attr name="withEndZero" format="boolean" /></declare-styleable></resources>你不怕困难,困难就怕你。

自定义显示价格的PriceView

相关文章:

你感兴趣的文章:

标签云: