View framework for Android Game Development

Source: Internet
Author: User

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.

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.