本文介绍了什么时候调用 View.onMeasure()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么时候

View.onMeasure(int widthMeasureSpec, int heightMeasureSpec)

打电话?我有一个 Activity 需要在 已被调用.

called? I have an Activity that needs to perform an action after onMeasure has been called.

我的问题与此处未回答的问题.View 文档指出 onMeasure 在 被调用,当它认为可以不再适合其当前范围.

My question is the same as the unanswered question posted here. The View documentation states that onMeasure is called when requestLayout() is called, which is apparently called by a view on itself when it believes that is can no longer fit within its current bounds.

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

However, this doesn't tell me when my activity can assume that my View has been measured. I've used this code to extend ImageView to form TouchImageView. It was suggested here that I should use the onMeasure method to scale my image. I wish to update the value of a TextView after the ImageView has been measured in order to display the percentage by which the image has been scaled.

推荐答案

onMeasure 在父 View 需要计算布局时调用.通常,根据存在的不同子项及其布局参数,可能会多次调用 onMeasure.

onMeasure is called when the parent View needs to calculate the layout. Typically, onMeasure may be called several times depending on the different children present and their layout parameters.

onMeasure 被称为是(我认为)创建自己的控件,扩展 ImageView 并覆盖 onMeasure(只需调用 super.onMeasure 并执行您想做的任何其他操作).

The best way to do something when onMeasure is called is (I think) to create your own control, extend the ImageView and override onMeasure (just call super.onMeasure and do whatever else you would like to do).

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

If you do that, just keep in mind that on Measure may be called several times with different parameters, so whatever is measured may not be the final width and height of what will be actually displayed.

这篇关于什么时候调用 View.onMeasure()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 21:57