SurfaceView is a classic in Android. It can implement all the functions required for two-dimensional animation. If you like animation, you can learn it in depth. Today we will start with this article, first understand the animation effect of SurfaceView.
1. A simple small example:
1) MySurfaceView. java
Package com. example. l0904_surfaceview;/*** use the combination of SurfaceView and Canvas to complete * simple animation effect */import java. util. timer; import java. util. timerTask; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. surfaceHolder; import android. view. surfaceView; // inherits the SurfaceView parent class and implements SurfaceView. callback interface public class MySurfaceView extends Surf AceView implements SurfaceHolder. Callback {// create the Paint brush object private paint Paint = new Paint (); // defines the Timer tool, which is used to schedule the execution of specified tasks in a background thread. It can plan to execute a task once or repeatedly. Private Timer timer; // an abstract class of TimerTask. Its subclass represents a task private TimerTask task that can be scheduled by Timer; // define and initialize the coordinates of the image to be painted. private float x = 0; private float y = 0; private float speedx = 50; private float speedy = 50; // define and initialize the change volume of coordinate movement private float addx = 2; private float addy = 2; /*** SurfaceView constructor * specifies the color of the paint brush to be initialized * @ param context */public MySurfaceView (Context context) {super (context); paint. setColor (Color. BLUE); getHolder (). addCallback (this);}/*** plotting method, in which the specific process is completed */public void draw () {// lock the Canvas canvas = getHolder (). lockCanvas (); // initialize the canvas. drawColor (Color. WHITE); // draw the image. A rectangular canvas is drawn here. drawRect (x, y, speedx + x, speedy + y, paint); x + = addx; y + = addy; // The following is the moving path of the rectangle. if (x <0) {// if the left boundary coordinate of the image exceeds the left screen, move addx = Math to the right. abs (addx);} if (x> getWidth ()-speedx) {// if the right boundary coordinate of the image exceeds the width of the screen, move addx =-Math to the left. abs (addx);} if (y <0) {addy = Math. abs (addy);} if (y> getHeight ()-speedy) {addy =-Math. abs (addy);} // unlock the canvas getHolder (). unlockCanvasAndPost (canvas);}/*** start the timer background thread */public void startTimer () {Timer = new timer (); task = new TimerTask () {@ Override public void run () {// call the drawing method draw () ;}}in the timer thread; // set the timer to start this task every 0.1 seconds, implement the animation effect timer. schedule (task, 100,100);}/*** Method for stopping the timer thread */public void stopTimer () {timer. cancel ();}/*** the following method must be rewritten */@ Override public void surfaceCreated (SurfaceHolder holder) {// start the thread startTimer () ;}@ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) after SurfaceView is created) {}@ Override public void surfaceDestroyed (SurfaceHolder holder) {// The thread stopTimer () must be terminated before the SurfaceView is destroyed ();}}
2) MainActivity. java
Package com. example. l0904_surfaceview; import android. app. activity; import android. OS. bundle; public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // sets the view setContentView (new MySurfaceView (this ));}}
3) running effect:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1020101F8-0.jpg "style =" float: none; "title =" Capture. JPG "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1020105394-1.jpg "style =" float: none; "title =" Capture 1.JPG"/>
In this way, the effect is not obvious. We can track the coordinates of a specific point:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1020105348-2.jpg "style =" float: none; "title =" Capture. JPG "/>
.
.
.
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1020104J8-3.jpg "style =" float: none; "title =" Capture 1.JPG"/>
2. Use SurfaceView to implement a well-known screen protection effect-random bubble.
1) MySurfaceView. java
Package com. example. l0904_collection; import java. util. timer; import java. util. timerTask; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. surfaceHolder; import android. view. surfaceView; public class MySurfaceView extends SurfaceView implements SurfaceHolder. callback {private Paint paint = new Paint (); private Timer timer; private TimerTask task; private Bolls bolls; public MySurfaceView (Context context) {super (context); paint. setColor (Color. YELLOW); getHolder (). addCallback (this);} public void draw () {Canvas canvas = getHolder (). lockCanvas (); // lock the canvas // draw a graphic canvas. drawColor (Color. WHITE); // initialize the canvas for (int I = 0; I <20; I ++) {bolls = new Bolls (this); canvas. drawCircle (bolls. getCx (), bolls. getCy (), bolls. getRadius (), paint); bolls. draw ();} getHolder (). unlockCanvasAndPost (canvas); // unlock canvas} public void startTimer () {timer = new Timer (); task = new TimerTask () {@ Override public void run () {draw () ;}}; timer. schedule (task, 100,100);} public void stopTimer () {timer. cancel () ;}@ Override public void surfaceCreated (SurfaceHolder holder) {startTimer () ;}@ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}@ Override public void surfaceDestroyed (SurfaceHolder holder) {stopTimer ();}}
2) Bolls. java
package com.example.l0904_collection;public class Bolls { private float cx; private float cy; private float radius; private int addx; private int addy; private int addRadius; MySurfaceView view; public Bolls(MySurfaceView view){ this.view=view; cx=(float) (Math.random()*300); cy=(float) (Math.random()*450); radius=(float) (Math.random()*30); addx=(int) (Math.random()*10); addy=(int) (Math.random()*10); addRadius=(int) (Math.random()*10); } public float getCx() { return cx; } public void setCx(float cx) { this.cx = cx; } public float getCy() { return cy; } public void setCy(float cy) { this.cy = cy; } public float getRadius() { return radius; } public void setRadius(float radius) { this.radius = radius; } public void draw(){ cx += addx; cy += addy; radius+=addRadius; if (cx < radius) { addx = Math.abs(addx); } if (cx > view.getWidth() - radius) { addx = -Math.abs(addx); } if (cy < radius) { addy = Math.abs(addy); } if (cy > view.getHeight() -radius) { addy = -Math.abs(addy); } }}
3) MainActivity. java
package com.example.l0904_collection;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new MySurfaceView(this)); }}
(4) Running Effect
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1020104091-4.jpg "style =" float: none; "title =" Capture. JPG "/>
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1020103421-5.jpg "style =" float: none; "title =" Capture 1.JPG"/>
This article is from the MySpace blog, please be sure to keep this source http://wangzhaoli.blog.51cto.com/7607113/1290355