Step-by-step _android Development Course [13]_ user interface View]

Source: Internet
Author: User

Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305

    • Topic: View of the user interface
      The-view class is a super class for Android, which contains almost all screen types. Each view has a canvas for drawing, and this canvas can be expanded arbitrarily. In the middle of the game development can be customized views (view), the function of this canvas is more to meet our needs in the game development. In Android, any view class simply overrides the OnDraw method to display the interface, and the custom view can be a complex 3D implementation, or it can be a very simple form of text.

View Usage Example
Let's customize a Gameview view (to show the game interface), all custom views come from view, so it must inherit the parent view.

Source code for Gameview.java:

ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.view.View;//If you want to customize the Gameview view, you must first inherit the parent Public  class gameview extends View {    intMicount =0; Public Gameview(Context context) {Super(context); }//OnDraw is a brush method and canvas is the canvas. The canvas is the equivalent of a paper. Draw on the canvas with a brush.      Public void OnDraw(Canvas canvas) {if(Micount < -) {micount++; }Else{Micount =0; }//DrawingPaint Mpaint =NewPaint ();Switch(Micount%4) { Case 0: Mpaint.setcolor (Color.Blue); Break; Case 1: Mpaint.setcolor (Color.green); Break; Case 2: Mpaint.setcolor (color.red); Break; Case 3: Mpaint.setcolor (Color.yellow); Break;default: Mpaint.setcolor (Color.White); Break; }//Draw a rectangle on the canvas, the parameters are used to define the display position (i.e. coordinates)Canvas.drawrect (( the- the) /2,0, ( the- the) /2+ the, +, Mpaint); }}

Mainactivity.java Source code:

Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;ImportAndroid.view.KeyEvent;ImportAndroid.view.MotionEvent; Public  class mainactivity extends Activity {    PrivateGameview Mgameview =NULL;@Override     Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);//Instantiate Gameview object         This. Mgameview =NewGameview ( This);//settings displayed for our custom view (Gameview)Setcontentview (Mgameview);//Open thread        NewThread (NewGamethread ()). Start (); } Handler MyHandler =NewHandler () {//Receive message after processing         Public void Handlemessage(Message msg) {Switch(msg.what) { Case 1://Note that the Refresh interface here is actually executing in the UI thread instead of opening a thread, here to figure out                //invalidate () is used to refresh the view and must be working in the UI threadMgameview.invalidate (); Break; }Super. Handlemessage (msg);    }    }; Class Gamethread implements Runnable { Public void Run() { while(! Thread.CurrentThread (). isinterrupted ()) {Message message =NewMessage (); Message.what =1;//Send MessageMyhandler.sendmessage (message);Try{Thread.Sleep ( +); }Catch(Interruptedexception e)                {Thread.CurrentThread (). interrupt (); }            }        }    }}

Operation Result:

The color of the rectangle constantly refreshes and simulates the effect of refreshing the game's interface.

Focus on technology, enjoy life! --qq:804212028
Browse Links: http://blog.csdn.net/y18334702058/article/details/44624305

Step-by-step _android Development Course [13]_ user interface View]

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.