1. Overview
Strictly speaking, I am an Android novice, and the purpose of writing is just to use it as a knowledge reserve... But thinking that others may accidentally search for this article by me. If I just a simple description, others may not understand it, and maybe they may be criticized. Wouldn't that be very unfair?
Therefore, I will describe the problem and process clearly, which is also an explanation to myself. At the same time, this is also my first article, and I should do it well;
Let me talk about the requirements first: I need to do a countdown in my work recently, which is a countdown of the animation that is slowly eaten. Since I don’t have enough knowledge, I only know that I have to use Canvas to draw. After searching online, I found that either I drew a fan shape statically or could not control the position size of the control... In short, after searching for a circle, I felt that I learned a lot of Canvas knowledge, but since I am also an Android novices, I cannot summarize the anime countdown of the animation I want (I will say here that this is the first time I use this thing and am not familiar with some edits, so I won’t put the renderings on it, but the code here is very simple. Friends who need it can directly take it to run it to see if it is the effect you need);
Finally, I had to ask a friend of mine. Now he is not a great master, but he is much better than me... To put it bluntly, I just moved his logic more over... I am so ashamed... because I just want to be a knowledge reserve...
2. Text
I just mentioned using Canvas, so we first customize a control and directly inherit the custom control of the View;
:
public class SweepView extends View { private static final int DEFAULT_WIDTH = 100; private static final int DEFAULT_HEIGHT = 100; private int mWidth; private int mHeight; private RectF rectF; private Paint paint; private int mColor = ;//The default color is red private float mSweep = 0; //Span angle public SweepView(Context context) { super(context); init(); } public SweepView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public SweepView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { paint = new Paint(); (mColor); //Brush color (); //filling (true); //Is it anti-aliasing } /** * Set the fan color * UIThred */ public void setColor(int color) { = color; (mColor); //Call onDraw and repaint invalidate(); } /** * Set the fan-shaped area 0-360 * UIThred */ public void setSweep(float mSweep) { = mSweep; //Call onDraw and repaint invalidate(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int resultWidth = measureWidth(widthMeasureSpec); int resultHeight = measureHeight(heightMeasureSpec); setMeasuredDimension(resultWidth, resultHeight); } /** * Drawn width * Actually, I don't understand the contents, I feel ashamed... I will understand them in the future...0.0 */ private int measureWidth(int widthMeasureSpec) { int size = (widthMeasureSpec); int mode = (widthMeasureSpec); int result; if (mode == ) { result = size; } else { result = DEFAULT_WIDTH; if (mode == MeasureSpec.AT_MOST) { result = (size, DEFAULT_WIDTH); } } return result; } /** * Drawn high * I don't understand the content here either, I'm so ashamed... I'll understand it in the future...0.0 */ private int measureHeight(int heightMeasureSpec) { int size = (heightMeasureSpec); int mode = (heightMeasureSpec); int result; if (mode == ) { result = size; } else { result = DEFAULT_HEIGHT; if (mode == MeasureSpec.AT_MOST) { result = (size, DEFAULT_HEIGHT); } } return result; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { = h; = w; rectF = new RectF(0, 0, w, h); (w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { //Draw a fan shape (rectF, -90, mSweep, true, paint); } }
Write a custom View, obviously we want to use it, so the layout file states: (But one thing to note is that if you want to control its position and size, you need to use ViewGroup to wrap it here, and control it by setting the position and size of ViewGroup. As for why, I also want to know 0.0)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andro xmlns:tools="/tools" android: android:layout_width="match_parent" android:layout_height="match_parent" tools:context=""> <RelativeLayout android:layout_centerInParent="true" android:layout_width="20dp" android:background="#00f" android:layout_height="20dp"> < android:layout_width="wrap_content" android: android:layout_height="wrap_content" /> </RelativeLayout> </RelativeLayout>
Finally, we draw the animation shape in the code, and the round fan countdown is:
:
public class MainActivity extends AppCompatActivity { private SweepView sweepView; float angle = 0;//Drawing angle @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); sweepView = (SweepView) findViewById(); (); //Set the brush color (0); //Initial drawing 0 degrees new Thread(new Runnable() { @Override public void run() { while (angle <= 360) { //This is equivalent to drawing a complete circle, combining the following 3.6 and 50, that is, the 5-second countdown angle += 3.6; runOnUiThread(new Runnable() { @Override public void run() { (angle); } }); try { (50); } catch (InterruptedException e) { (); } } } }).start(); } }
3. Summary
In the custom view, I don’t understand a lot, because I lack the understanding of some custom methods, but I think I will slowly understand the meaning of some of the methods in the future. Of course, if you accidentally see this article, I hope you can solve my doubts. I am very grateful.