Simple example of SurfaceView usage in Android
- 2020-09-28 09:08:07
- OfStack
This article gives an example of SurfaceView usage in Android. To share for your reference, the details are as follows:
Here is a small program that USES SurfaceView to draw a circle on the screen. You can change the position of the circle by pressing the arrow key and touching the screen
Code:
Activity:
package com.view;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hidden status bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// the Activity Remove the title of
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set the layout
this.setContentView(new MySurfaceView(this));
}
}
SurfaceView:
package com.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
public class MySurfaceView extends SurfaceView implements Runnable, Callback {
private SurfaceHolder mHolder; // Used to control the SurfaceView
private Thread t; // The statement 1 A thread
private boolean flag; // A thread running identity used to control the thread
private Canvas mCanvas; // The statement 1 A canvas
private Paint p; // The statement 1 The brush
private int x = 50, y = 50, r = 10; // The coordinates and the radius of the circle
public MySurfaceView(Context context) {
super(context);
mHolder = getHolder(); // To obtain SurfaceHolder object
mHolder.addCallback(this); // for SurfaceView Add status monitoring
p = new Paint(); // create 1 Brush objects
p.setColor(Color.WHITE); // Set the color of the brush to white
setFocusable(true); // Set the focus
}
/**
* The custom 1 Five ways, paint on the canvas 1 A round
*/
public void Draw() {
mCanvas = mHolder.lockCanvas(); // Get the canvas object and start painting on the canvas
mCanvas.drawRGB(0, 0, 0); // Fill the canvas with black
mCanvas.drawCircle(x, y, r, p); // draw 1 A round
mHolder.unlockCanvasAndPost(mCanvas); // Finish drawing and display the canvas on the screen
}
/**
* when SurfaceView This function is called when it is created
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
t = new Thread(this); // create 1 Thread objects
flag = true; // Set the thread running identity to true
t.start(); // Starting a thread
}
/**
* when SurfaceView This function is called when the view of
*/
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
/**
* when SurfaceView When destroyed, this function is called
*/
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
flag = false; // Set the thread running identity to false
}
/**
* Called when the screen is touched
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
x = (int) event.getX(); // Gets the corresponding screen when touched X Axis coordinates
y = (int) event.getY(); // Gets the corresponding screen when touched Y Axis coordinates
return true;
}
/**
* Called when the user presses a key
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_DPAD_UP){ // When the user clicks the ↑ button
y--; // Set up the Y Axis coordinate reduction 1
}
return super.onKeyDown(keyCode, event);
}
@Override
public void run() {
while (flag) {
Draw(); // Call the custom draw method
try {
Thread.sleep(50); // Let the thread rest 50 ms
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
MySurfaceView first inherited SurfaceView and then implemented the Runnable and Callback interfaces
Rewritten run method of Runnable and surfaceCreated(SurfaceHolder holder), surfaceChanged(SurfaceHolder holder, int format, int width,int height), surfaceDestroyed(SurfaceHolder holder) method of Callback,
Also implemented onTouchEvent(MotionEvent event), onKeyDown(int keyCode, KeyEvent event) method to, detailed in the code has been commented.
I hope this article is helpful for Android programming.