Android SurfaceView (3)

Source: Internet
Author: User

Example of SurfaceView oscilloscope: directly add the code

The program automatically draws the sine wave and Cosine waveform on the Screen Based on the button clicked. Each time the program draws, it only needs to draw (update) the waveform of the current point. The previously drawn waveform does not need to be updated. The lockCanvas (Rect r) method of SurfaceHolder is used.

1. Layout file Layout/show_wave.xml:

 

[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"

Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">

<Button
Android: id = "@ + id/sin"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "sine curve"
/>
<Button
Android: id = "@ + id/cos"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "cosine curve"
/>
<SurfaceView
Android: id = "@ + id/show"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
</LinearLayout>

2. Main Interface Activity: ShowWave

[Java]
Package com. infy. configuration;
 
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. view. SurfaceHolder;
Import android. view. SurfaceView;
Import android. view. View;
Import android. view. SurfaceHolder. Callback;
Import android. view. View. OnClickListener;
Import android. widget. Button;
 
Public class ShowWave extends Activity {
 
Private SurfaceHolder holder;
Private Paint paint;
Final int HEIGHT = 320;
Final int WIDTH = 320;
Final int X_OFFSET = 5;
Private int cx = X_OFFSET;
// The actual position of the Y axis
Int centerY = HEIGHT/2;
Timer timer = new Timer ();
TimerTask task = null;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );

SetContentView (R. layout. show_wave );

Final SurfaceView surface = (SurfaceView) findViewById (R. id. show );
// Initialize the SurfaceHolder object www.2cto.com
Holder = surface. getHolder ();
Paint = new Paint ();
Paint. setColor (Color. GREEN );
Paint. setStrokeWidth (3 );

Button sin = (Button) findViewById (R. id. sin );
Button cos = (Button) findViewById (R. id. cos );
OnClickListener listener = (new OnClickListener (){

@ Override
Public void onClick (final View source ){
// TODO Auto-generated method stub
DrawBack (holder );
Cx = X_OFFSET;
If (task! = Null ){
Task. cancel ();
}

Task = new TimerTask (){

@ Override
Public void run (){
Int cy = source. getId () = R. id. sin? CenterY-(int) (100 * Math. sin (cx-5) * 2 * Math. PI/150 )):
CenterY-(int) (100 * Math. cos (cx-5) * 2 * Math. PI/150 ));
Canvas canvas = holder. lockCanvas (new Rect (cx, cy-2, cx + 2, cy + 2 ));
Canvas. drawPoint (cx, cy, paint );
Cx ++;

If (cx> WIDTH ){

Task. cancel ();
Task = null;

}
Holder. unlockCanvasAndPost (canvas );
}
};
Timer. schedule (task, 0, 30 );

}
});

Sin. setOnClickListener (listener );
Cos. setOnClickListener (listener );
Holder. addCallback (new Callback (){
Public void surfaceChanged (SurfaceHolder holder, int format, int width, int height ){
DrawBack (holder );
}
 
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
// TODO Auto-generated method stub
}
 
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){
// TODO Auto-generated method stub
Timer. cancel ();
}

});


}


Private void drawBack (SurfaceHolder holder ){
Canvas canvas = holder. lockCanvas ();
// Draw a white background
Canvas. drawColor (Color. WHITE );
Paint p = new Paint ();
P. setColor (Color. BLACK );
P. setStrokeWidth (2 );

// Draw the coordinate axis
Canvas. drawLine (X_OFFSET, centerY, WIDTH, centerY, p );
Canvas. drawLine (X_OFFSET, 40, X_OFFSET, HEIGHT, p );
Holder. unlockCanvasAndPost (canvas );
Holder. lockCanvas (new Rect (0, 0, 0 ));
Holder. unlockCanvasAndPost (canvas );

}


}
3. The final result is displayed:


A. Sine Curve

 
B. cosine curve:

 




From Damon_tong's column

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.