ETHAN IN IT

Lost in it

Android 之 View线程 run方法和draw方法执行的先后顺序

package com.ethan.activity;

import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.View;

public class MyView extends View implements Runnable {

	private static final String TAG = "view";
	
	public MyView(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i(TAG, "hello2.................");
		super.onDraw(canvas);
	}

	@Override
	public void draw(Canvas canvas) {
		//先打印出来,draw调用onDraw
		Log.i(TAG, "hello.................");
		super.draw(canvas);
	}

	@Override
	public void run() {
		Log.i(TAG, "run..........");
	}

	public void start() {
		new Thread(this).start();
	}
}


package com.ethan.activity;

import android.app.Activity;
import android.os.Bundle;

public class ViewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyView view = new MyView(getApplicationContext());
        
      setContentView(view);
      view.start();
    }
}

根据打印结果得出:先执行run() ,然后执行draw()
阅读更多
个人分类: Android
上一篇Android 之 时钟组件
下一篇Android 之 ImageView 示例
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