Use SurfaceView to display the sine curve, simulate the oscilloscope, and surfaceview the sine.
As we all know, views are repainted through refresh. The Android system re-painted the screen by sending a VSYNC signal. The interval for refresh is 16 ms, if the view completes all the operations you need within 16 ms, the user will not feel stuck visually. If there are too many operation logics to be executed, especially the interface that needs to be refreshed frequently, will continue to block the main thread, resulting in choppy pictures.
Therefore, Android provides surfaceView.
1. View is mainly applicable to active updates, and surfaceView is mainly applicable to passive updates, such as frequent refreshes.
2. The View is refreshed in the main thread. surfaceView usually uses a subthread to refresh the page.
3. There is no dual buffering mechanism for the View during plotting, and the surfaceView has implemented a dual buffering mechanism at the underlying layer.
Therefore, if the custom view needs to be refreshed frequently or the data processing volume is large during refresh, you can use surfaceView instead of View.
SurfaceeView has a set of templates. The following example shows how to use surfaceView to create an oscilloscope and draw a sine wave.
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. */public class MySurfaceView extends Surfac EView implements SurfaceHolder. callback, Runnable // inherit and implement two interfaces {private SurfaceHolder mHolder; // Canvas private Canvas mCanvas used for drawing; // The private boolean mIsDrawing of the sub-thread flag; // brush private Paint mPaint; private Path mPath; // x coordinate private int x = 0; // y coordinate private int y = 400; public MySurfaceView (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); initView ();} public M YSurfaceView (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; m Path = 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 ;} @ Overr Ide 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 background mCanvas. drawColor (Color. WHITE); mCanvas. drawPath (mPath, mPaint);} catch (Exception e) {e. printStackTrace ();} finally {if (mCanvas! = Null) mHolder. unlockCanvasAndPost (mCanvas); // submit the canvas content }}}
Note that the current Canvas drawing object can be obtained through the lockCanvas () method of the SurfaceView object. This object is the same as the last Canvas object, therefore, previous drawing operations will be retained. If you need to erase the image, you can use the drawColor () method to clear the screen before painting.