When developing android mobile games to achieve more complex animation effects, SurfaceView is often used instead of View (of course, View can also achieve complex Animation),
Compared with View, SufaceView has some features:
- < /span>When using SurfaceView to develop, you can directly obtain the Canvas object, instead of obtaining the Canvas object in the onDraw method like View.
- SurfaceView supports double buffering technology, which makes drawing graphics more efficient.
- SurfaceView can draw graphics directly in non-UI threads, while View must use the Handler object to send messages to notify the UI thread to draw graphics. ,,,,,,,,,,,,,,,,,,,,,, Have to realize the interface of the Surface View sub-Surface, must be inherited. The interface to implement three methods 1.surfaceCreated: li> ul>
2.surfaceChanged div>3.surfaceDestroyed div>The following example shows using a substantially SurfaceView usage. In the example, use the SurfaceCreated method of SurfaceView to call the OnDraw method to draw a circle
public class MySurfaceViewActivity extends SurfaceView implements SurfaceHolder.Callback,OnTouchListener {public float x; public float y; private SurfaceHolder surfaceHolder; public MySurfaceViewActivity(Context context) {super(context); setOnTouchListener(this); surfaceHolder = this.getHolder(); surfaceHolder.addCallback(this); this.setFocusable(true);} @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {// TODO Auto-generated method stub} @Override public void surfaceCreated(SurfaceHolder holder) {// TODO Auto-generated method stub drawCircle();} @Override public void surfaceDestroyed(SurfaceHolder holder) {// TODO Auto-generated method stub} @Override public boolean onTouch(View view, MotionEvent event) {A nimThread animThread = new AnimThread(event.getX(),event.getY()); Thread thread = new Thread(animThread); thread.start(); return false;} class AnimThread implements Runnable {private float newX,newY; public AnimThread(float newX,float newY ){ this.newX=newX; this.newY=newY;} @Override public void run() {float scale = Math.abs(y-newY)/Math.abs(x-newX) ; while(newX!=x&&newY!=y) {if(newX> x) {x+=1; if(newX
x) {x=newX;}} if(newY >y) {y+=scale; if(newY y) y=newY;} try {drawCircle(); Thread.sleep(50);} catch (Exception e) {}}}} public void drawCircle(){ if(surfaceHolder == null) {return; } Canvas canvas = surfaceHolder.lockCanvas(); canvas.drawColor(Color.BLUE); if (canvas==null) {return;} P aint p = new Paint(); p.setColor(Color.YELLOW); canvas.drawCircle(x, y, 20, p); surfaceHolder.unlockCanvasAndPost(canvas); }) 2.surfaceChanged
3.surfaceDestroyed
The following example shows the basic usage of SurfaceView. In the example, use the SurfaceCreated method of SurfaceView to call the OnDraw method to draw a circle
public class MySurfaceViewActivity extends SurfaceView implements SurfaceHolder.Callback,OnTouchListener {public float x; public float y; private SurfaceHolder surfaceHolder; public MySurfaceViewActivity(Context context) {super(context); setOnTouchListener(this); surfaceHolder = this.getHolder(); surfaceHolder.addCallback(this); this.setFocusable(true);} @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {// TODO Auto-generated method stub} @Override public void surfaceCreated(SurfaceHolder holder) {// TODO Auto-generated method stub drawCircle();} @Override public void surfaceDestroyed(SurfaceHolder holder) {// TODO Auto-generated method stub} @Override public boolean onTouch(View view, MotionEvent event) {AnimThread animThread = new AnimThread(event.getX(),event.getY()); Thread thread = new Thread(animThread); thread.start(); return false;} class AnimThread implements Runnable {private float newX,newY; public AnimThread (float newX,float newY ){ this.newX=newX; this.newY=newY;} @Override public void run() {float scale = Math.abs(y-newY)/Math.abs(x-newX); while(newX!=x&&newY!=y) {if(newX> x) {x+=1; if(newX
x) {x=newX;}} if(newY >y) {y+=scale; if(newY y) y=newY;} try {drawCircle(); Thread.sleep(50);} catch (Exception e) {}}}} public void drawCircle(){ if(surfaceHolder == null) {return;} Canvas canvas = surfaceHolder.lockCanvas(); canvas.drawColor(Color.BLUE); if (canvas==null) {return;} Paint p = new Paint(); p.setColor(Color.YELLOW); canvas.drawCircle(x, y, 20, p); surfaceHolder.unlockCanvasAndPost(canvas); }) WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 1505 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC