Android Programming View Simple Learning example _android

Source: Internet
Author: User

This article illustrates the simple learning example of Android programming view. Share to everyone for your reference, specific as follows:

View, which is a superclass of Android, contains almost all of the screen types. Each view has a canvas for drawing, and the canvas can be arbitrarily extended.

The view is customizable in the middle of the game development, and the canvas features more to meet our needs in game development. In Android, any view class can simply rewrite the OnDraw method to display the interface, and a custom view could be a complex 3D implementation or a very simple text form.

The core of the game is constantly drawing and refreshing the interface, Android provides a invalidate method to achieve interface refresh. Note that invalidate cannot be invoked directly in a thread, which is not invoked in a child thread, so it violates the Android Single-threaded model: Android UI operations are not thread-safe and must be executed in the UI thread. So the most common approach to Android is to use handler to implement UI thread updates. In fact, you can use Asynctask.

Specific examples:

Activity:

public class Activity01 extends activity {private static final String TAG = "Mthread"; 
  private static final int REFRESH = 0x000001; 
  Private Gameview Mgameview = null; 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
    This.mgameview = new Gameview (this); 
    mgameview.x = 320.0f; 
    Mgameview.y = 120.0f; 
    Setcontentview (Mgameview); 
  New Thread (New Gamethread ()). Start (); } Handler MyHandler = new Handler () {@Override public void Handlemessage (msg) {switch (MSG.W   HAT) {case Activity01.REFRESH:mGameView.invalidate (); 
      Repaint View break; 
    Super.handlemessage (msg); 
  } 
  }; Class Gamethread implements Runnable {@Override public void run () {while! 
        Thread.CurrentThread (). isinterrupted ()) {message: = new Message (); 
        Message.what = Activity01.refresh; Activity01.this.myHandleR.sendmessage (message); 
        try {thread.sleep (1000); 
        The catch (Interruptedexception e) {thread.currentthread (). interrupt (); @Override public boolean ontouchevent (Motionevent event) {if event.getaction () = = Motio 
      Nevent.action_down) {mgameview.x = Event.getx (); 
    MGAMEVIEW.Y = Event.gety (); 
  return true; 
      @Override public boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back) { 
    This.finish (); 
  return true; } 
}

Gameview:

public class Gameview extends View { 
  int count = 0; 
  float x = 0, y = 0; 
  Public Gameview { 
    super (context); 
  } 
  public void OnDraw (Canvas Canvas) { 
    if (count <) { 
      count++; 
    } else { 
      count = 0; 
    } 
    Paint mpaint = new Paint (); 
    Switch (count% 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; 
    Canvas.drawrect (x-40, y-20, x +, Y +, mpaint); 
  } 

Operation Effect:

Full instance code code click here to download the site.

I hope this article will help you with the Android program.

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.