Android-自訂顯示價格的PriceView

來源:互聯網
上載者:User

標籤:

轉載請標明出處:http://blog.csdn.net/goldenfish1919/article/details/44418883

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


價格分成了3部分,前面是一個¥,中間的整數部分,後面的小數部分,中間還帶一個刪除線。

參考:http://blog.csdn.net/lmj623565791/article/details/44098729,我們這個其實更簡單,自訂一個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>

布局檔案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.example.priceview.PriceView        android:id="@+id/priceview1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        cart:value="¥12.345"        cart:moneySize="14dp"        cart:intSize="20dp"        cart:decimalSize="16dp"        cart:textColor="#ff0000"        cart:strike="true"        android:padding="10dp"/>        <com.example.priceview.PriceView        android:id="@+id/priceview2"        android:layout_width="wrap_content"        android: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.PriceView        android:id="@+id/priceview3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        cart:value="¥12.0"        cart:moneySize="14dp"        cart:intSize="20dp"        cart:decimalSize="16dp"        cart:textColor="#ff0000"        cart:strike="true"        cart:withEndZero="false"/></LinearLayout>

工程源碼下載:http://download.csdn.net/download/goldenfish1919/8512713


Android-自訂顯示價格的PriceView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.