• 画布函数介绍-android游戏开发


    主要函数介绍:

    //----设置画布绘图无锯齿
                    canvas.setDrawFilter(pfd);    
                    //----利用填充画布,刷屏
                    canvas.drawColor(Color.BLACK);
                    //----绘制文本
                    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);
                    //----绘制矩形2
                    Rect rect = new Rect(10, 110, 60, 130);
                    canvas.drawRect(rect, paint);
                    //----绘制圆角矩形
                    RectF rectF = new RectF(10, 140, 60, 170);
                    canvas.drawRoundRect(rectF, 5, 5, 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);
                    //路线1
                    path.lineTo(200, 150);
                    //路线2
                    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);

    界面:

    案例源码:

      1 package caicai.animation;
      2 import android.content.Context;
      3 import android.graphics.Bitmap;
      4 import android.graphics.BitmapFactory;
      5 import android.graphics.Canvas;
      6 import android.graphics.Color;
      7 import android.graphics.Paint;
      8 import android.graphics.PaintFlagsDrawFilter;
      9 import android.graphics.Path;
     10 import android.graphics.Rect;
     11 import android.graphics.RectF;
     12 import android.util.Log;
     13 import android.view.MotionEvent;
     14 import android.view.SurfaceHolder;
     15 import android.view.SurfaceHolder.Callback;
     16 import android.view.SurfaceView;
     17 
     18 public class canvarsView extends SurfaceView implements Callback, Runnable {
     19     private Paint paint;// 画布
     20     private SurfaceHolder sfh; // 用于控制SurfaceView
     21     private Canvas canvas;// 画布
     22     private boolean flag;// 关闭线程标志
     23     private Thread th;// 新建线程
     24 
     25     //设置画布绘图无锯齿
     26     private PaintFlagsDrawFilter pfd=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG);
     27 
     28     public canvarsView(Context context) {
     29         super(context);
     30         sfh = this.getHolder();// 实例SurfaceHolder
     31         sfh.addCallback(this);// 为SurfaceView添加状态监听
     32         paint = new Paint();//实例一个画笔
     33         paint.setColor(Color.WHITE);//设置画笔颜色为白色
     34         setFocusable(true);//设置焦点
     35     }
     36 
     37     @Override
     38     public void surfaceCreated(SurfaceHolder holder) {
     39         flag = true;
     40         //实例线程
     41         th = new Thread(this);
     42         //启动线程
     43         th.start();
     44     }
     45 
     46     public void mydraw() {
     47         try {
     48             canvas = sfh.lockCanvas();//锁定画布
     49             if (canvas != null) {
     50                 //----设置画布绘图无锯齿
     51                 canvas.setDrawFilter(pfd);    
     52                 //----利用填充画布,刷屏
     53                 canvas.drawColor(Color.BLACK);
     54                 //----绘制文本
     55                 canvas.drawText("drawtext", 10, 10, paint);
     56                 //----绘制像素点
     57                 canvas.drawPoint(10,20,paint);
     58                 //----绘制多个像素点
     59                 canvas.drawPoints(new float[]{10,30,30,30}, paint);
     60                 //----绘制直线
     61                 canvas.drawLine(10, 40, 50, 40, paint);
     62                 //----绘制多条直线
     63                 canvas.drawLines(new float[] { 10, 50, 50, 50, 70, 50, 110, 50 }, paint);
     64                 //----绘制矩形
     65                 canvas.drawRect(10, 60, 40, 100, paint);
     66                 //----绘制矩形2
     67                 Rect rect = new Rect(10, 110, 60, 130);
     68                 canvas.drawRect(rect, paint);
     69                 //----绘制圆角矩形
     70                 RectF rectF = new RectF(10, 140, 60, 170);
     71                 canvas.drawRoundRect(rectF, 5, 5, paint);
     72                 //----绘制圆形
     73                 canvas.drawCircle(20, 200, 20, paint);
     74                 //----绘制弧形
     75                 canvas.drawArc(new RectF(150, 20, 200, 70), 0, 230, true, paint);
     76                 //----绘制椭圆
     77                 canvas.drawOval(new RectF(150, 80, 180, 100), paint);
     78                 //----绘制指定路径图形
     79                 Path path = new Path();
     80                 //设置路径起点
     81                 path.moveTo(160, 150);
     82                 //路线1
     83                 path.lineTo(200, 150);
     84                 //路线2
     85                 path.lineTo(180, 200);
     86                 //路径结束
     87                 path.close();
     88                 canvas.drawPath(path, paint);
     89                 //----绘制指定路径图形
     90                 Path pathCircle = new Path();
     91                 //添加一个圆形的路径
     92                 pathCircle.addCircle(130, 260, 20, Path.Direction.CCW);
     93                 //----绘制带圆形的路径文本
     94                 canvas.drawTextOnPath("PathText", pathCircle, 10, 20, paint);
     95             } else {
     96                 Log.i("tt", "获取不到画布");//释放画布
     97             }
     98         } catch (Exception e) {
     99 
    100         } finally {
    101             if (canvas != null)
    102                 sfh.unlockCanvasAndPost(canvas);
    103         }
    104     }
    105     /**
    106      * SurfaceView视图状态发生改变,响应此函数
    107      */
    108     @Override
    109     public void surfaceChanged(SurfaceHolder holder, int format, int width,
    110             int height) {
    111         // TODO Auto-generated method stub
    112 
    113     }
    114     /**
    115      * SurfaceView视图消亡时,响应此函数
    116      */
    117     @Override
    118     public void surfaceDestroyed(SurfaceHolder holder) {
    119         flag = false;//结束游戏,设置线程关闭标志为false
    120 
    121     }
    122 
    123     public void logic() {
    124 
    125     };
    126 
    127     @Override
    128     public void run() {
    129         while (flag) {
    130             long start = System.currentTimeMillis();
    131             mydraw();
    132             logic();
    133             long end = System.currentTimeMillis();
    134             try {
    135                 if (end - start < 50) {
    136                     Thread.sleep(50 - (end - start));
    137                 }
    138             } catch (InterruptedException e) {
    139                 e.printStackTrace();
    140             }
    141         }
    142     }
    143     //获取点击坐标
    144     @Override
    145     public boolean onTouchEvent(MotionEvent event) {
    146         return true;
    147     }
    148 
    149 }
    canvarsView
     1 package caicai.animation;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.view.Window;
     6 import android.view.WindowManager;
     7 
     8 public class MainActivity extends Activity {
     9     /** Called when the activity is first created. */
    10     @Override
    11     public void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         //设置全屏
    14         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    15         requestWindowFeature(Window.FEATURE_NO_TITLE);
    16         //显示自定义的SurfaceView视图
    17         setContentView(new canvarsView(this));
    18 
    19     }
    20 }
    MainActivity

    谢谢支持下网站“趣淘鼓浪屿(www.qtgly.com)”

  • 相关阅读:
    H264解码的一个測试程序
    padding与margin的差别
    CreateProcess的使用方法
    eclipse设置快捷键sysout+Alt+/后出System.out.println!亲測可用!
    crm操作权限
    LeetCode Decode Ways
    epoll使用具体解释(精髓)
    一位Erlang程序猿的自白
    用JAVA写一个函数,功能例如以下: 随意给定一组数, 找出随意数相加之后的结果为35(随意设定)的情况
    liveness 生存性/活性
  • 原文地址:https://www.cnblogs.com/clarence/p/3284865.html
Copyright © 2020-2023  润新知