Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize the control view in Android

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to customize the control view in Android. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

First acquaintance of View

All the controls of Android are subclasses of View or View, which actually represents a rectangular area on the screen, which is represented by a Rect, left,top represents the starting point of View relative to its parent View, width,height represents the width and height of View itself, through these four fields you can determine the position of View on the screen, and after determining the location, you can begin to draw the content of View.

The drawing process of View

Generally speaking, we all know that the drawing of View can be divided into the following three processes:

Measure

View will first make a measurement to figure out how much area he needs to occupy. The Measure process of View exposes an interface onMeasure. The definition of the method is like this

Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {}

The View class already provides a basic onMeasure implementation

Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {setMeasuredDimension (getDefaultSize (getSuggestedMinimumWidth (), widthMeasureSpec), getDefaultSize (getSuggestedMinimumHeight (), heightMeasureSpec));} public static int getDefaultSize (int size, int measureSpec) {int result = size; int specMode = MeasureSpec.getMode (measureSpec); int specSize = MeasureSpec.getSize (measureSpec); switch (specMode) {case MeasureSpec.UNSPECIFIED: result = size; break; case MeasureSpec.AT_MOST: case MeasureSpec.EXACTLY: result = specSize; break } return result;}

Invoke has the setMeasuredDimension () method, which sets the width and height of the View in the measure process, and there is also a corresponding method for the minimum Width,Height in which getSuggestedMinimumWidth () returns View. By the way, the MeasureSpec class is an internal static class of the View class, which defines three constants UNSPECIFIED, AT_MOST, and EXACTLY. In fact, we can understand it this way, which corresponds to match_parent, wrap_content, and xxxdp in LayoutParams. We can rewrite onMeasure to redefine the width and height of View.

Layout

The Layout procedure is very simple for the View class, and View exposes the onLayout method.

Protected void onLayout (boolean changed, int left, int top, int right, int bottom) {}

Because we are talking about View, there is no sub-View to arrange, so we don't really need to do any extra work in this step.

Draw

The Draw process is to draw the View style we need on canvas. Similarly, View exposes the onDraw method to us.

Protected void onDraw (Canvas canvas) {}

The onDraw of the default View class does not have a single line of code, but it provides us with a blank canvas, for example, like a picture scroll, we are painters, what kind of effect we can draw is entirely up to us.

There are three more important methods in View

RequestLayout

View calls the layout procedure again.

Invalidate

View calls the draw procedure again

ForceLayout

Identifies that the View will be redrawn the next time, and the layout procedure needs to be called again.

Custom attribute

We have introduced the entire View drawing process, there is a very important knowledge, custom control properties, we all know that View already has some basic properties, such as layout_width,layout_height,background, etc., we often need to define their own properties, so we can do so.

1. In the values folder, open attrs.xml, in fact, the file name can be arbitrary, write here a little more standard, indicating that it contains all the attributes of view.

two。 Since our example below will use attributes of two lengths and one color value, we will first create three attributes here.

So exactly how to use it, we will look at an example.

Implement a relatively simple Google rainbow progress bar.

Because we don't have to focus on measrue and layout procedures here, we can just override the onDraw method.

In fact, it is to call the drawLine method of canvas, and then push the starting point of draw forward every time. At the end of the method, we call the invalidate method. As we have explained above, this method causes View to call the onDraw method again, so it achieves the effect that our progress bar is always drawing forward. Here is the final display. When made into gif, there seems to be a chromatic aberration, but the real effect is blue.

This is the end of this article on "how to customize the control view in Android". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report