查找 parent 视图的高度

Find height of parent view

我正在创建一个自定义图像视图,我正在尝试找到 parent 的高度。我所知道的关于 parent 的唯一细节是它可能会滚动。我需要 parent 高度的原因是因为我试图找出图像视图在屏幕上的位置。我已经制作了一个可以准确计算其位置的方程式,但它仅在我手动输入 parents 高度时有效。有什么方法可以检索此信息,或者有其他方法每次更改时都可以获取我的图像视图在屏幕上的位置吗?

试试这个方法

public class MyImageView extends ImageView {


    int parentHeight;
    int parentWidth;

    public MyImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public MyImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyImageView(Context context) {
        super(context);
    }





    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if(((View)this.getParent()).getMeasuredWidth()!=0){
            parentHeight = ((View)this.getParent()).getMeasuredHeight();
            parentWidth = ((View)this.getParent()).getMeasuredWidth();
        }

    }
}