Android-PriceView for custom display prices

Source: Internet
Author: User

Android-PriceView for custom display prices

 

Let's take a look at the effect:

The price is divided into three parts. The front part is a ¥, an integer in the middle, a decimal part, and a strikethrough line in the middle.

This is actually simpler. You can customize a view and draw the three parts and strikethrough them.

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 withenderson zero = true; pr Ivate 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 (Contex T context, AttributeSet attrs) {mPaint = new Paint (Paint. ANTI_ALIAS_FLAG); getProperties (context, attrs); calcTextDimens ();} private void calcTextDimens () {totalWidth = 0; maxHeight = 0; // divide text into three parts: 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 (! Withenderson zero) {decimalPart = decimalPart. replaceAll (0 {1, }$,) ;}if (decimalPart! = Null & decimalPart. length ()> 0) {decimalPart =. + decimalPart; }}// process ¥ int moneyWidth = process (money, moneySize); moneyStart = getPaddingLeft (); // process the integer part int intWidth = process (intPart, intSize ); intStart = moneyStart + moneyWidth; // process the fractional part of process (decimalPart, decimalSize); decimalStart = intStart + intWidth; totalWidth + = getPaddingLeft () + getPaddingRight (); maxHeight + = getPaddingTop () + getPaddingB Ottom ();} 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 rows) {int width = measureWidth (widthMeasureSpec); int height = measureHeight (heightMeasureSpec ); setMeasuredDimension (width, height);} protected void onDraw (Canvas canvas) {super. onDraw (canvas); mPaint. setColor (textColor); // strikethrough line in the middle of the painting if (strike) {// mPaint. setFlags (Paint. STRIKE_THRU_TEXT_FLAG); no. Why not try float startX = getPaddingLeft (); float startY = (getMeasuredHeight ()-getPaddingBottom ()-getPaddingTop ()) /2 + getPaddingTop (); float stopX = getMeasuredWidth ()-getPaddingRight (); float stopY = startY; canvas. drawLine (startX, startY, stopX, stopY, mPaint);} // draw $ mPaint. setTextSize (moneySize); canvas. drawText (money, moneyStart, getMeasuredHeight ()-getPaddingBottom (), mPaint); // draw the integer part mPaint. setTextSize (intSize); canvas. drawText (intPart, intStart, getMeasuredHeight ()-getPaddingBottom (), mPaint); // draw the mPaint in the decimal part. 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) {// custom attribute TypedArray a = context. obtainStyledAttributes (attrs, R. styleable. cartPriceValue); int textSize =. getDimensionPixelSize (R. styleable. cartPriceValue_textSize, 14); String value =. getString (R. styleable. cartPriceValue_value); int textColor =. getColor (R. styleable. cartPriceValue_textColor, 0 xffffff); int moneySize =. getDimensionPixelSize (R. styleable. cartPriceValue_moneySize, textSize); int intSize =. getDimensionPixelSize (R. styleable. cartPriceValue_intSize, textSize); int decimalSize =. getDimensionPixelSize (R. styleable. cartPriceValue_decimalSize, textSize); boolean strike =. getBoolean (R. styleable. cartPriceValue_strike, false); boolean withenderson zero =. getBoolean (R. styleable. cartpricevalue_withenderson zero, true); this. value = value; this. textColor = textColor; this. moneySize = moneySize; this. intSize = intSize; this. decimalSize = decimalSize; this. strike = strike; this. withenderson zero = withenderson zero;. recycle ();} public void setText (String text) {this. value = text; calcTextDimens ();}}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.