Understanding self-made SurfaceView code

Source: Internet
Author: User

Overview:

I recently read the android Game development column of himi, and found that SurfaceView is more suitable for Game Dev through the inheritance interface classes of the previous profiling views. Then I also typed a few times with the himi code. In the end, the himi operation is also achieved. However, many questions have been raised. Now I will release my understanding. Welcome to exchange and study.

 


Code List:


[Java]
Import android. content. Context;
Import android. content. Intent;
Import android. content. res. Resources;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. util. Log;
Import android. view. MotionEvent;
Import android. view. SurfaceHolder;
Import android. view. SurfaceHolder. Callback;
Import android. view. SurfaceView;
 
Public class LoginView extends SurfaceView implements Callback, Runnable {
 
Private Canvas canvas; // defines the Canvas object.
Private Thread thread; // defines the corresponding Thread.
Private int ScreenW, ScreenH; // defines the width and height of the screen.
Private Paint paint; // defines the Paint brush object.
Private SurfaceHolder sh; // declare a SurfaceHolder object
Private Resources resources; // declare a resource object
Private Bitmap bitmapr; // register bitmap
Private Bitmap bitmapb; // background bitmap
Private int bp_x, bp_y; // specifies the width and height of the registered background image?
Private boolean flag;
Public static String zh_k = "registration page ";
Private boolean zh_flag = true;
Private String str_pass = "aaa ";
// Initial structure?
Public LoginView (Context context ){
Super (context );
//
Resources = this. getResources ();
Bitmapr = BitmapFactory. decodeResource (resources, R. drawable. register); // registration window
Bitmapb = BitmapFactory. decodeResource (resources, R. drawable. duola); // register the background?
Thread = new Thread (this );
Sh = this. getHolder ();
Sh. addCallback (this );
Paint = new Paint ();
Paint. setColor (Color. RED );
Paint. setAntiAlias (true); // remove the Sawtooth
This. setFocusable (true );
This. setKeepScreenOn (true); // set the screen to always be on
Resources = this. getResources ();

}
Public void draw (){
Canvas = sh. lockCanvas (); // obtain the canvas object.
Paint. setColor (Color. RED );
If (null! = Canvas) {// The coordinate algorithm is as follows when the landscape screen is set
Canvas. drawColor (Color. WHITE );
Canvas. drawBitmap (bitmapb,
-(Bitmapb. getWidth ()-ScreenW),-(bitmapb
. GetHeight ()-ScreenH), paint );
// Parameters 2 and 3 are x and y coordinates, which are the upper left corner of the bitmap painting.
Canvas. drawBitmap (bitmapr, bp_x, bp_y, paint );
Sh. unlockCanvasAndPost (canvas );
}
}
@ Override
Public void run (){
While (! Flag ){
Draw ();
Try {
Thread. sleep (1000 );
} Catch (Exception e ){
Log. v ("run", "error ");
}
}
}
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
ScreenW = this. getWidth ();
ScreenH = this. getHeight ();
Bp_x = ScreenW/2-bitmapr.getWidth ()/2;
Bp_y = ScreenH/2-bitmapr.getHeight ()/2;
Thread. start ();
}
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){

}
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){

}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
Float x = event. getX ();
Float y = event. getY ();

If (x> bp_x + 14 & x <bp_x + 129) {// lock the x axis of the logon box name Region
If (y> = bp_y + 43 & y <= bp_y + 58) {// lock the y axis of the logon box name Region
Intent intent = new Intent ();
Intent. putExtra ("name", "zhanglei ");
Intent. setClass (MainActivity. instance, Register. class );
MainActivity. instance. startActivity (intent );
}
}
Return super. onTouchEvent (event );
}
}

Import android. content. Context;
Import android. content. Intent;
Import android. content. res. Resources;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. util. Log;
Import android. view. MotionEvent;
Import android. view. SurfaceHolder;
Import android. view. SurfaceHolder. Callback;
Import android. view. SurfaceView;

