zoukankan      html  css  js  c++  java
  • 双缓冲技术解决方案之二:内容不交叉时,可以增量绘制

    package com.loaderman.customviewdemo;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    
    public class RectRefreshTextView extends SurfaceView {
        private Paint mPaint;
    
        public RectRefreshTextView(Context context) {
            super(context);
            init();
        }
    
        public RectRefreshTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public RectRefreshTextView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        private void init() {
            mPaint = new Paint();
            mPaint.setColor(Color.RED);
            mPaint.setTextSize(30);
    
            getHolder().addCallback(new SurfaceHolder.Callback() {
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
    
                    drawText(holder);
                }
    
                @Override
                public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    
                }
    
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
    
                }
            });
        }
    
    
    private void drawText(final SurfaceHolder holder) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //先进行清屏操作
                while (true) {
                    Rect dirtyRect = new Rect(0, 0, 1, 1);
                    Canvas canvas = holder.lockCanvas(dirtyRect);
                    Rect canvasRect = canvas.getClipBounds();
                    if (getWidth() == canvasRect.width() && getHeight() == canvasRect.height()) {
                        canvas.drawColor(Color.BLACK);
                        holder.unlockCanvasAndPost(canvas);
                    } else {
                        holder.unlockCanvasAndPost(canvas);
                        break;
                    }
                }
    
                //画图
                for (int i = 0; i < 10; i++) {
                    int itemWidth = 50;
                    int itemHeight = 50;
                    Rect rect = new Rect(i*itemWidth,0,(i+1)*itemWidth-10,itemHeight);
                    Canvas canvas = holder.lockCanvas(rect);
                    if (canvas != null) {
                        canvas.drawColor(Color.GRAY);
                        canvas.drawText(i + "", i*itemWidth+10, itemHeight/2, mPaint);
                    }
                    holder.unlockCanvasAndPost(canvas);
    
                    try {
                        Thread.sleep(800);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    
    }

    效果:

  • 相关阅读:
    简单批处理语法结构
    简单批处理常用命令
    简单批处理符号简介
    简单批处理内部命令
    jQuery操作DOM
    jQuery中的事件与动画
    jQuery选择器
    初始面向对象
    初识jQuery
    操作DOM
  • 原文地址:https://www.cnblogs.com/loaderman/p/10232259.html
Copyright © 2011-2022 走看看