关闭

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

2881人阅读 评论(0) 收藏 举报
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()
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:614801次
    • 积分:7130
    • 等级:
    • 排名:第3143名
    • 原创:183篇
    • 转载:80篇
    • 译文:0篇
    • 评论:123条
    文章分类
    最新评论