利用SurfaceView顯示正弦曲線,仿造示波器,surfaceview正弦

來源:互聯網
上載者:User

利用SurfaceView顯示正弦曲線,仿造示波器,surfaceview正弦

 

 

眾所周知,view是通過重新整理來重繪視圖的,Android系統通過發出VSYNC訊號來進行螢幕重繪,重新整理的時間間隔為16ms,如果在16ms內view完成你所需要的所有操作,那麼使用者在視覺上就不會產生卡頓的感覺;而如果執行的操作邏輯太多,特別是需要頻繁重新整理的介面,就會不斷阻塞主線程,從而導致畫面卡頓。

因此Android提供了surfaceView。

1.View主要適用於主動更新的情況,surfaceView主要適用於被動更新,例如頻繁的重新整理。

2.View在主線程 中對View進行重新整理,surfaceView通常會用一個子線程來進行頁面的重新整理。

3.View在繪圖時沒有雙緩衝機制,而surfaceView在底層就已經實現了雙緩衝機制。

因此如果自訂view需要頻繁重新整理或者重新整理時候的資料處理量比較大,那麼就可以考慮使用surfaceView來代替View

使用SurfaceeView有一套模板,以下用一個例子說明:用surfaceView做出示波器的效果,畫出正弦波。

package com.example.tangzh.MyView;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.util.AttributeSet;import android.view.SurfaceHolder;import android.view.SurfaceView;import com.example.tangzh.mylearn.R;/** * Created by TangZH on 2017/4/30. */public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback,Runnable //繼承並實現兩個介面{    private SurfaceHolder mHolder;    //用於繪圖的Canvas    private Canvas mCanvas;    //子線程標誌位    private boolean mIsDrawing;    //畫筆    private Paint mPaint;    private Path mPath;    //x座標    private int x=0;    //y座標    private int y=400;    public MySurfaceView(Context context, AttributeSet attrs, int defStyleAttr)    {        super(context, attrs, defStyleAttr);        initView();    }    public MySurfaceView(Context context)    {        super(context);        initView();    }    public MySurfaceView(Context context, AttributeSet attrs)    {        super(context, attrs);        initView();    }    private void initView()    {        mHolder=getHolder();        mHolder.addCallback(this);        setFocusable(true);        setFocusableInTouchMode(true);        this.setKeepScreenOn(true);    }    @Override    public void surfaceCreated(SurfaceHolder surfaceHolder) {        mIsDrawing=true;        mPath=new Path();        mPath.moveTo(0,400);        mPaint=new Paint();        mPaint.setColor(getResources().getColor(R.color.colorTheme));        mPaint.setStyle(Paint.Style.STROKE);        mPaint.setStrokeWidth(5);        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)        {            draw();            x+=5;            y=(int)(100* Math.sin(x*2*Math.PI/180)+400);            mPath.lineTo(x,y);        }    }    private void draw() {        try {            mCanvas=mHolder.lockCanvas();            //SurfaceView背景            mCanvas.drawColor(Color.WHITE);            mCanvas.drawPath(mPath,mPaint);        }catch (Exception e)        {            e.printStackTrace();        }finally {            if(mCanvas!=null)                mHolder.unlockCanvasAndPost(mCanvas); //對畫布內容進行提交        }    }}


要注意,通過SurfaceView對象的lockCanvas()方法,就可以擷取當前的Canvas繪圖對象,這個對象跟上次的Canvas對象是同一個,因此之前的繪圖操作都會被保留,如果需要擦出,則可以在繪製前,通過drawColor()方法來進行清屏操作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.