public void onSensorChanged(String sensorStr) { Log.v(TAG, "onSensorChanged : " + sensorStr); String[] fs = sensorStr.split(","); if (fs.length == 4) { acceValusW = Float.parseFloat(fs[0]) * (BASE - 10); // 获得x轴的值 acceValusX = Float.parseFloat(fs[1]) * (BASE - 10); // 获得y轴的值 acceValusY = Float.parseFloat(fs[2]) * (BASE - 10); // 获得z轴的值 acceValusZ = Float.parseFloat(fs[3]) * (BASE - 10); // 锁定整个SurfaceView Canvas mCanvas = mSurfaceHolder.lockCanvas(); try { if (mCanvas != null) { // 画笔的颜色(红) mPaint.setColor(Color.RED); // 画X轴的点 mCanvas.drawPoint(x, (int) (BASE + acceValusX), mPaint); // 画笔的颜色(绿) mPaint.setColor(Color.GREEN); // 画Y轴的点 mCanvas.drawPoint(x, (int) (BASE * 2 + acceValusY), mPaint); // 画笔的颜色(蓝) mPaint.setColor(Color.CYAN); // 画Z轴的点 mCanvas.drawPoint(x, (int) (BASE * 3 + acceValusZ), mPaint); // 画笔的颜色(huang) mPaint.setColor(Color.WHITE); // 画W轴的点 mCanvas.drawPoint(x, (int) (BASE * 4 + acceValusW), mPaint); // 横坐标+1 x++; // 如果已经画到了屏幕的最右边 // if (x > getWindowManager().getDefaultDisplay().getWidth()) { if (x > mCanvas.getWidth()) { x = 0; // 清屏 mCanvas.drawColor(Color.BLACK); } // 绘制完成,提交修改 mSurfaceHolder.unlockCanvasAndPost(mCanvas); } } catch (Exception e) { e.printStackTrace(); } finally { if (mCanvas != null) { // 重新锁一次 mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0)); mSurfaceHolder.unlockCanvasAndPost(mCanvas); } } } }
void myDraw() { float width, height; width = getMeasuredWidth(); height = getMeasuredHeight(); canvas = holder.lockCanvas(); canvas.drawColor(Color.GREEN); canvas.drawPath(path, paint); holder.unlockCanvasAndPost(canvas); }
public void mDraw() { mCanvas = mHolder.lockCanvas(); // 获得画布对象,开始对画布画画 mCanvas.drawColor(++i); // 设置画布颜色为黑色 canvasMethod(mCanvas); // 调用自定义的方法,主要是在传进去的画布对象上画画 mHolder.unlockCanvasAndPost(mCanvas); // 把画布显示在屏幕上 }
@Override public void run() { while (running) { Canvas c = null; try { c = surfaceHolder.lockCanvas(null); synchronized (surfaceHolder) { if (!game.onEnterFrame(c)) { running = false; game.shutdown(true); view.shutdown(); } } } finally { // do this in a finally so that if an exception is thrown // during the above, we don't leave the Surface in an // inconsistent state if (c != null) { surfaceHolder.unlockCanvasAndPost(c); } } } }