MainActivity. java
[Java]
Package com. soai. view;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Window;
Import android. view. WindowManager;
Public class MainActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Full screen display
This. requestWindowFeature (Window. FEATURE_NO_TITLE );
This. getWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN, WindowManager. LayoutParams. FLAG_FULLSCREEN );
SetContentView (new MyView (this ));
}
} Www.2cto.com
MyView. java
[Java]
Package com. soai. view;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. view. MotionEvent;
Import android. view. View;
/**
*
* @ Author SoAi
*
*/
Public class MyView extends View {
Private int textX = 20, textY = 20;
Public MyView (Context context ){
Super (context );
SetFocusable (true );
}
@ Override
Protected void onDraw (Canvas canvas ){
// Create a paint brush instance
Paint paint = new Paint ();
Paint. setColor (Color. RED );
// Draw text
Canvas. drawText ("Game", textX, textY, paint );
Super. onDraw (canvas );
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
// Obtain the X coordinate assigned to the text by the X axis of the user's Touch Screen
TextX = (int) event. getX ();
// Obtain the Y coordinate assigned to the text by the Y axis of the user's Touch Screen
TextY = (int) event. getY ();
Invalidate ();
Return true;
}
}
MyView needs to inherit the View and implement the onDraw (Canvas canvas) method in it. Canvas is equivalent to a blank sheet of white paper, while painting is the pen of the painting tool, these two classes can be used to achieve good graphics in the game.
Call the invalidate () method to redraw it, that is, call the onDraw () method once. If it is called in other classes, you must call postInvalidate.