• android SurfaceView绘制 重新学习--基础绘制


    自从大二写了个android游戏去参加比赛,之后就一直写应用,一直没用过SurfaceView了,现在进入了游戏公司,准备从基础开始重新快速的学一下这个,然后再去研究openGL和游戏引擎。

    直接上代码吧:

     1 import android.content.Context;
     2 import android.util.AttributeSet;
     3 import android.util.Log;
     4 import android.view.SurfaceHolder.Callback;
     5 import android.view.SurfaceView;
     6 import android.graphics.Canvas;
     7 import android.graphics.Color;
     8 import android.graphics.Paint;
     9 import android.graphics.Typeface;
    10 import android.view.SurfaceHolder;
    11 import android.view.animation.Animation;
    12 
    13 public class MySurfaceView extends SurfaceView implements Callback, Runnable {
    14 
    15     private SurfaceHolder sfh;
    16     private Thread th;
    17     private Canvas canvas;
    18     private Paint paint;
    19     private boolean threadFlag;
    20 
    21     public MySurfaceView(Context context) {
    22         // this(context, null);
    23 
    24         super(context);
    25         sfh = this.getHolder();
    26         sfh.addCallback(this);
    27         paint = new Paint();
    28         paint.setAntiAlias(true);
    29         paint.setColor(Color.RED);
    30         paint.setTextSize(20);
    31         Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
    32         paint.setTypeface(font);
    33         this.setKeepScreenOn(true);
    34     }
    35 
    36     public MySurfaceView(Context context, AttributeSet attrs) {
    37         super(context, attrs);
    38         // th = new Thread(this);
    39         // sfh = this.getHolder();
    40         // sfh.addCallback(this);
    41         // paint = new Paint();
    42         // paint.setAntiAlias(true);
    43         // paint.setColor(Color.RED);
    44         // this.setKeepScreenOn(true);
    45     }
    46 
    47     public void startAnimation(Animation animation) {
    48         super.startAnimation(animation);
    49     }
    50 
    51     public void surfaceCreated(SurfaceHolder holder) {
    52         threadFlag = true;
    53         th = new Thread(this);
    54         th.start();
    55     }
    56 
    57     private void draw() {
    58         try {
    59             canvas = sfh.lockCanvas(); // 获取画布
    60             if (canvas != null) {
    61                 canvas.drawColor(Color.WHITE);// 画布颜色
    62                 canvas.drawText("奋斗的小猿", 100, 100, paint);
    63                 canvas.drawText("加班,还是不加班,it's a question!", 100, 130, paint);
    64             }
    65         } catch (Exception ex) {
    66         } finally {
    67             if (canvas != null)
    68                 sfh.unlockCanvasAndPost(canvas);
    69         }
    70     }
    71 
    72     public void run() {
    73         while (threadFlag) {
    74             Log.i("ceshi", "threadFlag=true");
    75             draw();
    76             try {
    77                 Thread.sleep(100);
    78             } catch (InterruptedException e) {
    79                 e.printStackTrace();
    80             }
    81         }
    82     }
    83 
    84     public void surfaceChanged(SurfaceHolder holder, int format, int width,
    85             int height) {
    86     }
    87 
    88     public void surfaceDestroyed(SurfaceHolder holder) {
    89         threadFlag = false;
    90     }
    91 
    92 }

    这里Activity用的是

    setContentView(new MySurfaceView(getApplicationContext()));

    如果想在布局中引用:

    1 <com.tq.listviewtest.MySurfaceView
    2         android:layout_width="match_parent"
    3         android:layout_height="match_parent"
    4         android:visibility="visible" >
    5  </com.tq.listviewtest.MySurfaceView>
    1 setContentView(R.layout.activity_main);

    则构造方法应改为:

     1 public MySurfaceView(Context context) {
     2         this(context, null);
     3     }
     4 
     5     public MySurfaceView(Context context, AttributeSet attrs) {
     6         super(context, attrs);
     7         sfh = this.getHolder();
     8         sfh.addCallback(this);
     9         paint = new Paint();
    10         paint.setAntiAlias(true);
    11         paint.setColor(Color.RED);
    12         paint.setTextSize(20);
    13         Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);
    14         paint.setTypeface(font);
    15         this.setKeepScreenOn(true);
    16     }
    View Code

    因为android 加载布局时,只是去执行了第二个构造方法,第一个构造方法没有执行,所以我们要把初始化写在第二个构造方法中了。

  • 相关阅读:
    Spring集成缓存
    spring配置注解context:annotation-config和context:component-scan区别
    Python新手练手项目
    Spring Boot常用功能
    Java IO NIO详细讲解
    设计模式
    [MacOS]Atom 安装与汉化
    [Nexus3]本地YUM源私有仓安装
    [Docker]安装
    [Linux]LVM扩展卷
  • 原文地址:https://www.cnblogs.com/huyong123/p/3307497.html
Copyright © 2020-2023  润新知