1、View
View
extends
Objectimplements
Drawable.Callback KeyEvent.Callback AccessibilityEventSource
java.lang.Object
↳
Android.view.View
Known Direct Subclasses(直接子類,SurfaceView是View的子類)
AnalogClock,
ImageView,
KeyboardView,
MediaRouteButton,
ProgressBar,
Space,
SurfaceView,
TextView,
TextureView,
ViewGroup,
ViewStub
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的詳細可以參看之前的文章 http://www.linuxidc.com/Linux/2012-08/67619.htm
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: [email protected]
- * 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秒以上)就會出現如下的圖。
- synchronized (this) {
- try {
- wait(10 * 1000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }