闲来看看View.java的Developer Guides



http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.2_r1/android/view/View.java?av=f

Event Handling and Threading

The basic cycle of a view is as follows:

  1. An event comes in and is dispatched to the appropriate view. The view handles the event and notifies any listeners.
  2. If in the course of processing the event, the view‘s bounds may need to be changed, the view will call
    requestLayout().
  3. Similarly, if in the course of processing the event the view‘s appearance may need to be changed, the view will call
    invalidate().
  4. If either requestLayout() or
    invalidate() were called, the framework will take care of measuring, laying out, and drawing the tree as appropriate.

Position

The geometry of a view is that of a rectangle. A view has a location, expressed as a pair of
left and top coordinates, and two dimensions, expressed as a width and a height. The unit for location and dimensions is the pixel.

It is possible to retrieve the location of a view by invoking the methods
getLeft()
and getTop(). The former returns the left, or X, coordinate of the rectangle representing the view. The latter returns the top, or Y, coordinate of the rectangle representing the view. These methods both return the location of
the view relative to its parent. For instance, when getLeft() returns 20, that means the view is located 20 pixels to the right of the left edge of
its direct parent.

In addition, several convenience methods are offered to avoid unnecessary computations, namely
getRight() and getBottom(). These methods return the coordinates of the right and bottom edges of the rectangle representing the view. For instance, calling
getRight() is similar to the following computation: getLeft() + getWidth() (see
Size for more information about the width.)

Size, padding and margins

The size of a view is expressed with a width and a height. A view actually possess two pairs of width and height values.

The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent (see
Layout for more details.) The measured dimensions can be obtained by calling
getMeasuredWidth() and getMeasuredHeight().

The second pair is simply known as width and height, or sometimes
drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values may, but do not have to, be different from the measured width and height. The width and height
can be obtained by calling getWidth() and
getHeight().

To measure its dimensions, a view takes into account its padding. The padding is expressed in pixels for the left, top, right and bottom parts of the view. Padding can be used to offset the content of the view by a specific amount of pixels. For instance,
a left padding of 2 will push the view‘s content by 2 pixels to the right of the left edge. Padding can be set using the
setPadding(int, int, int, int) or setPaddingRelative(int, int, int, int) method and queried by calling
getPaddingLeft(), getPaddingTop(), getPaddingRight(),
getPaddingBottom(), getPaddingStart(), getPaddingEnd().

Even though a view can define a padding, it does not provide any support for margins. However, view groups provide such a support. Refer to
android.view.ViewGroup and android.view.ViewGroup.MarginLayoutParams for further information.

Layout

Layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in
measure(int, int) and is a top-down traversal of the view tree. Each view pushes dimension specifications down the tree during the recursion. At the end of the measure pass, every view has stored its measurements. The second pass happens in
layout(int,int,int,int) and is also top-down. During this pass each parent is responsible for positioning all of its children using the sizes computed in the measure pass.

When a view‘s measure() method returns, its getMeasuredWidth() and
getMeasuredHeight() values must be set, along with those for all of that view‘s descendants. A view‘s measured width and measured height values must respect the constraints imposed by the view‘s parents. This guarantees that at the end of the measure
pass, all parents accept all of their children‘s measurements.
A parent view may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with
actual numbers if the sum of all the children‘s unconstrained sizes is too big or too small.

The measure pass uses two classes to communicate dimensions. The
MeasureSpec class is used by views to tell their parents how they want to be measured and positioned. The base LayoutParams class just describes how big the view wants to be for both width and height. For each dimension,
it can specify one of:

  • an exact number
  • MATCH_PARENT, which means the view wants to be as big as its parent (minus padding)
  • WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding).

There are subclasses of LayoutParams for different subclasses of ViewGroup. For example, AbsoluteLayout has its own subclass of LayoutParams which adds an X and Y value.

