Viewextends Object
AnalogClock,ImageView,KeyboardView,MediaRouteButton,ProgressBar, Space, SurfaceView, TextView, TextureView, ViewGroup, ViewStub
implements Drawable.Callback KeyEvent.Callback AccessibilityEventSource
java.lang.Object
↳
Android.view.View
Known Direct Subclasses(直接子类,SurfaceView是View的子类)
Known Indirect Subclasses(间接子类)
AbsListView,AbsSeekBar,AbsSpinner,AbsoluteLayout,AdapterView<T extends Adapter>,AdapterViewAnimator,AdapterViewFlipper,AppWidgetHostView, AutoCompleteTextView, Button, CalendarView, CheckBox, CheckedTextView, Chronometer, and 53 others.
Class Overview
This class represents the basic building block for user interface components. A View occupies a rectangular area on the screen and is responsible for drawing and event handling. View is the base class forwidgets, which are used to create interactive UI components (buttons, text fields, etc.). TheViewGroup subclass is the base class forlayouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.
View类为用户界面提供了最基础的组件,View类组件负责更换屏幕与处理事件。同时,View类也是widgets类的基础类,widgets类可以创建基础的UI组件,如Bottons、Textview等等。View类的其中一个直接子类ViewGroup是layous的基础类,layous是用来装载View或者其他的ViewGrous的,并且可以定义这些装载内容的特性。
2、
从上述的Overview可知,SurfaceView是继承于View类的,(GLSurfaceView是继承于SurfaceView的)。关于SurfaceView的详细可以参看之前的文章
Android更新屏幕主要有两种方式,继承SurfaceView实现SurfaceHolder.callback接口来实现屏幕的更新。
或者直接继承View类,复写OnDraw方法实现更新屏幕。
事实上,两种是用本质的区别的。
3、View与SurfaceView更新屏幕的区别
对于SurfaceView更新屏幕,是在非UI线程(主线程)中更新的。而对于View,则是在UI的主线程中更新画面。
那在UI的主线程中更新画面很容易造成主线程的堵塞,造成程序的长时间无响应,当主UI线程超过5秒钟没有响应用户的操作,Android系统会提示是否关闭应用程序。
当使用SurfaceView 来更新画面的话,就不必担心堵塞主UI线程这个问题了。但是这也带来了另外一个问题,线程的同步性。
所以当更新操作说花的时间较长,而且数据量较大的话,一般采用SurfaceView方式更新屏幕,而少用View。
4、Demo程序
/* * author: conowen * e-mail: conowen@hotmail.com * date : 2012.8.8 */ package com.conowen.viewtestdemo; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.view.View; public class MyView extends View { private int counter; public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); /* synchronized (this) { try { wait(10 * 1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } */ // 设定Canvas对象的背景颜色 canvas.drawColor(Color.YELLOW - counter); // 创建画笔 Paint p = new Paint(); // 设置画笔颜色 p.setColor(Color.RED); // 设置文字大小 p.setTextSize(40); // 消除锯齿 p.setFlags(Paint.ANTI_ALIAS_FLAG); // 在canvas上绘制rect canvas.drawArc(new RectF(100, 50, 400, 350), 0, counter, true, p); if (counter == 400) { counter = 0; } canvas.drawText("counter = " + (counter++), 500, 200, p); // 重绘, 再一次执行onDraw 程序 invalidate(); } }
效果图:打开下面的代码,测试堵塞主UI线程(长按屏幕5秒以上)就会出现如下的图。