• 11_SurfaceView绘图


      学习SurfaceView绘图API的使用方法,为接下来使用SurfaceView开发游戏做准备。

     1 package com.example.surfaceview;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 
     6 public class MainActivity extends Activity {
     7 
     8     @Override
     9     protected void onCreate(Bundle savedInstanceState) {
    10         super.onCreate(savedInstanceState);
    11         setContentView(new MyView(this));
    12 
    13     }
    14 
    15 }
    MainActivity
     1 package com.example.surfaceview;
     2 
     3 import android.content.Context;
     4 import android.graphics.Canvas;
     5 import android.graphics.Color;
     6 import android.graphics.Paint;
     7 import android.view.SurfaceHolder;
     8 import android.view.SurfaceView;
     9 
    10 public class MyView extends SurfaceView implements SurfaceHolder.Callback {
    11 
    12     private Paint paint = null;
    13 
    14     public MyView(Context context) {
    15         super(context);
    16         paint = new Paint();
    17         paint.setColor(Color.RED);
    18         getHolder().addCallback(this);
    19     }
    20 
    21     // 一定要锁定画布和解锁画布
    22     public void draw() {
    23         Canvas canvas = getHolder().lockCanvas();
    24         canvas.drawColor(Color.WHITE);
    25         canvas.drawRect(0, 0, 100, 100, paint);
    26 
    27         getHolder().unlockCanvasAndPost(canvas);
    28 
    29     }
    30 
    31     @Override
    32     public void surfaceCreated(SurfaceHolder holder) {
    33         draw();
    34     }
    35 
    36     @Override
    37     public void surfaceChanged(SurfaceHolder holder, int format, int width,
    38             int height) {
    39         // TODO Auto-generated method stub
    40 
    41     }
    42 
    43     @Override
    44     public void surfaceDestroyed(SurfaceHolder holder) {
    45         // TODO Auto-generated method stub
    46 
    47     }
    48 
    49 }
    MyView
  • 相关阅读:
    不要再浪费时间阅读文献技巧本
    Reporting Services 中的分页方式Report Builder 3.0
    国外实时数据库比较
    转VS2010 C++下编译调试MongoDB源码 代震军
    C#中异步和多线程的区别
    Python seems amazing!
    嵌入式(armcc)自动调用函数
    MSSQL2008 日志文件收缩
    萝卜地...分享;
    Apache配置多个站点;
  • 原文地址:https://www.cnblogs.com/510602159-Yano/p/4065376.html
Copyright © 2020-2023  润新知