IT学习联盟

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
IT学习联盟 门户 文章 android 查看内容

Android显示系统之View与SurfaceView更新屏幕的区别

2012-8-17 20:13| 发布者: admin| 查看: 1148| 评论: 0

/********************************************************************************************
 * author:conowen@大钟                                                                                                                         
 * E-mail:conowen@hotmail.com                                                                                                            
 * http://blog.csdn.net/conowen                                                                                                             
 * 注:本文为原创,仅作为学习交流使用,转载请标明作者及出处。     
 ********************************************************************************************/

1、View

View
extends Object
implements 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://blog.csdn.net/conowen/article/details/7821409
Android更新屏幕主要有两种方式,继承SurfaceView实现SurfaceHolder.callback接口来实现屏幕的更新。
或者直接继承View类,复写OnDraw方法实现更新屏幕。
事实上,两种是用本质的区别的。

3、View与SurfaceView更新屏幕的区别

对于SurfaceView更新屏幕,是在非UI线程(主线程)中更新的。而对于View,则是在UI的主线程中更新画面。
那在UI的主线程中更新画面很容易造成主线程的堵塞,造成程序的长时间无响应,当主UI线程超过5秒钟没有响应用户的操作,Android系统会提示是否关闭应用程序。

当使用SurfaceView 来更新画面的话,就不必担心堵塞主UI线程这个问题了。但是这也带来了另外一个问题,线程的同步性。

所以当更新操作说花的时间较长,而且数据量较大的话,一般采用SurfaceView方式更新屏幕,而少用View。

4、Demo程序
[java] 
/*
 * 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秒以上)就会出现如下的图。
[java] view plaincopy
synchronized (this) { 
            try { 
                wait(10 * 1000); 
            } catch (InterruptedException e) { 
                // TODO Auto-generated catch block 
                e.printStackTrace(); 
            } 
        } 

 


注意:
     onDraw方法是运行于主UI线程中的,如果你在onDraw中执行invalidate()方法去更新屏幕,是可以的。但是你既要继承View而且要不希望堵塞主UI线程的话,可以另外新建线程,然后在线程中执行postInvalidate()方法去更新屏幕。也就是说invalidate()方法只能在主UI线程中被调用,postInvalidate()方法只能在非主UI线程中被调用。否则会出现如下error
08-08 15:33:34.587: E/AndroidRuntime(4995): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
这两个方法只是再次调用onDraw方法而已。
Invalidate the whole view. If the view is visible, onDraw(android.graphics.Canvas) will be called at some point in the future. This must be called from a UI thread. To call from a non-UI thread, call postInvalidate().

如下面的代码所示。这样的话,就不必担心主UI线程被堵塞了。
[java]
/*
 * author: conowen
 * e-mail: conowen@hotmail.com
 * date  :  2012.8.4
 */ 
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; 
    private Thread mThread; 
 
    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); 
 
        mThread = new Thread(new Runnable() { 
 
            @Override 
            public void run() { 
                // TODO Auto-generated method stub 
 
                try { 
                    mThread.sleep(10 * 1000); 
                } catch (InterruptedException e) { 
                    // TODO Auto-generated catch block 
                    e.printStackTrace(); 
                } 
                // 重绘, 再一次执行onDraw 程序 
                invalidate(); 
                postInvalidate(); 
            } 
        }); 
        mThread.start(); 
 
        // 设定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; www.2cto.com
        } 
 
        canvas.drawText("counter = " + (counter++), 500, 200, p); 
         
 
    } 
 

Archiver|IT学习联盟| 网站地图

Powered by Discuz! X2.5© 2001-2010 世界学习室 大伟制作

GMT+8, 2018-8-16 04:17 , Processed in 0.104867 second(s), 12 queries .