使用PathMeasure簡(jiǎn)單模仿系統(tǒng)ProgressBar,效果如下:
創(chuàng)新互聯(lián)公司是一家從事企業(yè)網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、行業(yè)門戶網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)制作的專業(yè)網(wǎng)絡(luò)公司,擁有經(jīng)驗(yàn)豐富的網(wǎng)站建設(shè)工程師和網(wǎng)頁設(shè)計(jì)人員,具備各種規(guī)模與類型網(wǎng)站建設(shè)的實(shí)力,在網(wǎng)站建設(shè)領(lǐng)域樹立了自己獨(dú)特的設(shè)計(jì)風(fēng)格。自公司成立以來曾獨(dú)立設(shè)計(jì)制作的站點(diǎn)1000多家。
還蠻像的吧,有的人問了,系統(tǒng)自帶的你閑的搞這個(gè)干嘛,當(dāng)然是純粹為了學(xué)習(xí)PathMeasure這個(gè)類。
PathMeasure是用來測(cè)量Path路徑的,可以截取路徑中某一段路徑,通過改變這段路徑的起點(diǎn)、終點(diǎn),達(dá)到類似VectorDrawable中的路徑動(dòng)畫效果:
直接new就可以獲得PathMeasure對(duì)象:
PathMeasure pathMeasure = new PathMeasure();
或者
PathMeasure pathMeasure = new PathMeasure(path, forceClosed);
其中path代表一個(gè)Path對(duì)象,forceClosed代表你測(cè)量的path是否閉合,如果為true,那么測(cè)量長度的時(shí)候周長會(huì)按path.close()來算。
也可以調(diào)用以下方法設(shè)置路徑:
pathMeasure.setPath(path, forceClosed);
獲得路徑的長度:
float length = pathMeasure.getLength();
截取路徑,新截取到的賦值給一個(gè)新Path對(duì)象mDstPath
pathMeasure.getSegment(start, stop, mDstPath, true);
其中start和stop為起止長度,第四個(gè)參數(shù)代表是否startWithMoveTo,是否從moveTo位置開始,一般為true。
要實(shí)現(xiàn)上面的效果,那就用屬性動(dòng)畫寫一個(gè)0到1的百分比,根據(jù)當(dāng)前的百分比和原路徑的長度,動(dòng)態(tài)改變新路徑的起止點(diǎn)長度:
1、寫自定義屬性、構(gòu)造方法、初始化Paint、Path、測(cè)量寬高。注意Path要兩個(gè),一個(gè)裝有原始數(shù)據(jù),一個(gè)去裝新截取的路徑數(shù)據(jù):
mPath = new Path(); mDst = new Path();
2、初始化PathMeasure,并設(shè)置路徑,獲得原始長度:
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { mPath.addCircle(w / 2, h / 2, mRadius, Path.Direction.CW); mPathMeasure = new PathMeasure(); mPathMeasure.setPath(mPath, false); mPathLength = mPathMeasure.getLength(); }
因?yàn)榻omPathMeasure 設(shè)置的路徑必須要裝載數(shù)據(jù),所以此時(shí)mPath需要加上你想畫的東西,畫一個(gè)圓又要有寬高,onDraw中又不能new對(duì)象,所以我把這些操作放到了onSizeChanged中。
3、寫一個(gè)動(dòng)畫,獲取當(dāng)前長度的百分比mPathPercent:
private void startAnim() { ValueAnimator anim = ValueAnimator.ofFloat(0, 1); anim.setInterpolator(new DecelerateInterpolator()); anim.setRepeatCount(ValueAnimator.INFINITE); anim.setDuration(mAnimDuration); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mPathPercent = (float) animation.getAnimatedValue(); invalidate(); } }); anim.start(); //再加一個(gè)旋轉(zhuǎn)動(dòng)畫以及兩倍的時(shí)長,形成旋轉(zhuǎn)視差 ObjectAnimator animRotate = ObjectAnimator.ofFloat(this, View.ROTATION, 0, 360); animRotate.setInterpolator(new LinearInterpolator()); animRotate.setRepeatCount(ValueAnimator.INFINITE); animRotate.setDuration(2 * mAnimDuration); animRotate.start(); }
4、動(dòng)態(tài)改變起止點(diǎn)長度,截取新路徑并繪制:
@Override protected void onDraw(Canvas canvas) { float stop = mPathLength * mPathPercent; float start = (float) (stop - ((0.5 - Math.abs(mPathPercent - 0.5)) * mPathLength * 4)); mDst.reset(); // mDst.lineTo(0, 0); mPathMeasure.getSegment(start, stop, mDst, true); canvas.drawPath(mDst, mPaint); }
注意此時(shí)繪制的路徑是新路徑mDst,而不是裝有原始數(shù)據(jù)的老路徑mPath~
5、順便加幾個(gè)控制的方法:
public void start() { mIsLoading = true; setVisibility(View.VISIBLE); startAnim(); } public void stop() { mIsLoading = false; setVisibility(View.GONE); } public boolean isLoading() { return mIsLoading; }
Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (loadingView.isLoading()) { loadingView.stop(); } else { loadingView.start(); } } });
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前題目:Android自定義View使用PathMeasure簡(jiǎn)單模仿系統(tǒng)ProgressBar(四)
URL地址:http://chinadenli.net/article30/giejpo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、全網(wǎng)營銷推廣、外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)、網(wǎng)站營銷、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)