SurfaceView双缓冲应用(心电图绘制),避免闪烁

      一般的绘图都是刷屏就行了,留意SurfaceView有两个缓冲(网上对此介绍很多,我就不再赘述),每次重新绘制图形即可。

     我这里的要求是不刷屏,绘图(实时心电图)时,要求能保留画面上次所绘制的图像,在下次绘图时,直接在上次的画面上进行,根据采样率,每秒绘制25~40次。

   

           先上图:

    

程序运行效果截图

 

      下面为参考代码(并非我的源程序),注意代码中缓存的应用:

   

扫描二维码关注公众号,回复: 6065288 查看本文章
package cim.zhanhui.test; 

import java.util.Random; 
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.view.SurfaceHolder;
import android.view.SurfaceView; 
public class MyView extends SurfaceView implements SurfaceHolder.Callback 
{
     private SurfaceHolder holder;
     public MyView(Context context) 
     {
        super(context);
        holder = this.getHolder();//获取holder
        holder.addCallback(this);
    }

     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
    { 
       new Thread(new MyThread()).start();
    }

     public void surfaceCreated(SurfaceHolder holder) 
   {     }

     public void surfaceDestroyed(SurfaceHolder holder)
   {
        // TODO Auto-generated method stub
     }

     class MyThread implements Runnable 
    {
        public void run()
       {
            Paint mPaint = new Paint();
            mPaint.setStyle(Style.FILL);
            mPaint.setAntiAlias(true);
            Random random = new Random();
            Canvas canvas = null;

            Bitmap bak = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
            Canvas bakCanvas = new Canvas(bak);
            int rotate = 0;
            while (rotate < 100) 
           {
                long before = System.currentTimeMillis();
                rotate = rotate + 1;
                try { 
                    canvas = holder.lockCanvas();//获取canvas
                    mPaint.setColor(Color.rgb(random.nextInt(255), random.nextInt(255),random.nextInt(255)));
                    mPaint.setStrokeWidth(2);/*设置paint的外框宽度*/ 
                    Path mPath = new Path();
                    mPath.moveTo(random.nextInt(450), random.nextInt(600));
                    mPath.lineTo(random.nextInt(300), random.nextInt(400));
                    mPath.lineTo(random.nextInt(200), random.nextInt(300));
                    mPath.close();
                    bakCanvas.drawPath(mPath, mPaint);
                    canvas.drawBitmap(bak, 0, 0, null);
                    before = System.currentTimeMillis() - before;
                    if (before < 17) 
                   {
                        Thread.sleep(17 - before); //休眠
                    }
                 } 
                 catch (InterruptedException e) 
                {
                    e.printStackTrace();
                } 
                finally 
               { 
                   holder.unlockCanvasAndPost(canvas); //解锁canvas,提交画好的图像
                }
            }
        }
    }
}


猜你喜欢

转载自blog.csdn.net/ReserveRainbow/article/details/8752116