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


    函数介绍:

    canvas.drawColor(Color.WHITE);
                    Paint paint1 = new Paint();
                    canvas.drawCircle(40, 30, 20, paint1);
                    paint1.setAntiAlias(true);    //-----设置画笔无锯齿
                    canvas.drawCircle(100, 30, 20, paint1);
                    
                    canvas.drawText("无透明度", 100, 70, new Paint());
                    Paint paint2 = new Paint();
                    paint2.setAlpha(0x77);//-----设置画笔的透明度
                    canvas.drawText("半透明度", 20, 70, paint2);
                    
                    
                    canvas.drawText("锚点", 20, 90, new Paint());
                    Paint paint3 = new Paint();                
                    paint3.setTextAlign(Paint.Align.CENTER);//设置以文本的中心点绘制            
                    canvas.drawText("锚点", 20, 105, paint3);
                    
                    Paint paint4 = new Paint();
                    float len =paint4.measureText("文本宽度:");//------获取文本的长度
                    canvas.drawText("文本长度:"+len, 20, 130, new Paint());
                    
                    //------设置画笔样式
                    canvas.drawRect(new Rect(20,140,40,160), new Paint());
                    Paint paint5 = new Paint();            
                    paint5.setStyle(Style.STROKE);//设置画笔不填充
                    canvas.drawRect(new Rect(60,140,80,160), paint5);
                    
                
                    Paint paint6 = new Paint();
                    paint6.setColor(Color.GRAY);    //------设置画笔颜色
                    canvas.drawText("灰色", 30, 180, paint6);
                    
                    
                    canvas.drawLine(20, 200,70, 200, new Paint());
                    Paint paint7 = new Paint();
                    paint7.setStrokeWidth(7);//------设置画笔的粗细程度
                    canvas.drawLine(20, 220,70, 220,paint7);
                    
                    
                    Paint paint8 = new Paint();
                    paint8.setTextSize(20);
                    canvas.drawText("文字尺寸", 20, 260, paint8);//------设置画笔绘制文本的字体粗细
                    
                    Paint paint9 = new Paint();
                    paint9.setARGB(0x77, 0xff, 0x00, 0x00);//------设置画笔的ARGB分量
                    canvas.drawText("红色半透明", 20, 290, paint9);

    界面:

    案例源码:

      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.Paint.Style;
      9 import android.graphics.PaintFlagsDrawFilter;
     10 import android.graphics.Path;
     11 import android.graphics.Rect;
     12 import android.graphics.RectF;
     13 import android.util.Log;
     14 import android.view.MotionEvent;
     15 import android.view.SurfaceHolder;
     16 import android.view.SurfaceHolder.Callback;
     17 import android.view.SurfaceView;
     18 
     19 public class paintView extends SurfaceView implements Callback, Runnable {
     20     private Paint paint;// 画布
     21     private SurfaceHolder sfh; // 用于控制SurfaceView
     22     private Canvas canvas;// 画布
     23     private boolean flag;// 关闭线程标志
     24     private Thread th;// 新建线程
     25 
     26     //设置画布绘图无锯齿
     27     private PaintFlagsDrawFilter pfd=new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG);
     28 
     29     public paintView(Context context) {
     30         super(context);
     31         sfh = this.getHolder();// 实例SurfaceHolder
     32         sfh.addCallback(this);// 为SurfaceView添加状态监听
     33         paint = new Paint();//实例一个画笔
     34         paint.setColor(Color.WHITE);//设置画笔颜色为白色
     35         setFocusable(true);//设置焦点
     36     }
     37 
     38     @Override
     39     public void surfaceCreated(SurfaceHolder holder) {
     40         flag = true;
     41         //实例线程
     42         th = new Thread(this);
     43         //启动线程
     44         th.start();
     45     }
     46 
     47     public void mydraw() {
     48         try {
     49             canvas = sfh.lockCanvas();//锁定画布
     50             if (canvas != null) {
     51                 canvas.drawColor(Color.WHITE);
     52                 Paint paint1 = new Paint();
     53                 canvas.drawCircle(40, 30, 20, paint1);
     54                 paint1.setAntiAlias(true);    //-----设置画笔无锯齿
     55                 canvas.drawCircle(100, 30, 20, paint1);
     56                 
     57                 canvas.drawText("无透明度", 100, 70, new Paint());
     58                 Paint paint2 = new Paint();
     59                 paint2.setAlpha(0x77);//-----设置画笔的透明度
     60                 canvas.drawText("半透明度", 20, 70, paint2);
     61                 
     62                 
     63                 canvas.drawText("锚点", 20, 90, new Paint());
     64                 Paint paint3 = new Paint();                
     65                 paint3.setTextAlign(Paint.Align.CENTER);//设置以文本的中心点绘制            
     66                 canvas.drawText("锚点", 20, 105, paint3);
     67                 
     68                 Paint paint4 = new Paint();
     69                 float len =paint4.measureText("文本宽度:");//------获取文本的长度
     70                 canvas.drawText("文本长度:"+len, 20, 130, new Paint());
     71                 
     72                 //------设置画笔样式
     73                 canvas.drawRect(new Rect(20,140,40,160), new Paint());
     74                 Paint paint5 = new Paint();            
     75                 paint5.setStyle(Style.STROKE);//设置画笔不填充
     76                 canvas.drawRect(new Rect(60,140,80,160), paint5);
     77                 
     78             
     79                 Paint paint6 = new Paint();
     80                 paint6.setColor(Color.GRAY);    //------设置画笔颜色
     81                 canvas.drawText("灰色", 30, 180, paint6);
     82                 
     83                 
     84                 canvas.drawLine(20, 200,70, 200, new Paint());
     85                 Paint paint7 = new Paint();
     86                 paint7.setStrokeWidth(7);//------设置画笔的粗细程度
     87                 canvas.drawLine(20, 220,70, 220,paint7);
     88                 
     89                 
     90                 Paint paint8 = new Paint();
     91                 paint8.setTextSize(20);
     92                 canvas.drawText("文字尺寸", 20, 260, paint8);//------设置画笔绘制文本的字体粗细
     93                 
     94                 Paint paint9 = new Paint();
     95                 paint9.setARGB(0x77, 0xff, 0x00, 0x00);//------设置画笔的ARGB分量
     96                 canvas.drawText("红色半透明", 20, 290, paint9);
     97             } else {
     98                 Log.i("tt", "获取不到画布");//释放画布
     99             }
    100         } catch (Exception e) {
    101 
    102         } finally {
    103             if (canvas != null)
    104                 sfh.unlockCanvasAndPost(canvas);
    105         }
    106     }
    107     /**
    108      * SurfaceView视图状态发生改变,响应此函数
    109      */
    110     @Override
    111     public void surfaceChanged(SurfaceHolder holder, int format, int width,
    112             int height) {
    113         // TODO Auto-generated method stub
    114 
    115     }
    116     /**
    117      * SurfaceView视图消亡时,响应此函数
    118      */
    119     @Override
    120     public void surfaceDestroyed(SurfaceHolder holder) {
    121         flag = false;//结束游戏,设置线程关闭标志为false
    122 
    123     }
    124 
    125     public void logic() {
    126 
    127     };
    128 
    129     @Override
    130     public void run() {
    131         while (flag) {
    132             long start = System.currentTimeMillis();
    133             mydraw();
    134             logic();
    135             long end = System.currentTimeMillis();
    136             try {
    137                 if (end - start < 50) {
    138                     Thread.sleep(50 - (end - start));
    139                 }
    140             } catch (InterruptedException e) {
    141                 e.printStackTrace();
    142             }
    143         }
    144     }
    145     //获取点击坐标
    146     @Override
    147     public boolean onTouchEvent(MotionEvent event) {
    148         return true;
    149     }
    150 
    151 }
    paintView
     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 paintView(this));
    18 
    19     }
    20 }
    MainActivity

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

  • 相关阅读:
    谈谈Oracle基本操作(下)
    谈谈Oracle基本操作(上)
    java理论之java多线程与网络编程
    java理论之java--GUI(图形用户管理)与 IO/流
    java理论之java数组和集合
    存图片的
    .html()渲染后的内容还是带标签的字符串的问题
    Vuejs报错error: Unexpected console statement (no-console) at src... 解决办法
    常用正则表达式
    移动WEB---01.关于屏幕像素
  • 原文地址:https://www.cnblogs.com/clarence/p/3284906.html
Copyright © 2020-2023  润新知