Reprint please indicate source: http://blog.csdn.net/goldenfish1919/article/details/44418883
Let's look at the effect we're going to make:
The price is divided into 3 parts, preceded by a ¥, an integral part of the middle, a fractional portion behind it, and a strikethrough in the middle.
Reference: http://blog.csdn.net/lmj623565791/article/details/44098729, we are actually more simple, customize a view, and then put three parts and delete lines are drawn separately.
Priceview.java:
public class Priceview extends view{private String value = null;private int moneysize = -1;private int intSize = -1;privat e int decimalsize = -1;private String money = "¥";p rivate string decimalpart = "";p rivate string intpart = "";p rivate int Moneystart = 0;private int intstart = 0;private int decimalstart = 0;private int textcolor = 0;private Boolean strike = FA Lse;private Boolean Withendzero = true;private Paint mpaint;private rect mtextbound = new Rect ();p rivate 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;//divides text into three partsDivide 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; }}//handles ¥int moneywidth = Process (money, moneysize); Moneystart = Getpaddingleft ();//processing integer part int intwidth = Process (in Tpart, intSize); Intstart = Moneystart + moneywidth;//Process Decimal part process (Decimalpart, decimalsize);d Ecimalstart = 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);//Draw Middle strikethrough if (strike) {// Mpaint.setflags (Paint.strike_thru_text_flag); No, why can't you try float StartX = getpaddingleft (); Float Starty = ( Getmeasuredheight ()-Getpaddingbottom ()-getpaddingtop ())/2 + getpaddingtop (); Float stopx = Getmeasuredwidth ()-GetPad Dingright (); Float stopy = Starty;canvas.drawline (StartX, Starty, Stopx, Stopy, mpaint);} Draw ¥mpaint.settextsize (moneysize); Canvas.drawtext (Money, Moneystart, Getmeasuredheight ()-Getpaddingbottom (), Mpaint);//Draw Integer part mpaint.settextsize (intSize); CanvAs.drawtext (Intpart, Intstart, Getmeasuredheight ()-Getpaddingbottom (), mpaint);//drawing of the decimal part 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) {//custom properties Typedarray A = Context.obtainstyledattribu TES (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>
Layout file Main.xml:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:cart=" Http://schemas.android.com/apk/res-auto "android:layout_width=" Match_parent "Android:layout_ height= "Match_parent" android:background= "@android: Color/white" android:orientation= "vertical" > <COM.E Xample.priceview.PriceView android:id= "@+id/priceview1" android:layout_width= "Wrap_content" android:l ayout_height= "Wrap_content" cart:value= "¥12.345" cart:moneysize= "14DP" cart:intsize= "20DP" car T:decimalsize= "16DP" cart:textcolor= "#ff0000" cart:strike= "true" android:padding= "10DP"/> & Lt;com.example.priceview.priceview android:id= "@+id/priceview2" android:layout_width= "Wrap_content" a ndroid:layout_height= "Wrap_content" cart:value= "¥12.0" cart:moneysize= "14DP" cart:intsize= "20DP" Cart:decimalsize= "16DP" CArt:textcolor= "#ff0000" cart:strike= "true" android:padding= "10DP"/> <com.example.priceview.pric Eview android:id= "@+id/priceview3" android:layout_width= "wrap_content" android:layout_height= "Wrap_co Ntent "cart:value=" ¥12.0 "cart:moneysize=" 14DP "cart:intsize=" 20DP "cart:decimalsize=" 16DP " Cart:textcolor= "#ff0000" cart:strike= "true" cart:withendzero= "false"/></linearlayout>
Project Source Download: http://download.csdn.net/download/goldenfish1919/8512713
android-Custom Display Price Priceview