案例简介:通过贝塞尔曲线手指点击屏幕划出曲线来
主要内容:
path.reset();// 重置path
path.moveTo(x[0], y[0]);// 贝赛尔曲线的起始点
path.quadTo(x[i-1], y[i-1], x[i], y[i]);// 设置贝赛尔曲线的操作点以及终止点
canvas.drawPath(path, paint); // 绘制贝赛尔曲线(Path)
源码:
1 package caicai.animation; 2 import java.util.Random; 3 4 import android.content.Context; 5 import android.graphics.Bitmap; 6 import android.graphics.BitmapFactory; 7 import android.graphics.Canvas; 8 import android.graphics.Color; 9 import android.graphics.Paint; 10 import android.graphics.Path; 11 import android.graphics.Paint.Style; 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 beisaierView 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 private int[] x=null,y=null;//赛尔曲线的起点,操作点以及终止点 25 private Path path; 26 int num=0; 27 28 public beisaierView(Context context) { 29 super(context); 30 sfh = this.getHolder();// 实例SurfaceHolder 31 sfh.addCallback(this);// 为SurfaceView添加状态监听 32 x=new int[100]; 33 y=new int[100]; 34 x[0]=0;y[0]=0; 35 path=new Path(); 36 paint = new Paint();//实例一个画笔 37 paint.setColor(Color.WHITE);//设置画笔颜色为白色 38 paint.setStyle(Style.STROKE);//设置画笔为轻敲类型 39 paint.setStrokeWidth(5); 40 paint.setAntiAlias(true);//画笔是否有锯齿 41 setFocusable(true);//设置焦点 42 } 43 44 @Override 45 public void surfaceCreated(SurfaceHolder holder) { 46 flag = true; 47 //实例线程 48 th = new Thread(this); 49 //启动线程 50 th.start(); 51 } 52 53 public void mydraw() { 54 try { 55 canvas = sfh.lockCanvas();//锁定画布 56 if (canvas != null) { 57 canvas.drawRGB(0, 0, 0);//刷屏,或者叫重置画布 58 drawQpath(canvas); 59 } else { 60 Log.i("tt", "获取不到画布");//释放画布 61 } 62 } catch (Exception e) { 63 64 } finally { 65 if (canvas != null) 66 sfh.unlockCanvasAndPost(canvas); 67 } 68 } 69 /** 70 * 绘制贝赛尔曲线 71 * 72 * @param canvas 主画布 73 */ 74 private void drawQpath(Canvas canvas2) { 75 path.reset();// 重置path 76 path.moveTo(x[0], y[0]);// 贝赛尔曲线的起始点 77 for(int i=1;i<num;i++){ 78 path.quadTo(x[i-1], y[i-1], x[i], y[i]); // 设置贝赛尔曲线的操作点以及终止点 79 } 80 canvas.drawPath(path, paint); // 绘制贝赛尔曲线(Path) 81 } 82 83 /** 84 * SurfaceView视图状态发生改变,响应此函数 85 */ 86 @Override 87 public void surfaceChanged(SurfaceHolder holder, int format, int width, 88 int height) { 89 // TODO Auto-generated method stub 90 91 } 92 /** 93 * SurfaceView视图消亡时,响应此函数 94 */ 95 @Override 96 public void surfaceDestroyed(SurfaceHolder holder) { 97 flag = false;//结束游戏,设置线程关闭标志为false 98 99 } 100 /** 101 * 游戏逻辑 102 */ 103 public void logic() { 104 105 }; 106 107 @Override 108 public void run() { 109 while (flag) { 110 long start = System.currentTimeMillis(); 111 mydraw(); 112 logic(); 113 long end = System.currentTimeMillis(); 114 try { 115 if (end - start < 50) { 116 Thread.sleep(50 - (end - start)); 117 } 118 } catch (InterruptedException e) { 119 e.printStackTrace(); 120 } 121 } 122 } 123 //获取点击坐标 124 @Override 125 public boolean onTouchEvent(MotionEvent event) { 126 x[num] = (int) event.getX(); 127 y[num] = (int) event.getY(); 128 num++; 129 return true; 130 } 131 132 }
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 beisaierView(this)); 18 19 } 20 }
谢谢支持“趣淘鼓浪屿”(www.qtgly.com)