You can use custom controls (such as buttons) to customize a SurfaceView.
For example, a dynamic graph (custom) or a toolbar similar to that in the UC browser.
Example:
Custom class code:
Copy codeThe Code is as follows: public class ImageSurfaceView extends SurfaceView implements Callback {
// Used to control SurfaceView
Private SurfaceHolder sfh;
Private Handler handler = new Handler ();
Private ImageRunnable imageRunnable = new ImageRunnable ();
Private Paint paint;
Private Canvas canvas;
Private Matrix matrix;
/** Coordinates of the image */
Private float imageX, imageY;
/** Obtained image */
Private Bitmap bmp;
/** Image width/height */
Private float bmp w, BMP h;
/** Screen size */
Private int screenW, screenH;
/**
* SurfaceView initialization Function
*/
Public ImageSurfaceView (Context context, AttributeSet attrs ){
Super (context, attrs );
Sfh = this. getHolder ();
Sfh. addCallback (this );
Paint = new Paint ();
Paint. setColor (Color. WHITE );
Paint. setAntiAlias (true );
SetFocusable (true );
}
/**
* SurfaceView is created in response to this function
*/
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
System. out. println ("ImageSurfaceView is surfaceCreated ");
ScreenH = this. getHeight ();
ScreenW = this. getWidth ();
Handler. post (imageRunnable );
}
/**
* Game plotting
*/
Public void draw (){
Try {
Canvas = sfh. lockCanvas ();
Canvas. drawRGB (0, 0, 0 );
Canvas. save ();
// Draw
Canvas. drawBitmap (bmp, matrix, paint );
System. out. println ("have you drawn the image? ");
Canvas. restore ();
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
If (canvas! = Null)
Sfh. unlockCanvasAndPost (canvas );
}
}
/**
* Touch screen event monitoring
*/
@ Override
Public boolean onTouchEvent (MotionEvent event ){
Return true;
}
/**
* Image thread running
*/
Class ImageRunnable implements Runnable {
@ Override
Public void run (){
Long start = System. currentTimeMillis ();
Draw ();
Long end = System. currentTimeMillis ();
If (end-start <500 ){
Handler. postDelayed (this, 200-(end-start ));
} Else {
Handler. post (this );
}
}
}
/**
* The SurfaceView status changes and responds to this function.
*/
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width, int height ){
System. out. println ("ImageSurfaceView is surfaceChanged ");
}
/**
* Response to this function when SurfaceView disappears
*/
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){
System. out. println ("ImageSurfaceView is surfaceDestroyed ");
}
}
The xml Code of layout is as follows (the full address of the class is used as the control name ):Copy codeThe Code is as follows: <FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Akai. test. getImage. ImageSurfaceView android: id = "@ + id/myImageView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
/>
<LinearLayout android: id = "@ + id/buttons"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal"
Android: background = "@ android: color/white"
>
<Button android: id = "@ + id/getImage"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "select image"
/>
<Button android: id = "@ + id/getImage_ OK"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "OK"
/>
<Button android: id = "@ + id/getImage_cancle"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "cancel"
/>
</LinearLayout>
</FrameLayout>
The above code is for reference only!
Note the following questions::
1. parameters need to be added to the initialization function of this class: public ImageSurfaceView (Context context, AttributeSet attrs );
2. do not obtain the screen width and height during initialization. during initialization, SurfaceCreated is not executed. Therefore, the width and height must be after surfaceCreated or before surfaceDestroyed;
3. When this control is displayed, surfaceCreated and surfaceChanged will be executed. When you jump to another interface, surfaceDestroyed will be executed (whether or not the current activity has been destroyed ), therefore, if you execute sfh immediately when you jump back to the secondary control. lockCanvas () is Null.