什么时候调用View.onMeasure()?

Gre*_*ash 23 java android view android-activity onmeasure

什么时候

View.onMeasure(int widthMeasureSpec, int heightMeasureSpec)
Run Code Online (Sandbox Code Playgroud)

叫什么名字?我有一个Activity,需要在调用onMeasure后执行一个动作.

我的问题与此处发布的未回答的问题相同.在查看文档指出onMeasure当被称为requestLayout()时,它认为是可以没有它的当前范围内不再适合被称为,这显然是对自身的视图调用.

但是,这并没有告诉我何时我的活动可以假设我的视图已被测量.我已经使用此代码ImageView扩展为TouchImageView.这里建议我应该使用onMeasure方法来缩放我的图像.我希望在测量ImageView后更新TextView的值,以显示图像缩放的百分比.

Sur*_*gch 19

您可以在中获取自定义视图的测量值onSizeChanged.

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    // use the provided width and height for whatever you need
}
Run Code Online (Sandbox Code Playgroud)

说明

创建视图时,这是调用以下方法的顺序:

  1. 构造函数
    • CustomView(Context context) (如果以编程方式创建)
    • CustomView(Context context, AttributeSet attrs) (如果从xml创建)
  2. onFinishInflate (假设您使用了xml构造函数)
  3. onAttachedToWindow
  4. onMeasure
  5. onSizeChanged
  6. onLayout
  7. onDraw

您最早可以获得视图的测量结果onMeasure.在此之前,宽度和高度是0.但是,您应该做的唯一事情onMeasure是确定视图的大小.当视图告诉父母它想要多大但父母正在确定实际的最终大小时,会多次调用此方法.(请参阅此答案,了解如何onMeasure使用.)

如果你想实际使用测量的尺寸,那么最早的地方就是onSizeChanged.每当创建视图时都会调用它,因为大小会从大小变为0大小.

您也可以使用onLayout,但据我了解,它onLayout可以用于自定义自定义视图的任何子项的布局方式.onSizeChanged例如,如果requestLayout()在实际没有实际更改大小时调用,它也可能会被更频繁地调用.

您还可以onDraw使用getMeasuredWidth()和访问大小getMeasuredHeight().但是,如果您使用它们进行任何繁重的计算,最好事先做好.一般来说,尽量保持尽可能多,onDraw因为它可能被多次调用.(只要invalidate()被调用就会被调用.)

你自己看

如果您不相信我,您可以在下面的自定义视图中查看事件的顺序.这是输出:

XML constructor called, measured size: (0, 0)
onFinishInflate called, measured size: (0, 0)
onAttachedToWindow called, measured size: (0, 0)
onMeasure called, measured size: (350, 1859)
onMeasure called, measured size: (350, 350)
onMeasure called, measured size: (350, 2112)
onMeasure called, measured size: (350, 350)
onSizeChanged called, measured size: (350, 350)
onLayout called, measured size: (350, 350)
onDraw called, measured size: (350, 350)
Run Code Online (Sandbox Code Playgroud)

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.viewlifecycle.CustomView
        android:id="@+id/customView"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@color/colorAccent"/>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

CustomView.java

public class CustomView extends View {

    private void printLogInfo(String methodName) {
        Log.i("TAG", methodName + " called, measured size: (" + getMeasuredWidth() + ", " + getMeasuredHeight() + ")");
    }

    // constructors

    public CustomView(Context context) {
        super(context);
        printLogInfo("Programmatic constructor");
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        printLogInfo("XML constructor");
    }

    // lifecycle methods

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        printLogInfo("onFinishInflate");
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        printLogInfo("onAttachedToWindow");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        printLogInfo("onMeasure");
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        printLogInfo("onSizeChanged");
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        printLogInfo("onLayout");
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        printLogInfo("onDraw");
    }
}
Run Code Online (Sandbox Code Playgroud)

进一步阅读


Mat*_*ieu 14

当父View需要计算布局时调用onMeasure.通常,onMeasure可能会被调用多次,具体取决于不同的孩子和他们的布局参数.

调用onMeasure时执行某些操作的最佳方法是(我认为)创建自己的控件,扩展ImageView并覆盖onMeasure(只需调用super.onMeasure并执行其他任何您想要执行的操作).

如果你这样做,请记住,在测量上可能会使用不同的参数多次调用,因此测量的内容可能不是实际显示的最终宽度和高度.