Improve SurfaceView (Part 2) for Android)

Source: Internet
Author: User

The last time we introduced MediaPlayer, we introduced SurfaceView. SurfaceView is a very important drawing container because it can directly obtain image data from hardware interfaces such as memory or DMA, this time I will use two articles to introduce the usage of SurfaceView. There are many usage of SurfaceView on the Internet, and the writing method is also different, such as inheriting SurfaceView class or SurfaceHolder. callback class, which can be selected based on actual functional needs. I will call the lockCanvas and unlockCanvasAndPost of SurfaceHolder on the general user interface.

Let's take a look at the program running:

 

1. demonstrate how to directly draw a sine wave on SurfaceView.

 

Compare the two pictures on the left and right, and use. lockCanvas (null) on the Right, while use. lockCanvas (new Rect (oldX, 0, oldX + length,
GetWindowManager (). getdefadisplay display (). getHeight (), compare the two effects. Because the left image is painted by the specified Rect, the efficiency is higher than that of the full control on the right image, and after the screen is cleared (canvas. drawColor (Color. BLACK.

 

Next, paste the source code of main. xml:

View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Android: orientation = "vertical">

<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<Button android: id = "@ + id/Button01" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "simple painting"> </Button>
<Button android: id = "@ + id/Button02" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "timer painting"> </Button>
</LinearLayout>
<SurfaceView android: id = "@ + id/SurfaceView01"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </SurfaceView>
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"
Android: orientation = "vertical">

<LinearLayout android: id = "@ + id/LinearLayout01"
Android: layout_width = "wrap_content" android: layout_height = "wrap_content">
<Button android: id = "@ + id/Button01" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "simple painting"> </Button>
<Button android: id = "@ + id/Button02" android: layout_width = "wrap_content"
Android: layout_height = "wrap_content" android: text = "timer painting"> </Button>
</LinearLayout>
<SurfaceView android: id = "@ + id/SurfaceView01"
Android: layout_width = "fill_parent" android: layout_height = "fill_parent"> </SurfaceView>
</LinearLayout>
 

Next, paste the program source code:

View plaincopy to clipboardprint?
Package com. testSurfaceView;

Import java. util. Timer;
Import java. util. TimerTask;

Import android. app. Activity;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. graphics. Rect;
Import android. OS. Bundle;
Import android. util. Log;
Import android. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. View;
Import android. widget. Button;

Public class testSurfaceView extends Activity {
/** Called when the activity is first created .*/
Button btnSimpleDraw, btnTimerDraw;
SurfaceView sfv;
SurfaceHolder sfh;

Private Timer mTimer;
Private MyTimerTask mTimerTask;
Int Y_axis [], // Save the point on the Y axis of the sine wave
CenterY, // center line
OldX, oldY, // The Last XY point
CurrentX; // the point on the X-axis currently drawn

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

BtnSimpleDraw = (Button) this. findViewById (R. id. Button01 );
BtnTimerDraw = (Button) this. findViewById (R. id. Button02 );
BtnSimpleDraw. setOnClickListener (new ClickEvent ());
BtnTimerDraw. setOnClickListener (new ClickEvent ());
Sfv = (SurfaceView) this. findViewById (R. id. SurfaceView01 );
Sfh = sfv. getHolder ();

// A timer that dynamically draws a sine wave
MTimer = new Timer ();
MTimerTask = new MyTimerTask ();

// Initialize Y-axis data
CenterY = (getWindowManager (). getdefadisplay display (). getHeight ()-sfv
. GetTop ()/2;
Y_axis = new int [getWindowManager (). getdefadisplay display (). getWidth ()];
For (int I = 1; I <Y_axis.length; I ++) {// calculate the sine wave
Y_axis [I-1] = centerY
-(Int) (100 * Math. sin (I * 2 * Math. PI/180 ));
}
}

Class ClickEvent implements View. OnClickListener {

@ Override
Public void onClick (View v ){

If (v = btnSimpleDraw

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.