《Expert Android》关键点摘录之一:Exploring Custom Views

简介: 一、In Android you can customize views in three ways:1、Custom views (by extending the View class...

一、In Android you can customize views in three ways:

1、Custom views (by extending the View class);

2、Compound views/controls(by composing other controls through extending one of existing Layout classes);

3、Custom layouts( by extending the ViewGroup class);

 

二、the custom view

1、how to embed the costom view in any Android layouts;

2、how the cusotm view responds to touch events;

3、how the custom view remembers states as you rotate the device;

4、how to use custom attributes in layout files to initialize the custom view.

 

三、the key classes

View、ViewParent(interface)、ViewGroup(extends View and implements ViewParent)、ViewRoot

1、View

     View is the fundamental class that all of the visible compontents in Android are derived from. It defines a number of callbacks to customize its behavior, like the ability to define size, draw, and save to state.

2、ViewParent(interface)

     A ViewParent defines the protocol for any object(including another view) that wants to play the role of a parent to other views. There are two important view parents. Of course, ViewGroup is the key one. In addition to being a ViewParent, a ViewGroup also defines the protocol for a coolection of child views. All layouts like the FrameLayout and LinearLayout in Android SDK extend this class ViewGroup. ViewGroup plays a central role in defining these layouts in XML files and in placing the controls(views) at the right place. A ViewGroup also controls the background and animation of its child views.

    3、 The other key ViewParent,  the ViewRoot is implementation centric and its not a public API. In some release it is called ViewRoot, and in some implementations it is called ViewRoot Implementation and it may be changed in the future to something else. However, this class is important for understanding how drawing is done in Android.

     Being a root parent of all views in the activity, the ViewRoot schedules traversals of all the views in order to first lay them out at the right place with the right size; this is called the layout phase. The ViewRoot then traverses the view hierarchy to draw them; this phase is called drawing phase.

 

四、Layout Phase:Measurement and Layout

      The goal of the layout phase is to know the position and size of each view in the view hierarchy owned by a parent such as the VIewRoot. To calculate the position and size of each view, the ViewRoot initiates a layout phase. However, in the layout phase, the view root dose a traversal of only those views that reported or requested a layout change. This conditional measurement is to save resource and improve response time.

     The trigger to the layout phase may come from multiple events. One trigger may be the very first time everytihing is being drawn. Or one of the views, while reacting to an event like a click or touch, could report that its size has changed. In such an event, the view that got clicked on calls the method requestLayout(). This call walks up the chain and gets to the root view(ViewRoot). The root view then schedules a layout traversal message ont the main thread's queue.

 

 

 

 

相关文章
|
前端开发 Android开发
Android custom View AirConditionerView hacking
package com.example.arc.view; import android.content.Context; import android.graphics.Canvas; import android.
660 0
|
XML Java API
Understanding Android Custom Attributes: An Article
Understanding Android Custom Attributes: An Article Being able to modify behavior of a run ti...
1186 0
|
6天前
|
Android开发 开发者 Kotlin
探索安卓开发中的新特性
【9月更文挑战第14天】本文将引导你深入理解安卓开发领域的一些最新特性,并为你提供实用的代码示例。无论你是初学者还是经验丰富的开发者,这篇文章都会给你带来新的启示和灵感。让我们一起探索吧!
|
2天前
|
Java Linux Android开发
深入理解Android开发:从基础到高级
【9月更文挑战第17天】本文将深入探讨Android开发的各个方面,包括应用开发、操作系统等。我们将通过代码示例来展示如何创建一个简单的Android应用,并解释其背后的原理。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和启示。
|
10天前
|
IDE 开发工具 Android开发
安卓与iOS开发对比:平台选择对项目成功的影响
【9月更文挑战第10天】在移动应用开发的世界中,选择正确的平台是至关重要的。本文将深入探讨安卓和iOS这两大主要移动操作系统的开发环境,通过比较它们的市场份额、开发工具、编程语言和用户群体等方面,为开发者提供一个清晰的指南。我们将分析这两个平台的优势和劣势,并讨论如何根据项目需求和目标受众来做出最佳选择。无论你是初学者还是有经验的开发者,这篇文章都将帮助你更好地理解每个平台的特性,并指导你做出明智的决策。
|
6天前
|
XML 编解码 Android开发
安卓开发中的自定义视图控件
【9月更文挑战第14天】在安卓开发中,自定义视图控件是一种高级技巧,它可以让开发者根据项目需求创建出独特的用户界面元素。本文将通过一个简单示例,引导你了解如何在安卓项目中实现自定义视图控件,包括创建自定义控件类、处理绘制逻辑以及响应用户交互。无论你是初学者还是有经验的开发者,这篇文章都会为你提供有价值的见解和技巧。
14 3
|
8天前
|
API Android开发 iOS开发
安卓与iOS开发中的线程管理对比
【9月更文挑战第12天】在移动应用的世界中,安卓和iOS平台各自拥有庞大的用户群体。开发者们在这两个平台上构建应用时,线程管理是他们必须面对的关键挑战之一。本文将深入探讨两大平台在线程管理方面的异同,通过直观的代码示例,揭示它们各自的设计理念和实现方式,帮助读者更好地理解如何在安卓与iOS开发中高效地处理多线程任务。
|
10天前
|
开发框架 Android开发 iOS开发
探索安卓与iOS开发的差异:构建未来应用的指南
在移动应用开发的广阔天地中,安卓与iOS两大平台各占半壁江山。本文将深入浅出地对比这两大操作系统的开发环境、工具和用户体验设计,揭示它们在编程语言、开发工具以及市场定位上的根本差异。我们将从开发者的视角出发,逐步剖析如何根据项目需求和目标受众选择适合的平台,同时探讨跨平台开发框架的利与弊,为那些立志于打造下一个热门应用的开发者提供一份实用的指南。
24 5