微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 在RelativeLayout中缩放内容

我有我的Android 2.1应用程序,我有一个孩子的根布局,我可以点击,移动和缩放.一切都很好,只要根布局没有缩放.

我有这样的设置;

<ZoomableRelativeLayout ...>  // Root, Moveable and zoomable
    <ImageView ....>
    <RelativeLayout ...> // Clickable, moveable and zoomable
    <RelativeLayout ...> // Clickable, moveable and zoomable
</ZoomableRelativeLayout>

我喜欢缩放ZoomableRelativeLayout中的内容.我在ZoomableRelativeLayout类中缩放我的内容;

protected void dispatchDraw(Canvas canvas) {
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    canvas.scale(mScaleFactor, mScaleFactor, mXPointCenter, mYPointCenter);
    super.dispatchDraw(canvas);
    canvas.restore();
}

我得到了我想要的缩放结果,但问题是我想在缩放画布时点击Childviews到ZoomableRelativeLayout.

当比例为1(无缩放)时,与子视图的交互很好,但随着缩放我的缩放,就像触摸区域被翻译或其他东西一样,因为我不能再点击它们了.

我该如何解决?我试图在ZoomableRelativeLayout中覆盖onMeasure,就像这样;

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension((int) (widthSize * mScaleFactor), (int) (heightSize * mScaleFactor));
}

如果有人可以帮助我!

好的,所以我改用了使用Matrix并使用画布比例来跟随;

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != View.GONE) {
            child.layout((int) mPosX, (int) mPosY, (int) (mPosX + getWidth()), (int) (mPosY + getHeight()));
        }
    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension((int) (widthSize * mScaleFactor), (int) (heightSize * mScaleFactor));
}

我还有我的设置;

<ZoomableRelativeLayout ...>  // Root, Moveable and zoomable
    <ImageView ....>
    <RelativeLayout ...> // Clickable, moveable and zoomable
    <RelativeLayout ...> // Clickable, moveable and zoomable
</ZoomableRelativeLayout>

我可以移动布局,一切都很好,但是当我缩放时,作为ZoomableRelativeLayout的子项的RelativeLayouts不会被缩放..我该如何解决这个问题?我是否必须继承RelativeLayouts并覆盖onMeasure()或onLayout()或其他任何内容

解决方法:

你在dispatchDraw()中所做的事实上只是缩放视图的绘图,而不是视图本身.视图的位置和大小(左,上,右,下)仍然相同,但您会看到画布中的视图正在缩放.试试这个:缩放ZoomrelativeLayoutjust一点点,然后在原始(非缩放)位置与孩子们互动,看看孩子们是否有所反应.

要真正缩放视图/视图组,您需要转换实际视图,而不仅仅是绘图,即转换视图的(l,t,r,b),然后requestLayout()invalidate(),但这可能会打击关于表现.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