Android:View

View

View's Main Draw Process:

1. onMeasure()                          Called to determine the size requirements for this view and all its children.
2. onLayout()                             Called when this view should assign a size and postion to all of its children.       
3. onDraw()                               Called when this view render its content.          
4. dispatchDraw()                      Called by draw to draw its child view.

The following source code for a test:

package com.slowalker.viewdemo;

import android.os.Bundle;
import android.os.Parcelable;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    private static final String TAG = MainActivity.class.getSimpleName();
    
    private static final boolean DEBUG = true;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DEBUG) {
            Log.d(TAG, "onCreate");
        }
        setContentView(new SView(this), new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        
    }
    
    private static class SView extends View {

        private static final String TAG = SView.class.getSimpleName();
        
        public SView(Context context) {
            super(context);
        }

        @Override
        protected void dispatchDraw(Canvas canvas) {
            if (DEBUG) {
                Log.d(TAG, "dispatchDraw");
            }
            super.dispatchDraw(canvas);
            
        }

        @Override
        protected void onDraw(Canvas canvas) {
            if (DEBUG) {
                Log.d(TAG, "onDraw");
            }
            super.onDraw(canvas);
        }

        @Override
        protected void onLayout(boolean changed, int left, int top, int right,
                int bottom) {
            if (DEBUG) {
                Log.d(TAG, "onLayout");
            }
            super.onLayout(changed, left, top, right, bottom);
        }

        @Override
        protected void onFinishInflate() {
            if (DEBUG) {
                Log.d(TAG, "onFinishInflate");
            }
            super.onFinishInflate();
        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            if (DEBUG) {
                Log.d(TAG, "onMeasure");
            }
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
        
    }

}

 

 

posted @ 2013-10-31 12:43  slowalker  阅读(163)  评论(0编辑  收藏  举报