Android Notes(SurfaceView)

SurfaceView

  • View适用于主动刷新,SurfaceView适用于被动刷新,例如频繁刷新
  • View在主线程中对画面进行刷新,SurfaceView通常会在子线程进行页面刷新
  • View绘制时没有使用双缓冲机制,SurfaceView在底层机制中实项了双缓冲机制
    正弦曲线,绘画板
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
       private void init() {
    mSurfaceHolder = getHolder();
    mSurfaceHolder.addCallback(this);
    setFocusable(true);
    setFocusableInTouchMode(true);
    setKeepScreenOn(true);
    // mSurfaceHolder.setFormat(PixelFormat.OPAQUE);

    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setColor(Color.RED);
    mPaint.setStyle(Paint.Style.STROKE);
    mPath = new Path();

    mDrawPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mDrawPaint.setColor(Color.RED);
    mDrawPaint.setStyle(Paint.Style.STROKE);
    mDrawPaint.setStrokeCap(Paint.Cap.ROUND);
    mDrawPaint.setStrokeWidth(5);
    mDrawPaint.setStrokeJoin(Paint.Join.ROUND);
    mDrawPath = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    mPath.moveTo(0, getHeight() / 2);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    mIsDrawing = true;
    Log.e("surfaceCreated", Thread.currentThread().getName());
    new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    mIsDrawing = false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
    int x = (int) event.getX();
    int y = (int) event.getY();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    mDrawPath.moveTo(x, y);
    break;
    case MotionEvent.ACTION_MOVE:
    mDrawPath.lineTo(x, y);
    break;
    case MotionEvent.ACTION_UP:
    break;
    }
    return true;
    }

    @Override
    public void run() {
    while (mIsDrawing) {
    long start = System.currentTimeMillis();
    draw();
    long end = System.currentTimeMillis();
    //100是经验值,取值范围50-100ms
    if (end - start < 100) {
    try {
    Thread.sleep(100 - (end - start));
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    x += 1;
    y = (int) (100 * Math.sin(x * 2 * Math.PI / 180) + 100);
    mPath.lineTo(x, y);
    }
    }

    private void draw() {
    try {
    //获得canvas图像
    mCanvas = mSurfaceHolder.lockCanvas();
    //surfaceview 背景
    mCanvas.drawColor(Color.WHITE);
    //绘制路径
    mCanvas.drawPath(mPath, mPaint);
    mCanvas.drawPath(mDrawPath, mDrawPaint);
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    //保证每次内容的提交
    if (mCanvas != null) mSurfaceHolder.unlockCanvasAndPost(mCanvas);
    }
    }
willkernel wechat
关注微信公众号
帅哥美女们,请赐予我力量吧!