Android自定義View仿QQ等級天數(shù)進度
最近一直都在看自定義View這一塊。差不多一個星期了吧。這個星期堅持每天更新博客,感覺自己的技術(shù)也有點突破,對自定義View的計算也有了更深的認識。
今天看到手機一個成長天數(shù)進度的控件,覺得挺有意思的,于是想自己也寫一個。效果如下:
由圖可以知道,這里面有很多個元素,首先是背景的矩形區(qū)域,其次就是兩個環(huán)形,然后三個Text文本。其實不復雜,我們一點一點的去實現(xiàn)。
首先呢,畫矩形背景。這里用到一個RectF的類,這個類包含一個矩形的四個單精度浮點坐標。矩形通過上下左右4個邊的坐標來表示一個矩形。這些坐標值屬性可以被直接訪問,用width()和 height()方法可以獲取矩形的寬和高,同時他還有構(gòu)造方法:
RectF一共有四個構(gòu)造方法:
RectF()構(gòu)造一個無參的矩形
RectF(float left,float top,float right,float bottom)構(gòu)造一個指定了4個參數(shù)的矩形
RectF(Rect F r)根據(jù)指定的RectF對象來構(gòu)造一個RectF對象(對象的左邊坐標不變)
RectF(Rect r)根據(jù)給定的Rect對象來構(gòu)造一個RectF對象
那么,這里使用第二個構(gòu)造方法,代碼如下:
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth=getWidth(); mHeight=getHeight(); mRectF=new RectF((float)(mWidth*0.1), (float)(mHeight*0.1), (float)(mWidth*0.9), (float)(mHeight*0.9)); }
現(xiàn)在是矩形的背景有了,那么,還有環(huán)形跟文字又怎么去處理呢,別著急,我們先看看這個環(huán)形,我這里之所以定義兩個環(huán)形,是因為,一個用作進度去顯示,一個當做背景去實現(xiàn),好了,分別繪制兩個環(huán)形。代碼如下:
canvas.drawArc(mRectF, 90, 360, false, mButtomPaint); canvas.drawArc(mRectF, 15, 180, false, mTopPaint);
兩個環(huán)形也有了,接下來就是文字了,繪制文字我們使用canvas.DrawText方法,去繪制,具體代碼如下:
canvas.drawText("5.0", (mWidth-(mTextWidth+mTextSmail))/2, (float)(mHeight/2), mTextPaint);
canvas.drawText("天", (mWidth-(mTextWidth+mTextSmail))/2+mTextWidth, (float)(mHeight/2), mSmailTextPaint);
canvas.drawText("升級年費超級會員立即升至", (mWidth-mTextSmailButtom)/2, (float)(mHeight/2+30), mSmailTextPaint);
這下全部的效果也出來了,最后,我貼上所有的代碼:
public class MyProgress extends View { private Paint mButtomPaint; private Paint mTopPaint; private Paint mTextPaint; private Paint mSmailTextPaint; private float mWidth; private float mHeight; private RectF mRectF; public MyProgress(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } public MyProgress(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public MyProgress(Context context) { super(context); initView(); } private void initView() { mButtomPaint=new Paint(); mButtomPaint.setColor(Color.rgb(69, 142, 253)); mButtomPaint.setAntiAlias(true); mButtomPaint.setStrokeWidth(10); mButtomPaint.setStyle(Style.STROKE); mTopPaint=new Paint(); mTopPaint.setColor(Color.parseColor("#ffffff")); mTopPaint.setAntiAlias(true); mTopPaint.setStrokeWidth(10); mTopPaint.setStyle(Style.STROKE); mTextPaint=new Paint(); mTextPaint.setColor(Color.WHITE); mTextPaint.setAntiAlias(true); mTextPaint.setStrokeWidth(5); mTextPaint.setTextSize(50); mSmailTextPaint=new Paint(); mSmailTextPaint.setStrokeWidth(3); mSmailTextPaint.setColor(Color.WHITE); mSmailTextPaint.setAntiAlias(true); mSmailTextPaint.setTextSize(15); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth=getWidth(); mHeight=getHeight(); mRectF=new RectF((float)(mWidth*0.1), (float)(mHeight*0.1), (float)(mWidth*0.9), (float)(mHeight*0.9)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawArc(mRectF, 90, 360, false, mButtomPaint); canvas.drawArc(mRectF, 15, 180, false, mTopPaint); float mTextWidth=mTextPaint.measureText("5.0"); float mTextSmail=mSmailTextPaint.measureText("天"); float mTextSmailButtom=mSmailTextPaint.measureText("升級年費超級會員立即升至"); canvas.drawText("5.0", (mWidth-(mTextWidth+mTextSmail))/2, (float)(mHeight/2), mTextPaint); canvas.drawText("天", (mWidth-(mTextWidth+mTextSmail))/2+mTextWidth, (float)(mHeight/2), mSmailTextPaint); canvas.drawText("升級年費超級會員立即升至", (mWidth-mTextSmailButtom)/2, (float)(mHeight/2+30), mSmailTextPaint); } }
謝謝閱讀。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter 控制屏幕旋轉(zhuǎn)的實現(xiàn)
這篇文章主要介紹了Flutter 控制屏幕旋轉(zhuǎn)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09解決Bitmap通過getWidth和getHeight獲取尺寸不符的問題
這篇文章主要介紹了解決Bitmap通過getWidth和getHeight獲取尺寸不符的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Android Studio之Debug運行期代碼植入的方法
這篇文章主要介紹了Android Studio之Debug運行期代碼植入的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07Android build.gradle版本名打包配置的方法
這篇文章主要介紹了Android build.gradle版本名打包配置的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02