Surfaceview component usage resolution in Android

Source: Internet
Author: User

Surfaceview components can be used to create high-efficiency drawing of two-dimensional graphs or display images, which are often in game development. In Android, the Surfaceview component is already available. When used, a custom Surfaceview is generally implemented by means of an inherited method, It can also be implemented in Mainactivity via interface Surfaceholder.callback interface, where Surfaceview drawing can be achieved through an interface, of course, the drawing may be a static diagram (only once in the specified area), or it can be a dynamic graph (specified area- > Draw-and-cycle).

Surfaceview component Usage Flow: through Findviewbyid () instantiate Surfaceview component, get instantiated object, use Getholder () method to get Surfaceviewholder class object, Add a callback (context context) callback for the object, and in the Surfacecreate () method, add the initialization code that Surfaceview just displayed, which is executed when the Surfaceview component is instantiated. such as can initialize the background color, the plot area displays the shape, the coordinate and so on; the Surfacechanged () method is mainly called when the Surfaceview object size and shape change, and the Surfacedestroyed () method is called when the Surfaceview is destroyed. , typically adding a kill code.

On the Surfaceview component display area, implementing a two-dimensional drawing requires three classes: paint, canvas, and Surfaceholder class. Where the paint class refers to a brush, you can set the color of the brush (similar to a red or blue refill), the line width of the brush display (similar to 0.35mm or 0.5mm), etc., the canvas class is equivalent to a canvas, which passes through the Lockcanvas () in the Surfaceholder class. Method, the Lockcanvas () method can have parameters that specify the area on which the canvas is locked (that is, the area of the canvas lock is not necessarily the entire area defined by the Surfaceview, or a portion of the area specified by the parameter), and when the area is locked, You can draw points, lines, circles, rectangles, images, and so on, through the methods provided by the Canvas class instance object, and the parameters in these methods typically have a paint class object.

One thing to note is that the code to draw graphics or images in the lock canvas area is usually done on a separate thread that can be opened in the Surfacecreate () method.

Alas, the picture does not add up, can only paste code.

package com.main.dfa_surfaceview; import android.support.v7.app.actionbaractivity;import  android.graphics.canvas;import android.graphics.color;import android.graphics.paint;import  android.os.bundle;import android.view.surfaceholder;import android.view.surfaceview;import  android.view.view;import android.view.view.onclicklistener;import android.widget.button;import  android.widget.toast; public class mainactivity extends actionbaractivity  implements surfaceholder.callback {    button btn1=null;     SurfaceView sfc=null;    SurfaceHolder holder=null;     paint paint_one=null;    paint paint_two=null;    @ Override    protected void oncreate (bundle savedinstancestate)  {       &nbsP; super.oncreate (savedinstancestate);         setcontentview ( R.layout.activity_main);                  btn1= (Button) Findviewbyid (R.ID.BTN1);         sfc= ( Surfaceview) Findviewbyid (r.id.surfaceview1);         holder= Sfc.getholder ();  //get Surfaceview holder, similar to Surfaceview controller          holder.addcallback (This);                 paint_one=new paint ();  //Get Brush Instances          Paint_two=new paint ();         paint_one.setcolor (Color.BLUE);  //Brush Color         paint_two.setcolor (color.green);         paint_one.seTstrokewidth ( //); Set the line width         paint_two.setstrokewidth (10);          btn1.setonclicklistener (New View.OnClickListener ()  {             @Override              public void onclick (View arg0)  {                 // todo  Auto-generated method stub                 toast.maketext (mainactivity.this,  "Asdgdsgas",  Toast.LENGTH_SHORT). Show ();                 Mythread mthread=new mythread ();                 mthread.sTart ();            }         });           }      @Override     public void surfacechanged (surfaceholder arg0, int  ARG1, INT ARG2, INT ARG3)  {        //  TODO Auto-generated method stub             }     @Override     public void  Surfacecreated (surfaceholder arg0)  {        // todo  auto-generated method stub        // Execute         init_background () when Surfaceview is created;//Initialize Drawing interface      }     @Override   &Nbsp; public void surfacedestroyed (surfaceholder arg0)  {         // TODO Auto-generated method stub             }        private void  init_background () {        canvas canvas=null;         canvas=holder.lockcanvas ();//Get Target paint area          canvas.drawcolor (color.red);//canvas background color             holder.unlockcanvasandpost (canvas);  //Unlock and display      }          private class MyThread extends Thread{                 public void run ( ) {            canvas canvas=null;             canvas=holder.lockcanvas ();//Gets the target paint area, no parameter indicates that all plot areas are locked              canvas.drawcolor (color.red);//canvas background color settings              canvas.drawcircle (100, 100, 50, paint_one);//To ( 100,100) for the center, draw a circle with a radius of 50 pixels              Canvas.drawcircle (50, 50, 50, paint_two);             holder.unlockcanvasandpost (canvas);  //Unlock and display           }       }}



Surfaceview component usage resolution in Android

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.