Android: Use Surfaceview to draw stock slip line to solve delay problem

Source: Internet
Author: User

1. Background information

The most recent project is to draw a stock chart and draw an indicator line (Indicator) that follows the finger slide to see the stock price and date accurately. As shown in the following:

The white line in it is the indicator line of the stock, which is used to follow the finger to determine the stock's time and stock price accurately. Whether you're drawing a stock chart or drawing an indicator line, the first thing we think about is using a custom view from Android. It has been proved that the use of view can be a good way to achieve static picture, but the use of dynamic image rendering, often there will be a delay phenomenon. such as the indicator line, the actual implementation of the view class, follow the finger movement, the indicator line will be delayed phenomenon, seriously affect the user experience, it is natural to use Surfaceview to improve performance, improve the smoothness of sliding.

2.Android View vs. Surfaceview

The following examples illustrate the difference between the two:

View Surfaceview

The screen can be updated only in the UI main thread and in the new standalone thread.

The double buffering mechanism of the matchless buffering mechanism, the speed is fast

There are other differences I hope you will add.

3. Case studies

The following is the program implementation of the main code, the data is hidden away from the population.

 PackageCom.devin;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.graphics.PixelFormat;ImportAndroid.graphics.PorterDuff.Mode;Importandroid.view.MotionEvent;ImportAndroid.view.SurfaceHolder;ImportAndroid.view.SurfaceHolder.Callback;ImportAndroid.view.SurfaceView; Public classMystockindicatorviewextendsSurfaceviewImplementsCallback {PrivateSurfaceholder Surfaceholder; PrivatePaint paint; Private floatCurrentX; PublicMystockindicatorview (Context context) {Super(context); //Initialize SurfaceholderSurfaceholder = This. Getholder (); Surfaceholder.addcallback ( This); //make the entire interface transparentSurfaceholder.setformat (pixelformat.transparent); Setzorderontop (true); //Initializing brushesPaint =NewPaint (); Paint.setantialias (true);    Paint.setcolor (Color.White); //The settings screen can be clickedSetfocusable (true); }//Touch-screen events, change the coordinate values after each response to the event, and then redraw Public Booleanontouchevent (Motionevent event) {intEventaction =event.getaction (); intx = (int) Event.getx (); Switch(eventaction) { CaseMotionEvent.ACTION_DOWN:currentX=x;            Paintindicator ();  Break;  CaseMotionEvent.ACTION_UP:clearCavas ();  Break;  CaseMotionEvent.ACTION_MOVE:currentX;            Paintindicator ();  Break;  CaseMotionEvent.ACTION_CANCEL:clearCavas ();  Break; }        return true; }//Draw a line    Private voidpaintindicator () {Canvas Canvas=Surfaceholder.lockcanvas (); //The following two sentences are used to change the origin and convert the default coordinate system to a Cartesian coordinate system.//canvas.translate (Chartleft, GetHeight ()); //Canvas.scale (1,-1);Canvas.drawline (CurrentX, 0, CurrentX, GetHeight (), paint);    Surfaceholder.unlockcanvasandpost (canvas); }//Clear Screen    Private voidClearcavas () {//to lock the canvas before each drawingCanvas Canvas =Surfaceholder.lockcanvas ();    Canvas.drawcolor (Color.transparent, mode.clear); //unlock Canvas When drawing is completesurfaceholder.unlockcanvasandpost (canvas); }    Public voidSurfacechanged (Surfaceholder Surfaceholder,intFormatintWidthintheight) {    }     Public voidsurfacecreated (Surfaceholder surfaceholder) {//Initialize the data hereInitData (); }     Public voidsurfacedestroyed (Surfaceholder holder) {}}

Comments are available in the key areas of the above code. For the implementation of Surfaceview, we need to inherit the Surfaceview and implement the callback interface, and we need to implement three methods: Surfacecreated, Surfacedestroyed, surfacechanged method, Represents the method that is executed when the Surfaceview is created, destroyed, and changed by the interface. In the constructor, to initialize the Surfaceholder, lock the canvas before each drawing, and then unlock the canvas after the drawing is complete.

Android: Use Surfaceview to draw stock slip line to solve delay problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.