AndroidÏÔʾϵͳ֮ViewÓëSurfaceView¸üÐÂÆÁÄ»µÄÇø±ð
/********************************************************************************************
* author£ºconowen@´óÖÓ
* E-mail£ºconowen@hotmail.com
* http://blog.csdn.net/conowen
* ×¢£º±¾ÎÄΪԴ´£¬½ö×÷Ϊѧϰ½»Á÷ʹÓã¬×ªÔØÇë±êÃ÷×÷Õß¼°³ö´¦¡£
********************************************************************************************/
1¡¢View
Viewextends Object
implements Drawable.Callback KeyEvent.Callback AccessibilityEventSourceKnown Direct Subclasses£¨Ö±½Ó×ÓÀ࣬SurfaceViewÊÇViewµÄ×ÓÀࣩKnown Indirect Subclasses£¨¼ä½Ó×ÓÀࣩ
´ò¿ªÏÂÃæµÄ´úÂ룬²âÊÔ¶ÂÈûÖ÷UIỊ̈߳¨³¤°´ÆÁÄ»5ÃëÒÔÉÏ£©¾Í»á³öÏÖÈçϵÄͼ¡£
×¢Ò⣺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, callpostInvalidate()
.
ÈçÏÂÃæµÄ´úÂëËùʾ¡£ÕâÑùµÄ»°£¬¾Í²»±Øµ£ÐÄÖ÷UIÏ̱߳»¶ÂÈûÁË¡£
/* * 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}@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);mThread = new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubtry {mThread.sleep(10 * 1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.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ÉÏ»æÖÆrectcanvas.drawArc(new RectF(100, 50, 400, 350), 0, counter, true, p);if (counter == 400) {counter = 0;}canvas.drawText("counter = " + (counter++), 500, 200, p);}}