Public class LoginView extends SurfaceView implements Callback, Runnable {

Private Canvas canvas; // defines the Canvas object.
Private Thread thread; // defines the corresponding Thread.
Private int ScreenW, ScreenH; // defines the width and height of the screen.
Private Paint paint; // defines the Paint brush object.
Private SurfaceHolder sh; // declare a SurfaceHolder object
Private Resources resources; // declare a resource object
Private Bitmap bitmapr; // register bitmap
Private Bitmap bitmapb; // background bitmap
Private int bp_x, bp_y; // specifies the width and height of the registered background image?
Private boolean flag;
Public static String zh_k = "registration page ";
Private boolean zh_flag = true;
Private String str_pass = "aaa ";
// Initial structure?
Public LoginView (Context context ){
Super (context );
//
Resources = this. getResources ();
Bitmapr = BitmapFactory. decodeResource (resources, R. drawable. register); // registration window
Bitmapb = BitmapFactory. decodeResource (resources, R. drawable. duola); // register the background?
Thread = new Thread (this );
Sh = this. getHolder ();
Sh. addCallback (this );
Paint = new Paint ();
Paint. setColor (Color. RED );
Paint. setAntiAlias (true); // remove the Sawtooth
This. setFocusable (true );
This. setKeepScreenOn (true); // set the screen to always be on
Resources = this. getResources ();

}
Public void draw (){
Canvas = sh. lockCanvas (); // obtain the canvas object.
Paint. setColor (Color. RED );
If (null! = Canvas) {// The coordinate algorithm is as follows when the landscape screen is set
Canvas. drawColor (Color. WHITE );
Canvas. drawBitmap (bitmapb,
-(Bitmapb. getWidth ()-ScreenW),-(bitmapb
. GetHeight ()-ScreenH), paint );
// Parameters 2 and 3 are x and y coordinates, which are the upper left corner of the bitmap painting.
Canvas. drawBitmap (bitmapr, bp_x, bp_y, paint );
Sh. unlockCanvasAndPost (canvas );
}
}
@ Override
Public void run (){
While (! Flag ){
Draw ();
Try {
Thread. sleep (1000 );
} Catch (Exception e ){
Log. v ("run", "error ");
}
}
}
@ Override
Public void surfaceCreated (SurfaceHolder holder ){
ScreenW = this. getWidth ();
ScreenH = this. getHeight ();
Bp_x = ScreenW/2-bitmapr.getWidth ()/2;
Bp_y = ScreenH/2-bitmapr.getHeight ()/2;
Thread. start ();
}
@ Override
Public void surfaceChanged (SurfaceHolder holder, int format, int width,
Int height ){

}
@ Override
Public void surfaceDestroyed (SurfaceHolder holder ){

}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
Float x = event. getX ();
Float y = event. getY ();

If (x> bp_x + 14 & x <bp_x + 129) {// lock the x axis of the logon box name Region
If (y> = bp_y + 43 & y <= bp_y + 58) {// lock the y axis of the logon box name Region
Intent intent = new Intent ();
Intent. putExtra ("name", "zhanglei ");
Intent. setClass (MainActivity. instance, Register. class );
MainActivity. instance. startActivity (intent );
}
}
Return super. onTouchEvent (event );
}
}


Run:

 

 


Method execution sequence:

LoginView --> surfaceCreated --> run --> draw

 


Method Analysis:

LoginView method:

OK. We can see that LoginView is actually initializing some global variables of this View, including thread objects and bitmap objects. In addition, some effects are set, including the removal of serrations. Notes in the program are particularly clear

SurfaceCreated method:


In this method, we can see that two groups of lengths and heights are initialized. I will not explain the Sreen as the name suggests. Bp_x and bp_y indicate the starting coordinates of the registered image on the screen. That is, the position in the upper left corner [algorithm resolution: "The Half coordinate of the screen is the Center Coordinate-register the half pixel value of the image"] is the starting position of the bitmap. Do you understand?

Run method:


The run method indicates the running of the thread. The Code logic is simple and will not be explained.

Draw method:


OK. I think the draw method is the core of a View. It is used to draw and refresh content on the screen. I have simplified the content of my himi blog. It should be easy to understand.

I got the canvas and drew two bitmaps. The comments in the two bitmaps are clearly written.

 


Note:

I encountered a particularly difficult problem when I was doing this case, that is, a new project of the himi source code encountered a disordered problem during the painting bitmap.

 

 

Note:

In the login window, click events for usernames and passwords are implemented by rewriting the onTouchEvent method. The related pixel coordinate area is locked. If the region is clicked, an Intent is sent. The Intent parameter passing method is the put method. In addition, in the AndroidManifest. xml file, pay attention to declare the Activity that your Intent jumps.

 


Disclaimer: I am also a beginner in Android. You are welcome to exchange and learn together. I will release the Source Code address of this blog post later. Because the company's network is not good, it may be upload after going home at night

 


 

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.