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

Android开发 专栏收录该内容
2 篇文章 0 订阅

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

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

   

           先上图:

    

程序运行效果截图

 

 

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

   

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,提交画好的图像
                }
            }
        }
    }
}


  • 1
    点赞
  • 29
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值