Surfaceview examples of Android programming _android

Source: Internet
Author: User

This example describes the Surfaceview usage of Android programming. Share to everyone for your reference, specific as follows:

Related knowledge about Surfaceview:

The main differences between view and Surfaceview :

1. View can only be refreshed in the UI thread, while Surfaceview may be refreshed in child threads

2. Surfaceview can control the refresh frequency

Surfaceview Several important methods :

1. After inheriting Surfaceview, call the Getholder () method to obtain the Msurfaceholder object, which can control the rendering of Surfaceview.

2. Implement this Surfaceholder.callback interface and Msurfaceholder.addcallback (this) to add a callback to perceive the Surfaceview lifecycle

3. When drawing Mcanvas.drawcolor (Color.Black); This method is very important, this method is to clean up the last drawing of things, this method must call to see the effect

The implementation effect is as follows:

The first step: new Xrsurfaceview inheritance Surfaceview

Package com.rong.activity;
Import Android.content.Context;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.util.AttributeSet;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceView; /** * Custom Surfaceview * * @author Xu Rong * */public class Xrsurfaceview extends Surfaceview implements Surfaceholder.cal
  Lback, Runnable {//surfaceview wide int surfacewidth;
  Surfaceview High int surfaceheight;
  Surfaceholder object Surfaceholder Msurfaceholder;
  Switch thread's flag bit Boolean isrunning = true;
  Brush Paint mpaint;
  Radius of the circle float radius = 0;
  Whether a circle is larger or smaller is a Boolean status = TRUE;
  The speed of the circle changes int mspeed = 3;
    Public Xrsurfaceview (context, AttributeSet attrs) {Super (context, attrs);
  Initview ();
    private void Initview () {//get Msurfaceholder Msurfaceholder = Getholder ();
  Add callback Msurfaceholder.addcallback (this); @Override public void surfacecreated (SurfaceholderHolder) {isrunning = true;
    Initializes the brush mpaint = new Paint ();
    Mpaint.setantialias (TRUE);
    Mpaint.setcolor (Color.Blue);
  Opens the Draw thread new thread (this). Start ();
    @Override public void surfacechanged (surfaceholder holder, int format, int width, int height) {//Get surface width
    Surfacewidth = width;
  Get the high surfaceheight = height of surface;
  @Override public void surfacedestroyed (Surfaceholder holder) {//close drawing thread isrunning = false;
    @Override public void Run () {Canvas mcanvas = null;
        while (isrunning) {try {//Lock Canva to draw Mcanvas = Msurfaceholder.lockcanvas (null);
        This method is very important, which is equivalent to redrawing (must call or not see the effect) Mcanvas.drawcolor (Color.Black);
        Draw Round Mcanvas.drawcircle ((SURFACEWIDTH/2), (SURFACEHEIGHT/2), radius, mpaint);
          Change radius variable if (status) {radius = radius + mspeed;
          if (Radius >) {status = FALSE; } else{radius = radius-mspeed;
          if (RADIUS < 0) {status = TRUE;
      A catch (Exception e) {e.printstacktrace ());
      finally {//unlock canvas lock Msurfaceholder.unlockcanvasandpost (Mcanvas);

 }
    }
  }
}

Step Two: Create a new layout file Activity_main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <relativelayout xmlns:android=
"http://schemas.android.com/apk" /res/android "
  android:layout_width=" match_parent "
  android:layout_height=" Match_parent "
  android: Background= "#ffffff"
  android:orientation= "vertical" >
  <com.rong.activity.xrsurfaceview
    Android:layout_width= "Match_parent"
    android:layout_height= "match_parent"
    android:layout_centerinparent = "true"
    android:orientation= "vertical"/>
</RelativeLayout>

Run!

More interested readers of Android-related content can view the site's topics: Introduction to Android Development and advanced tutorials, the summary of Android basic components usage, the overview of Android View View techniques, the Android Layout layout skills Summary, and A summary of the usage of Android controls

I hope this article will help you with the Android program.

Related Article

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.