view的执行过程,各种方法的调用先后顺序

view的执行过程,各种方法的调用先后顺序

这里只是测试各个方法的执行流程,有些时候就是这些小基础是非常重要的,下了测试代码如下:

package com.world.hello.viewpagerindictor.view;

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

/**
 * 自定义View的常用方法的调用先后顺序
 * Created by chengguo on 2016/3/18.
 */
public class CustomView extends View {

    /**
     * 用java代码创建CustomView时调用此构成方法
     *
     * @param context
     */
    public CustomView(Context context) {
        super(context);
        Log.i("tag", "----  public CoustomView(Context context) ----");
    }

    /**
     * 用布局文件xml创建CustomView时调用此构成方法
     *
     * @param context
     */
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.i("tag", "----  public CoustomView(Context context, AttributeSet attrs) ----");
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        Log.i("tag", "----  public CoustomView(Context context, AttributeSet attrs, int defStyleAttr) ----");
    }


    /**
     * 使用布局文件XML创建CustomView时,在xml文件加载完成后调用这个方法
     */
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        Log.i("tag", "----  onFinishInflate() ----");
    }

    /**
     * CustomView的大小发生改变时调用这个方法
     *
     * @param w
     * @param h
     * @param oldw
     * @param oldh
     */
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i("tag", "----  onSizeChanged = " + " w = " + w + "  h = " + h + "  oldW = " + oldw + "  oldH = " + oldw);
    }

    /**
     * 在画布上面绘制
     *
     * @param canvas
     */
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        Log.i("tag", "----  dispatchDraw = ");
    }

    /**
     * 绘制CustomView时调用
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i("tag", "----  onDraw ----");
    }

    /**
     * 测量CustomView的大小
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i("tag", "----  onMeasure ----");
    }

    /**
     * 将CustomView放置到父容器中去
     *
     * @param changed
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        Log.i("tag", "----  onLayout ----");
    }


    /**
     * 将CustomView依附到Window中
     */
    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        Log.i("tag", "----  onAttachedToWindow ----");
    }

    /**
     * 当手机屏幕从横屏和竖屏 相互转化时调用
     *
     * @param newConfig
     */
    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.i("tag", "----  onConfigurationChanged ----");
    }

}

      下面是Log信息,如下

03-18 11:32:30.198 14844-14844/? I/tag: ----  public CoustomView(Context context, AttributeSet attrs) ----
03-18 11:32:30.198 14844-14844/? I/tag: ----  onFinishInflate() ----
03-18 11:32:30.227 14844-14844/? I/tag: ----  onAttachedToWindow ----
03-18 11:32:30.228 14844-14844/? I/tag: ----  onMeasure ----
03-18 11:32:30.294 14844-14844/? I/tag: ----  onMeasure ----
03-18 11:32:30.295 14844-14844/? I/tag: ----  onSizeChanged =  w = 1080  h = 135  oldW = 0  oldH = 0
03-18 11:32:30.295 14844-14844/? I/tag: ----  onLayout ----
03-18 11:32:30.310 14844-14844/? I/tag: ----  onMeasure ----
03-18 11:32:30.310 14844-14844/? I/tag: ----  onLayout ----
03-18 11:32:30.311 14844-14844/? I/tag: ----  onDraw ----
03-18 11:32:30.311 14844-14844/? I/tag: ----  dispatchDraw = 


        经过log测试,就知道了,View的方法的执行过程,那么在自定义View的时候,就会在正确的方法中,进行操作。
        希望对大家有所帮助


### OpenGL 中变换矩阵应用顺序 在 OpenGL 中,当执行一系列变换操作时,这些变换会按照特定的顺序应用于顶点数据。具体来说,在 `GL_MODELVIEW` 矩阵模式下进行的操作会影响最终渲染的对象位置、方向和大小。 #### 应用顺序解释 所有的变换函数(如旋转、平移、缩放)都会修改当前的活动矩阵堆栈顶部的内容。由于矩阵乘法不是交换性的,因此变换的先后顺序至关重要。一般情况下: - **先缩放**:这一步骤调整物体的比例因子。 - **再旋转**:围绕指定轴改变物体的角度。 - **最后平移**:移动物体至所需的位置[^2]。 这种顺序确保了物体首先被适当地拉伸或压缩,接着绕原点正确地转动,之后才沿坐标系的方向位移到目标地点。如果颠倒这个流程,则可能导致意外的结果,比如不希望有的剪裁效果或其他变形现象。 #### 示例代码展示 下面是一个简单的例子来说明如何设置并应用上述提到的不同类型的转换: ```cpp // 假设已经设置了正确的投影矩阵和观察矩阵... glMatrixMode(GL_MODELVIEW); // 切换回模型视图矩阵 glLoadIdentity(); // 初始化为单位矩阵 // 开始构建局部空间到世界空间的变化链... // Step 1: Scale the object first. float scaleFactors[] = { scaleX, scaleY, scaleZ }; glScalef(scaleFactors[0], scaleFactors[1], scaleFactors[2]); // Step 2: Rotate around some axis next. GLfloat angleInDegrees = ...; GLfloat rotationAxis[] = {...}; // e.g., X-axis as {1.0, 0.0, 0.0} glRotatef(angleInDegrees, rotationAxis[0], rotationAxis[1], rotationAxis[2]); // Step 3: Translate to final position lastly. GLfloat translationVector[] = { posX, posY, posZ }; glTranslatef(translationVector[0], translationVector[1], translationVector[2]); ``` 这段程序片段展示了怎样通过调用相应的 OpenGL 函数依次完成缩放、旋转和平移的动作。值得注意的是,这里使用的固定管线命令集适用于较旧版本的 OpenGL;现代 OpenGL 实践更倾向于利用着色器语言(如 GLSL),并通过统一变量传递变换参数给 GPU 进行处理[^4]。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值