2018-05-14 10:58:45 1493瀏覽
本文實例為大家分享了Android實現(xiàn)自動轉(zhuǎn)圈效果展示的具體代碼,供大家參考,具體內(nèi)容如下:
<?xmlversion="1.0"encoding="utf-8"?> <resources> <declare-styleablename="MyPb"> <attrname="circle_color"format="color"/> <attrname="circle_radius"format="dimension"/><!--尺寸--> <attrname="circle_x"format="dimension"/> <attrname="circle_y"format="dimension"/> </declare-styleable> </resources>
packagewidget; importandroid.content.Context; importandroid.content.res.TypedArray; importandroid.graphics.Canvas; importandroid.graphics.Color; importandroid.graphics.Paint; importandroid.graphics.RectF; importandroid.support.annotation.Nullable; importandroid.util.AttributeSet; importandroid.view.View; importcom.bwie.zdycircle.R; importjava.util.Timer; importjava.util.TimerTask; /** *CreatedbyAdministratoron2017/12/7. */ publicclassMyPbextendsView{ privatefloatradius,cx,cy; privatePaintpaint; privatefloatsweepAngle;//旋轉(zhuǎn)角度 publicMyPb(Contextcontext){ super(context,null); } publicMyPb(Contextcontext,@NullableAttributeSetattrs){ super(context,attrs); //獲取自定義的屬性 TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.MyPb); //獲取顏色 intcolor=a.getColor(R.styleable.MyPb_circle_color,Color.BLACK);//獲取不到給默認(rèn)值 radius=a.getDimension(R.styleable.MyPb_circle_radius,20); cx=a.getDimension(R.styleable.MyPb_circle_x,100); cy=a.getDimension(R.styleable.MyPb_circle_y,100); //需要回收 a.recycle(); paint=newPaint(); paint.setAntiAlias(true);//抗鋸齒 paint.setColor(color); paint.setStyle(Paint.Style.STROKE);//空心 Timertimer=newTimer(); timer.schedule(newTimerTask(){ @Override publicvoidrun(){ if(sweepAngle>360){ return; } sweepAngle+=1; postInvalidate(); } },1000,20);//每隔20毫秒執(zhí)行一次 } @Override protectedvoidonDraw(Canvascanvas){ paint.setColor(Color.BLUE); paint.setStrokeWidth(10); canvas.drawCircle(cx,cy,radius,paint);//畫圓 paint.setStrokeWidth(20);//粗細(xì) //畫運動的軌跡 paint.setColor(Color.RED); //上下左右與圓重合,左邊為圓心的橫坐標(biāo)減去半徑,上邊為縱坐標(biāo)減去半徑,以此類推 RectFrectF=newRectF(cx-radius,cy-radius,cx+radius,cy+radius); //起始角度,旋轉(zhuǎn)角度,第三個屬性為是否填充,畫筆 canvas.drawArc(rectF,-90,sweepAngle,false,paint); //繪制文字 intprogress=(int)(sweepAngle/360f*100); paint.setTextSize(50); paint.setStrokeWidth(0); paint.setColor(Color.BLACK); canvas.drawText(progress+"%",cx-20,cy,paint); } }
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.bwie.zdycircle.MainActivity"> <widget.MyPb android:layout_width="wrap_content" android:layout_height="wrap_content" app:circle_color="#0000ff" app:circle_radius="70dp" app:circle_x="200dp" app:circle_y="200dp"/> </LinearLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持扣丁學(xué)堂。最后想要了解更多請關(guān)注扣丁學(xué)堂Android培訓(xùn)官網(wǎng)、微信公眾號平臺,扣丁學(xué)堂Android視頻教程從零基礎(chǔ)到精通免費試聽試學(xué),就業(yè)終端服務(wù)系統(tǒng)隨時監(jiān)控學(xué)員的學(xué)習(xí)情況和效果,順利畢業(yè)后根據(jù)學(xué)員自身風(fēng)格和學(xué)習(xí)效果推薦就業(yè)。如果你想要學(xué)習(xí)Android開發(fā)工程師技術(shù)就不要再猶豫了,喜歡就付諸行動吧
【關(guān)注微信公眾號獲取更多學(xué)習(xí)資料】
查看更多關(guān)于“Android開發(fā)技術(shù)”的相關(guān)資訊>>