This article shares the specific code for Android to realize automatic circle effect display for your reference. The specific content is as follows
Create in the values folder
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyPb"> <attr name="circle_color" format="color" /> <attr name="circle_radius" format="dimension" /><!-- size --> <attr name="circle_x" format="dimension" /> <attr name="circle_y" format="dimension" /> </declare-styleable> </resources>
Write a class inheritance view
package widget; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Created by Administrator on 2017/12/7. */ public class MyPb extends View { private float radius, cx, cy; private Paint paint; private float sweepAngle;// Rotation angle public MyPb(Context context) { super(context, null); } public MyPb(Context context, @Nullable AttributeSet attrs) { super(context, attrs); // Get custom properties TypedArray a = (attrs, ); // Get color int color = (.MyPb_circle_color, );// Can't get the default value radius = (.MyPb_circle_radius, 20); cx = (.MyPb_circle_x, 100); cy = (.MyPb_circle_y, 100); // Need to be recycled (); paint = new Paint(); (true);// Anti-aliasing (color); ();// Hollow Timer timer = new Timer(); (new TimerTask() { @Override public void run() { if (sweepAngle > 360) { return; } sweepAngle += 1; postInvalidate(); } }, 1000, 20);// Execute every 20 milliseconds } @Override protected void onDraw(Canvas canvas) { (); (10); (cx, cy, radius, paint);// Draw a circle (20);// Thickness // Draw the track of movement (); // The upper, lower, left and right overlap with the circle, the left side is the horizontal coordinate minus the radius at the center of the circle, the upper side is the vertical coordinate minus the radius, and so on RectF rectF = new RectF(cx - radius, cy - radius, cx + radius, cy + radius); // Start angle, rotation angle, the third attribute is whether to fill, brush (rectF, -90, sweepAngle, false, paint); // Draw text int progress = (int) (sweepAngle / 360f * 100); (50); (0); (); (progress + "%", cx - 20, cy, paint); } }
Introduce a custom view class in the main page layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro xmlns:app="/apk/res-auto" xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=""> < 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>
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.