Simple use of SurfaceView in Android game development

Source: Internet
Author: User

Simple use of SurfaceView in Android game development
Related Knowledge points: 1. Call back a SurfaceView to implement a SurfaceHolder. Callback interface. To notify the View when the underlying Surface status changes, this interface needs to implement the following three methods: public void surfaceCreated (SurfaceHolder holder) {} public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {} public void surfaceDestroyed (SurfaceHolder holder) {} 2. SurfaceHolder this is an interface used to control the surface, which provides a control over the surface size, the getHolder () function of SurfaceView can obtain the SurfaceHolder object, and the Surface is in the SurfaceHolder object. 3. Specific Code: MySurfaceView. java

package cn.llbb.testsurfaceviewdemo;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.Rect;import android.graphics.RectF;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;public class MySurfaceView extends SurfaceView implements Callback {private SurfaceHolder sfh;private Canvas canvas;private Paint paint;public MySurfaceView(Context context) {super(context);sfh = getHolder();sfh.addCallback(this);paint = new Paint();paint.setColor(Color.RED);setFocusable(true);}public void surfaceCreated(SurfaceHolder holder) {MyDraw();}public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}public void surfaceDestroyed(SurfaceHolder holder) {}private void MyDraw(){try{canvas = sfh.lockCanvas();if(canvas != null){canvas.drawColor(Color.WHITE);canvas.drawText("drawtext", 10, 10, paint);canvas.drawPoint(10, 20, paint);canvas.drawPoints(new float[]{10,30,30,30}, paint);canvas.drawLine(10,40,50,40, paint);canvas.drawLines(new float[]{10,50,50,50,70,50,110,50}, paint);canvas.drawRect(10,60,40,100, paint);Rect rect = new Rect(10,110,60,130);canvas.drawRect(rect, paint);RectF rectf = new RectF(10,140,60,170);canvas.drawRoundRect(rectf, 20, 20, paint);canvas.drawCircle(20, 200, 20, paint);canvas.drawArc(new RectF(150,20,200,70), 0, 230, true, paint);canvas.drawOval(new RectF(150,80,180,100), paint);Path path = new Path();path.moveTo(160,150);path.lineTo(200,150);path.lineTo(180,200);path.close();canvas.drawPath(path, paint);Path pathcircle = new Path();pathcircle.addCircle(130, 260, 20, Path.Direction.CCW);canvas.drawTextOnPath("pathtext", pathcircle, 10, 20, paint);}}catch(Exception e){}finally{sfh.unlockCanvasAndPost(canvas);}}}MainActivity.javapackage cn.llbb.testcanvasdemo;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.Window;import android.view.WindowManager;public class MainActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);setContentView(new MySurfaceView(this));}}

 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.