《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.
667 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...
1191 0
|
4天前
|
搜索推荐 Android开发 开发者
探索安卓开发中的自定义视图:打造个性化UI组件
【10月更文挑战第39天】在安卓开发的世界中,自定义视图是实现独特界面设计的关键。本文将引导你理解自定义视图的概念、创建流程,以及如何通过它们增强应用的用户体验。我们将从基础出发,逐步深入,最终让你能够自信地设计和实现专属的UI组件。
|
6天前
|
Android开发 Swift iOS开发
探索安卓与iOS开发的差异和挑战
【10月更文挑战第37天】在移动应用开发的广阔舞台上,安卓和iOS这两大操作系统扮演着主角。它们各自拥有独特的特性、优势以及面临的开发挑战。本文将深入探讨这两个平台在开发过程中的主要差异,从编程语言到用户界面设计,再到市场分布的不同影响,旨在为开发者提供一个全面的视角,帮助他们更好地理解并应对在不同平台上进行应用开发时可能遇到的难题和机遇。
|
8天前
|
XML 存储 Java
探索安卓开发之旅:从新手到专家
【10月更文挑战第35天】在数字化时代,安卓应用的开发成为了一个热门话题。本文旨在通过浅显易懂的语言,带领初学者了解安卓开发的基础知识,同时为有一定经验的开发者提供进阶技巧。我们将一起探讨如何从零开始构建第一个安卓应用,并逐步深入到性能优化和高级功能的实现。无论你是编程新手还是希望提升技能的开发者,这篇文章都将为你提供有价值的指导和灵感。
|
6天前
|
存储 API 开发工具
探索安卓开发:从基础到进阶
【10月更文挑战第37天】在这篇文章中,我们将一起探索安卓开发的奥秘。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和建议。我们将从安卓开发的基础开始,逐步深入到更复杂的主题,如自定义组件、性能优化等。最后,我们将通过一个代码示例来展示如何实现一个简单的安卓应用。让我们一起开始吧!
|
7天前
|
存储 XML JSON
探索安卓开发:从新手到专家的旅程
【10月更文挑战第36天】在这篇文章中,我们将一起踏上一段激动人心的旅程,从零基础开始,逐步深入安卓开发的奥秘。无论你是编程新手,还是希望扩展技能的老手,这里都有适合你的知识宝藏等待发掘。通过实际的代码示例和深入浅出的解释,我们将解锁安卓开发的关键技能,让你能够构建自己的应用程序,甚至贡献于开源社区。准备好了吗?让我们开始吧!
19 2
|
8天前
|
Android开发
布谷语音软件开发:android端语音软件搭建开发教程
语音软件搭建android端语音软件开发教程!