View
onDraw方法中:
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Log.d(TAG, "onDraw: "); mHandler.sendEmptyMessage(0x1234); Log.d(TAG, "onDraw: 结束"); } private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what == 0x1234) { invalidate(); } } };
View只有在初始绘画时会调用一次onDraw进行绘画,所以如果需要一直调用onDraw函数,需要使用invalidate()来对View进行重新绘画。
初始化完毕的监听器,一些操作可能要用到,比如说获取控件的高度,在初始化完毕之前获取到时0
tv_travel.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // TODO Auto-generated method stubint[] position = new int[2]; tv_travel.getLocationOnScreen(position); } });