MeasureSpecs are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:

  • UNSPECIFIED: This is used by a parent to determine the desired dimension of a child view. For example, a LinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of
    EXACTLY 240 to find out how tall the child view wants to be given a width of 240 pixels.
  • EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.
  • AT_MOST: This is used by the parent to impose a maximum size on the child. The child must gurantee that it and all of its descendants will fit within this size.

To intiate a layout, call requestLayout. This method is typically called by a view on itself when
it believes that is can no longer fit within its current bounds.

Drawing

Drawing is handled by walking the tree and rendering each view that intersects the invalid region. Because the tree is traversed in-order, this means that parents will draw before (i.e., behind) their children, with siblings drawn in the order they appear
in the tree. If you set a background drawable for a View, then the View will draw it for you before calling back to its
onDraw() method.

Note that the framework will not draw views that are not in the invalid region.

To force a view to draw, call
invalidate().

Event Handling and Threading

The basic cycle of a view is as follows:

  1. An event comes in and is dispatched to the appropriate view. The view handles the event and notifies any listeners.
  2. If in the course of processing the event, the view‘s bounds may need to be changed, the view will call
    requestLayout().
  3. Similarly, if in the course of processing the event the view‘s appearance may need to be changed, the view will call
    invalidate().
  4. If either
    requestLayout() or
    invalidate() were called, the framework will take care of measuring, laying out, and drawing the tree as appropriate.

Note: The entire view tree is single threaded. You must always be on the UI thread when calling any method on any view. If you are doing work on other threads and want to update the
state of a view from that thread, you should use a Handler.

//必须在UI线程中调用这2个方法

Focus Handling

The framework will handle routine focus movement in response to user input. This includes changing the focus as views are removed or hidden, or as new views become available. Views indicate their willingness to take focus through the
isFocusable method. To change whether a view can take focus, call
setFocusable(boolean)
. When in touch mode (see notes below) views indicate whether they still would like focus via
isFocusableInTouchMode and can change this via setFocusableInTouchMode(boolean).

Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer. In these situations, you can provide explicit overrides by using these
XML attributes in the layout file:

 nextFocusDown
 nextFocusLeft
 nextFocusRight
 nextFocusUp
 

To get a particular view to take focus, call requestFocus().

Touch Mode

When a user is navigating a user interface via directional keys such as a D-pad, it is necessary to give focus to actionable items such as buttons so the user can see what will take input. If the device has touch capabilities, however, and the user begins
interacting with the interface by touching it, it is no longer necessary to always highlight, or give focus to, a particular view. This motivates a mode for interaction named ‘touch mode‘.

For a touch capable device, once the user touches the screen, the device will enter touch mode. From this point onward, only views for which
isFocusableInTouchMode is true will be focusable, such as text editing widgets. Other views that are touchable, like buttons, will not take focus when touched; they will only fire the on click listeners.

Any time a user hits a directional key, such as a D-pad direction, the view device will exit touch mode, and find a view to take focus, so that the user may resume interacting with the user interface without touching the screen again.

The touch mode state is maintained across android.app.Activitys. Call
isInTouchMode to see whether the device is currently in touch mode.

Scrolling

The framework provides basic support for views that wish to internally scroll their content. This includes keeping track of the X and Y scroll offset as well as mechanisms for drawing scrollbars. See
scrollBy(int, int), scrollTo(int, int), and awakenScrollBars() for more details.

Tags

Unlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view. They are most often used as a convenience to store data related to views in the views themselves rather than by putting
them in a separate structure.

Properties

The View class exposes an ALPHA property, as well as several transform-related properties, such as
TRANSLATION_X and TRANSLATION_Y. These properties are available both in the
Property form as well as in similarly-named setter/getter methods (such as
setAlpha(float) for ALPHA). These properties can be used to set persistent state associated with these rendering-related properties on the view. The properties and methods can also be used in conjunction with
Animator-based animations, described more in the
Animation section.

Animation

