The following describes how to implement the effects of a splash screen. First, the custom class inherits from the View and then the onDraw method is rewritten. Previously, the onDraw method can be used for plotting, but only once, how can we implement a loop. It is easy to think of using the invalidate () method, because using this method will call the onDraw method, which forms an endless loop and keeps refreshing the drawing interface. Of course there is another
PostInvalidate (). method, which differs from invalidate () in that it is used for non-UI threads and invalidate () must be used in UI threads. Therefore, the code can be written as follows:
import java.util.Random;import android.content.Context;import android.graphics.Canvas;import android.view.View;class RenderView extends View {Random rand = new Random();public RenderView(Context context) {super(context);}protected void onDraw(Canvas canvas) {canvas.drawRGB(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));invalidate();}}
The following is an example of the previous article:
Import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rectF; import android. OS. bundle; import android. view. view; public class Test extends GraphicsActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (new SampleView (this);} private static class SampleView extends View {// Paint brush private Paint [] mPaints; // Paint brush private Paint mFramePaint; // whether the center exists private boolean [] mUseCenters; // rectangular private RectF [] mOvals; // rectangular private RectF mBigOval; // start radian private float mStart; // incremental radian private float mSweep; // index private int mBigIndex; // scan incremental private static final float SWEEP_INC = 2; private static final float START_INC = 15; public SampleView (Context context) {super (context); mPaints = new Paint [4]; mUseCenters = new boolean [4]; mOvals = new RectF [4]; mPaints [0] = new Paint (); mPaints [0]. setAntiAlias (true); mPaints [0]. setStyle (Paint. style. FILL); mPaints [0]. setColor (0x88FF0000); mUseCenters [0] = false; mPaints [1] = new Paint (mPaints [0]); mPaints [1]. setColor (0x8800FF00); mUseCenters [1] = true; mPaints [2] = new Paint (mPaints [0]); mPaints [2]. setStyle (Paint. style. STROKE); mPaints [2]. setStrokeWidth (4); mPaints [2]. setColor (0x880000FF); mUseCenters [2] = false; mPaints [3] = new Paint (mPaints [2]); mPaints [3]. setColor (0x88888888); mUseCenters [3] = true; mBigOval = new RectF (40, 10,280,250); mOvals [0] = new RectF (10,270, 70,330 ); mOvals [1] = new RectF (90,270,150,330); mOvals [2] = new RectF (170,270,230,330); mOvals [3] = new RectF (250,270,310,330); mFramePaint = new Paint (); mFramePaint. setAntiAlias (true); mFramePaint. setStyle (Paint. style. STROKE); mFramePaint. setStrokeWidth (0);}/*** @ category * @ param canvas * @ param oval * @ param useCenter * @ param paint */private void drawArcs (Canvas canvas, RectF oval, boolean useCenter, Paint paint) {// draw a rectangular canvas. drawRect (oval, mFramePaint); // draw an arc canvas. drawArc (oval, mStart, mSweep, useCenter, paint) ;}@ Override protected void onDraw (Canvas canvas) {// set the background color canvas. drawColor (Color. WHITE); // draw a large rectangle drawArcs (canvas, mBigOval, mUseCenters [mBigIndex], mPaints [mBigIndex]); // draw four small rectangles for (int I = 0; I <4; I ++) {drawArcs (canvas, mOvals [I], mUseCenters [I], mPaints [I]);} /*** calculate radian */mSweep + = SWEEP_INC; if (mSweep> 360) {mSweep-= 360; mStart + = START_INC; if (mStart> = 360) {mStart-= 360;} // change mBigIndex = (mBigIndex + 1) % mOvals. length ;}// refresh invalidate ();}}}
The effect is as follows:
In android, the class for drawing 2D flat images is basically in the android. graphics package, such as commonly used Paint, Path, Canvas, Rect, Bitmap, Color, Matrix, and Point. These are basic but important plotting classes, so you must master them first.