《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.
674 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...
1199 0
|
1月前
|
开发框架 前端开发 Android开发
安卓与iOS开发中的跨平台策略
在移动应用开发的战场上,安卓和iOS两大阵营各据一方。随着技术的演进,跨平台开发框架成为开发者的新宠,旨在实现一次编码、多平台部署的梦想。本文将探讨跨平台开发的优势与挑战,并分享实用的开发技巧,帮助开发者在安卓和iOS的世界中游刃有余。
|
1月前
|
缓存 前端开发 Android开发
安卓开发中的自定义视图:从零到英雄
【10月更文挑战第42天】 在安卓的世界里,自定义视图是一块画布,让开发者能够绘制出独一无二的界面体验。本文将带你走进自定义视图的大门,通过深入浅出的方式,让你从零基础到能够独立设计并实现复杂的自定义组件。我们将探索自定义视图的核心概念、实现步骤,以及如何优化你的视图以提高性能和兼容性。准备好了吗?让我们开始这段创造性的旅程吧!
27 1
|
21天前
|
搜索推荐 前端开发 API
探索安卓开发中的自定义视图:打造个性化用户界面
在安卓应用开发的广阔天地中,自定义视图是一块神奇的画布,让开发者能够突破标准控件的限制,绘制出独一无二的用户界面。本文将带你走进自定义视图的世界,从基础概念到实战技巧,逐步揭示如何在安卓平台上创建和运用自定义视图来提升用户体验。无论你是初学者还是有一定经验的开发者,这篇文章都将为你打开新的视野,让你的应用在众多同质化产品中脱颖而出。
42 19
|
1月前
|
IDE Java 开发工具
移动应用与系统:探索Android开发之旅
在这篇文章中,我们将深入探讨Android开发的各个方面,从基础知识到高级技术。我们将通过代码示例和案例分析,帮助读者更好地理解和掌握Android开发。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的信息和技巧。让我们一起开启Android开发的旅程吧!
|
21天前
|
JSON Java API
探索安卓开发:打造你的首个天气应用
在这篇技术指南中,我们将一起潜入安卓开发的海洋,学习如何从零开始构建一个简单的天气应用。通过这个实践项目,你将掌握安卓开发的核心概念、界面设计、网络编程以及数据解析等技能。无论你是初学者还是有一定基础的开发者,这篇文章都将为你提供一个清晰的路线图和实用的代码示例,帮助你在安卓开发的道路上迈出坚实的一步。让我们一起开始这段旅程,打造属于你自己的第一个安卓应用吧!
47 14
|
24天前
|
Java Linux 数据库
探索安卓开发:打造你的第一款应用
在数字时代的浪潮中,每个人都有机会成为创意的实现者。本文将带你走进安卓开发的奇妙世界,通过浅显易懂的语言和实际代码示例,引导你从零开始构建自己的第一款安卓应用。无论你是编程新手还是希望拓展技术的开发者,这篇文章都将为你打开一扇门,让你的创意和技术一起飞扬。