Detailed explanation of custom view usage in Android Development

This article describes the usage of user-defined view in Android development. Share with you for your reference, as follows:

The view class is a super class of Android. This class contains almost all screen types. Each view has a canvas for drawing, which can be extended arbitrarily. In game development, we often need to customize the view. The function of this canvas can better meet our needs in game development. In Android, any view class only needs to rewrite the OnDraw method to realize the interface display. The customized view can be a complex 3D implementation or a very simple text form.

In order to implement a custom view, you need to create a new class and override the OnDraw method. Here, you need to note that the newly created class MyView should inherit the view base class and add two constructor methods MyView (context context) and MyView (attributeset attr) with parameters, otherwise the compilation operation cannot pass.

In the OnDraw method, initialize a brush object Mypaint, set the brush color, text size, filling and other attributes. Then use the parameter canvas passed in by this method to draw a bar statistical chart. The specific codes are as follows:

Then add our custom view to main In the XML layout file, the background color of view is set to white here to better show the contents. The code is as follows:

Initial activity Java files do not need to be modified. The final effect is shown in the figure below:

For more Android related content, readers who are interested can view the topics on this site: summary of Android view skills, summary of Android graphics and image processing skills, introduction and advanced tutorial of Android development, summary of Android debugging skills and solutions to common problems, summary of Android multimedia operation skills (audio, video, recording, etc.) Usage Summary of Android basic components, Android layout skills summary and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>