In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what are the core knowledge related to Android View". In daily operation, I believe many people have doubts about the core knowledge related to Android View. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what are the core knowledge related to Android View?" Next, please follow the editor to study!
1.View coordinates
(1) what are the main coordinate parameters of View? What are the main points for attention respectively?
Several main coordinate parameters are:
1) Left,Right,top,Bottom; they do not represent the absolute value from the upper left of the screen, but represent the relative coordinate values of view and his parent control, and represent the initial coordinates of View, which will not be changed after drawing.
2) X and Y represent the coordinate values of the upper-left corner of View relative to the parent control, that is, real-time relative coordinates.
3) both values of TranslationX,TranslationY default to 0, indicating the offset relative to the upper-left corner of the parent control.
The conversion relationship between them is:
X translator X; width toptranslation translationY; width = right-left; height = translation = getLeft ()
(2) several important methods in View?
1) onMeasure (widthMeasureSpec, heightMeasureSpec)
The onMeasure process determines the width and height of the View. After the completion of the Measure, the measured width and height of the view can be obtained by getMeasureWidth and getMeasureHeight methods, which will be equal to the width and height of the final view in almost all cases.
The onMeasure () method takes two parameters, widthMeasureSpec and heightMeasureSpec, which are used to determine the specification and size of the width and height of the view, respectively.
2) onLayout (boolean changed, int left, int top, int right, int bottom)
The layout process determines the coordinates of the four vertices of the View and the width and height of the actual View. After completion, you can obtain the four vertex positions of the View through getTop,getBottom,getLeft,getRight, and the final width and height of the View through getWidth,getHeight.
3) onDraw (Canvas canvas)
The draw process determines the display of the View. After the completion of the draw, the view will be displayed on the screen.
Draw the background (background.draw (Canvas))
Draw yourself
Protected void onDraw (Canvas canvas)
OnDraw draws its own, create a new paint and draw its own graphics on canvas.
Draw children (dispatchDraw)
DispatchDraw iterates through the draw methods that call all child elements
Paint Decoration (onDrawScrollBars)
4) whether the current view of isEnabled () is available.
You can call the setEnable () method to change the available state of the view, passing in true to indicate availability and false to indicate unavailability.
The difference between them is that unavailable views cannot respond to onTouch events.
5) whether the current view of isFocused () gets focus
In general, there are two ways to focus a view, that is, to switch views through the keyboard keys and to call the requestFocus () method.
Today's Android phones almost don't have a keyboard, so basically you can only use requestFocus () to get the view into focus.
The requestFocus () method does not guarantee that the view will get focus. It will have a Boolean return value. If you return true, you will get focus successfully. If you return false, you will fail to get focus. In general, the view can only successfully gain focus if the focusable and focusable in touch mode are established at the same time, such as EditText.
6) offsetTopAndBottom (int offset) and offsetLeftAndRight (int offset)
OffsetTopAndBottom directly changes top, bottom, which is equivalent to translating the position of View up and down in parent.
OffsetLeftAndRight directly changes left, right, which is equivalent to translating the position of View left and right in parent.
The boundary of View changes directly, and because the relative position of View and his child View remains the same, so does the boundary of his child View.
(3) what to do when the width and height of View is 0?
If we want to get the location coordinates of a view, we can get it directly after the child findviewbyid, in the click event of the view, or elsewhere. If we get it directly after finedviewbyid, sometimes it will fail, and the value we get is 0.
There are three possible reasons for the analysis:
1) the width and height of view itself is 0) 2) the visibility property of View is gone;3) the view has not been drawn, of course, it is not finished yet, for example, the interface represented by activity has not been displayed and has not been added to WindowPhone's DecorView; the view to be obtained has not been added to the DecorView.
The main thing we want to talk about is the third situation. how can we solve it?
1) get it in the event callback of View; at this time, the view has been displayed and added to the DecorView, such as click event, touch event focus event, etc.
2) get a callback method as wide and as high as onWindowFocusChanged () when activity is added to DecorView when it is displayed
3) through post (Runnable) in the onResume method
4) use getViewTreeObserver () .addOnGlobalLayoutListener () when you need to obtain width and height in methods such as onCreate () or onResume ().
To add a callback for view and ask view to delete the callback after obtaining the width or height in the callback.
5) view.measure (int widthMeasureSpec,int heightMeasureSpec)
Manually measure the view to get the width and height of the view (this is a special way, you need to consider the incoming spec)
2.View sliding
1. There are several ways to let view slide. What should you pay attention to respectively? Which scenarios do they apply to?
1) Slide through the scrollTo and scrollBy provided by view itself; however, you can only slide the contents of view, not view itself. For example, when textview calls these two methods, the slide is the content of the displayed word. Check out the details
Http://www.jianshu.com/p/2b48551d53192
2) translate view with motion tween
Motion tween translation view, is the operation of the image of view, it can not really change the position parameters of view.
3) translate view with attribute animation
ObjectAnimator .offloat (view, "translationX", 0500) .setDuration (2000) .start (); ObjectAnimator .offloat (view, "translationY", 0500) .setDuration (2000) .start ()
Attribute animation translation view needs to be compatible with version 3.0 or below
4) change the LayoutParams of view to make the view relayout to achieve sliding.
ViewGroup.MarginLayoutParams params = view.getLayoutParams (); params.leftMargin + = 500
(2) comparison of three sliding modes
ScrollTo and scrollBy are simple and easy to operate, but they can only slide the contents of view, not view itself.
Motion tween sliding is an operation on view images, and it can not really change the position parameters of view.
Attribute animation sliding can change the position parameters of view, but it needs to be compatible with version 3.0 or below.
LayoutParams, which is a little troublesome to use, is used in interactive view.
(3) what are the consequences of using animation (non-attribute animation) to achieve view sliding?
A: in fact, the view animation is the movement of the ui on the surface of the view, that is, the visual effects presented to the user. The animation itself cannot move the real position of the view, with the exception of attribute animation.
After the animation is over, view will eventually return to its own position. Of course, you can set the FillAfter property to make the view representation stay in the changed position after the animation ends.
So this will have a very serious consequence. For example, if your button is on the left side of the screen, you now use an animation and set the fillafter property to let him go to the right.
You will find that there is no click event triggered by clicking on the button on the right, but it can be triggered by clicking on the left, because the button on the right is just the appearance of view, and the real button has not been moved on the left.
If you must do this, you can put a new button in the position where the button is moved on the right in advance. When you finish the animation, you can gone the visible on the right and the one on the left.
(4) what are the methods of sliding gradient effect of view?
Three kinds
* Scroller is also the most frequently used.
The second kind is animation, animation I will not say more, does not belong to the scope of this article.
The third, which we often use, is to use handler to update the status of view at regular intervals.
(5) when is the better time to use onTouchEvent or GestureDetector?
Use the former only when sliding the demand, and the latter if there is a double click, etc.
At this point, the study of "what are the core knowledge related to Android View" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.