Starting with Android 3.0, the preferred way of animating views is to use the
android.animation
package APIs. These Animator-based classes change actual properties of the View object, such as
alpha and translationX. This behavior is contrasted to that of the pre-3.0
Animation-based classes, which instead animate only how the view is drawn on the display. In particular, the
ViewPropertyAnimator class makes animating these View properties particularly easy and efficient.

Alternatively, you can use the pre-3.0 animation classes to animate how Views are rendered. You can attach an
Animation object to a view using setAnimation(Animation) or
startAnimation(Animation). The animation can alter the scale, rotation, translation and alpha of a view over time. If the animation is attached to a view that has children, the animation will affect the entire subtree rooted by that node. When
an animation is started, the framework will take care of redrawing the appropriate views until the animation completes.

闲来看看View.java的Developer Guides,布布扣,bubuko.com

时间: 2024-08-02 23:02:40

闲来看看View.java的Developer Guides的相关文章

eclipse中jsp文档无语法着色,安装Eclipse Java Web Developer Tools插件

一.安装Eclipse Java Web Developer Tools插件 1.eclipse菜单:help/install New Software,打开Available Software窗体: 2.Available Software窗体:Work with下拉框中选择mars - http://download.eclipse.org/releases/mars项,mars是eclipse版本,可能有所不同: 3.待Pending...完成后,会显示可选装的插件列表,如果不勾选Cont

月经贴:当落魄的.NET基佬遇上不可一世的JAVA派 developer

事先声明,这篇文章很没有营养,大家当笑话看就好,不要搞骂战污染博客园了谢谢. 背景: .NET(以下简称N)心里想:现在企业级应用.NET用的少,但起码.net的语法特性优美,IDE宇宙最强吧,啧啧. JAVA(以下简称J)心里想:我大JAVA遍地开花,什么Android,kotlin,scale,总之就是一个字:叼. 撕逼大战: 事情的发生是从落魄N想学安卓开始.他接触了一段时候后,吐槽Android studio开始的.大家知道这玩意很多坑的,随后N就随带的埋怨其老东家Google起来最后连

android-23 View.java - dispatchTouchEvent源码

public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource { /** * Pass the touch screen motion event down to the target view, or this * view if it is the target. * * @param event The motion event to be dispatched. *

android-8~23 View.java - dispatchTouchEvent源码

android-8 /** * Pass the touch screen motion event down to the target view, or this * view if it is the target. * * @param event The motion event to be dispatched. * @return True if the event was handled by the view, false otherwise. */ public boolea

[LeetCode] 199. Binary Tree Right Side View Java

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3,

199. Binary Tree Right Side View java solutions

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3, 4].

java.lang.NoSuchMethodException: [class android.view.View]

05-24 11:38:35.884: E/AndroidRuntime(1819): FATAL EXCEPTION: main05-24 11:38:35.884: E/AndroidRuntime(1819): Process: com.example.activitytest, PID: 181905-24 11:38:35.884: E/AndroidRuntime(1819): java.lang.IllegalStateException: Could not find a met

view.setTag(key,Object) (java.lang.IllegalArgumentException: The key must be an application-specific resource id.)

转自: http://blog.csdn.net/brokge/article/details/8536906 setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息,在很多场合下都得到妙用. setTag(Object tag)方法比较简单,这里主要谈一谈带两个参数的setTag方法. 官方的api文档中提到:" The specified key should be an id declared in the resources of the applica

Android java.lang.StackOverflowError at android.view.ViewGroup.drawChild(ViewGroup.java:2666)

做这样一个页面,下面有五个页签,滑动,有数据显示listview,无数据显示动画,开始我看到他们好多东西是相同的,就提取出来,用include包含布局文件: 结果报一下错误: 01-28 11:27:36.593: E/AndroidRuntime(16182): FATAL EXCEPTION: main 01-28 11:27:36.593: E/AndroidRuntime(16182): java.lang.StackOverflowError 01-28 11:27:36.593: E