Structure:
Baseview:
Package Com.caiduping.canvas;import Android.content.context;import Android.graphics.canvas;import Android.util.attributeset;import Android.view.view;public abstract class Baseview extends view{/thread private Mythre Ad myThread; Public Baseview (context context, AttributeSet Attrs) {Super (context, attrs); } public Baseview (context context) {super (context); } protected abstract void Ondrawsub (canvas canvas);//Draw an image protected abstract void logic ();//logical Method Subclass implementation @Override protected void OnDraw (canvas canvas) {if (null==mythread) {Mythre Ad=new MyThread (); Mythread.start (); }else{ondrawsub (canvas); }} private Boolean running=true;//control loop @Override protected void Ondetachedfromwindow () { running=false;//destroy the view when set to False, exit the Infinite Loop Super.ondetachedfromwindow (); } Open a child thread drawing UI private class MyThread extends thread{@Override public void run () { while (running) {logic (); Postinvalidate ();//Redraw, will call OnDraw try {thread.sleep (200); Thread hangs for 0.2 seconds, arc speed is 0.2 seconds/radians} catch (Interruptedexception e) {e.printstacktrace (); } } } } }
Logicview:
Package Com.caiduping.canvas;import Java.util.random;import Android.content.context;import Android.graphics.Canvas ; Import Android.graphics.paint;import Android.graphics.rectf;import Android.util.attributeset;public class Logicview extends baseview{private Paint paint;//brush private int x=0; Private RECTF rectf=new RECTF (150,150,380,380); The/*rect class is primarily used to represent a rectangular area in a coordinate system * and can do some simple things about it. This rectangular area needs to be represented by the lower left-right two coordinate points (Left,top,right,bottom) If you declare a rect class like this: Rect rect=new rect (100,50,300,50 0); Then the lower right corner (300,500) is not actually inside the rectangle, but the upper left corner (100,50) in other words, the rectangle actually represents the area is: (100,50,299,499) */private int sweepangle=0 ;//The End of the arc is the private random random=new random ();//random number public Logicview (context context, attributeset at TRS) {Super (context, attrs); Initialize init (); } public Logicview (context context) {super (context); Init (); }//initialization brush private void init () { Paint=new Paint (); Paint.settextsize (30); } @Override protected final void ondrawsub (canvas canvas) {canvas.drawtext ("Android Custom paint text animation", X, +, paint); /* The first parameter is the X y-coordinate of the X y-coordinate at the top left of the RECTF the second parameter is the arc's start angle The third parameter is the arc's end angle The fourth parameter is true: The drawing is a fan of false: Arc The fifth parameter of the line is the brush */Canvas.drawarc (RECTF, 0, SweepAngle, true, paint); } @Override protected void logic () {x+=20; sweepangle+=5;//each Radian plus 5//randomly sets the color of the brush int r=random.nextint (255); Red int g=random.nextint (255); Green int B=random.nextint (255); Blue Paint.setargb (255, R, G, b); if (sweepangle>=360) {//If radians are greater than 360 ° then start from scratch sweepangle=0; } if (X>getwidth ()) {//If you move to the screen, start from the beginning int textwidth= (int) paint.measuretext ("Android Custom drawing Text animation ");Measurement text width x=0-textwidth; } } }
Mainactivity:
Setcontentview (New Logicview (this));
Run
Android Custom paint text animations