Android UI Development Topic (iv) View self-painting control

Source: Internet
Author: User

Most of the time you want to design a nice Android UI, the controls that you use with Android don't meet our needs. Consider customizing the control, display Class View in the Android interface, and implement our graph drawing by inheriting extension rewrite related methods.

First we need to understand the bottom of the view class implementation, in the SDK we can see the view directly inherit from the Java base class object, the implementation of graphics and key events Drawable.callback Keyevent.callback related methods, we mainly implement the internal OnDraw method, the relevant interface calculation can rewrite the Onmeasure method, for the relevant keys can overload onkeydown, OnKeyUp and Ontouchevent and so on, the following Android Development network is represented as an example.

public class Cwjview Extendsview

{

Public Cwjview

{

This (context,null);

}

Public Cwjview (Context Context,attributeset attrs)

{

This (context,attrs,0);

}

Public Cwjview (Context Context,attributeset attrs,int Defstyle)

{

Super (Context,attrs,defstyle);

Here is the construction of this class, where related initialization can add code

}

@Override

PROTECTD void OnDraw (Canvas Canvas)

{

Super (canvas);

The key to the drawing, you can see that already contains a canvas handle, you can directly through the canvas class we talked about the relevant operations, complete examples, you can refer to the Android SDK Example Snake snake Game implementation.

}

}

For updates to the view class, we update a rect rectangle area directly by calling invalidate (int l,int r,int r,int b), and we update the interface with the need to invoke postinvalidate in the thread.

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.