canvas教程

Android绘图机制和处理技巧(2)

字号+ 作者:H5之家 来源:H5之家 2017-05-03 17:05 我要评论( )

restoreCount()将图层出栈 入栈时,后面所有的操作都会发生在这个图层中 出栈时,则会把图像绘制到上层Canvas上 @Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.drawColor(Color.WHI

restoreCount() 将图层出栈

  • 入栈时,后面所有的操作都会发生在这个图层中
  • 出栈时,则会把图像绘制到上层Canvas上
  • @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE); p.setColor(Color.BLUE); canvas.drawCircle(150, 150, 100, p); // 图一 canvas.saveLayerAlpha(0, 0, 400, 400, 127, LAYER_TYPE_NONE); // 图二 // canvas.saveLayerAlpha(0, 0, 400, 400, 255, LAYER_TYPE_NONE); p.setColor(Color.RED); canvas.drawCircle(200, 200, 100, p); canvas.restore(); }


    图一,透明度为127时,为半透明状态


    图二,透明度设置为255时,完全不透明

    Android图像处理技巧 色彩矩阵分析

    // ---- 施工中(8月2日--6日) ----

    SurfaceView的使用
  • SurfaceView是为了解决View中执行操作逻辑太多出现卡顿的情况
  • SurfaceView和View的区别 SurfaceView的使用
  • 创建SurfaceView
  • @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { } @Override public void run() { }
  • 初始化SurfaceView
  • private SurfaceHolder mHolder; private Canvas mCanvas; private boolean mIsDrawing; private void init() { mHolder = getHolder(); mHolder.addCallback(this); }
  • 使用SurfaceView
  • API 作用

    lockCanvas() 获得当前Canvas对象

    drawColor() 清屏

    public class SurfaceViewDemo extends SurfaceView implements Runnable, SurfaceHolder.Callback { private SurfaceHolder mHolder; private Canvas mCanvas; private boolean mIsDrawing; // 子线程标志位 public SurfaceViewDemo(Context context) { super(context); initView(); } private void initView() { mHolder = getHolder(); mHolder.addCallback(this); setFocusable(true); setFocusableInTouchMode(true); this.setKeepScreenOn(true); // mHolder.setFormat(PixelFormat.OPAQUE); } @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { mIsDrawing = true; new Thread(this).start(); } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { mIsDrawing = false; } @Override public void run() { while (mIsDrawing) { draws(); } } public void draws() { try { mCanvas = mHolder.lockCanvas(); } catch (Exception e) { } finally { if (mCanvas != null) { mHolder.unlockCanvasAndPost(mCanvas); } } } } SurfaceView实例 1. 正选曲线 @Override public void run() { while (mIsDrawing) { draws(); x += 1; y = (int) (100 * Math.sin(x * 2 * Math.PI/180) + 400); mPath.lineTo(x, y); } } private void draws() { try { mCanvas = mHolder.lockCanvas(); // Surface背景 mCanvas.drawColor(Color.WHITE); mCanvas.drawPath(mPath, mPaint); } catch (Exception e) { } finally { if (mCanvas != null) { mHolder.unlockCanvasAndPost(mCanvas); } } }

     

    1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • Canvas与ValueAnimator

      Canvas与ValueAnimator

      2017-04-28 18:00

    • Android Bitmap和Canvas学习笔记(转)

      Android Bitmap和Canvas学习笔记(转)

      2017-04-28 17:00

    • HTML5 Canvas 绘图实例教程

      HTML5 Canvas 绘图实例教程

      2017-04-28 13:09

    • 21天学习android开发教程之SurfaceView与多线程的混搭

      21天学习android开发教程之SurfaceView与多线程的混搭

      2017-04-27 12:00

    网友点评