在相对布局中缩放内容 [英] Zoom Content in a RelativeLayout

查看:21
本文介绍了在相对布局中缩放内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有这样的设置;

//根,可移动和可缩放<ImageView ....><RelativeLayout ...>//可点击、可移动和可缩放<RelativeLayout ...>//可点击、可移动和可缩放</ZoomableRelativeLayout>

而且我喜欢在我的 ZoomableRelativeLayout 中缩放内容.我在 ZoomableRelativeLayout 类中像这样缩放我的内容;

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

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

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

我该如何解决这个问题?我试图像这样覆盖 ZoomableRelativeLayout 中的 onMeasure

@Overrideprotected 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));}

如果有人可以请帮助我!

<小时>

好的,所以我从使用矩阵和使用画布比例改为跟随;

@Overrideprotected void onLayout(boolean 改变,int l,int t,int r,int b) {final int count = getChildCount();for (int i = 0; i < count; i++) {最终视图子 = getChildAt(i);如果(child.getVisibility()!= View.GONE){child.layout((int) mPosX, (int) mPosY, (int) (mPosX + getWidth()), (int) (mPosY + getHeight()));}}}@覆盖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));}

我还有我的设置;

//根,可移动和可缩放<ImageView ....><RelativeLayout ...>//可点击、可移动和可缩放<RelativeLayout ...>//可点击、可移动和可缩放</ZoomableRelativeLayout>

我可以在布局中移动,一切都很好,但是当我缩放时,ZoomableRelativeLayout 的孩子的 RelativeLayouts 没有被缩放.. 我该如何解决这个问题?我是否必须继承相对布局并覆盖 onMeasure()onLayout() 或其他任何东西?

解决方案

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

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

I have my application for Android 2.1 where I have a root layout with children's that I can click, move and zoom. Everything is fine, as long as the root layout is not zoomed.

I have a setup like this;

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

And I like to zoom the content in my ZoomableRelativeLayout. I zoom my content like this in my ZoomableRelativeLayout class;

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

I get the Zoom result I want, but the problem is then I want to click on the Childviews to ZoomableRelativeLayout when canvas is scaled..

When scale is 1 (no zoom) the interaction with child views is fine, but as zoom as I zoom, its just like the touch area is translated or something, because I can't click them anymore.

How do I fix this? I tried to override onMeasure in ZoomableRelativeLayout like this;

@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));
}

Please help me if anyone can!


Ok, so I changed from using Matrix and using canvas scale to following;

@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));
}

I still have my setup;

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

I can move around the layout and everything is fine, but when I zoom, the RelativeLayouts that is children's of ZoomableRelativeLayout doesnt gets zoomed.. How can I fix this? Do I have to subclass the RelativeLayouts and override onMeasure() or onLayout() or anything?

解决方案

What you're doing in your dispatchDraw(), is actually just zooming the drawing of the view, and not the view itself. The location and size of the view (left, top, right, bottom) is still the same, although you see the view's drawing in the canvas is zooming. Try this: zoom your ZoomRelativeLayoutjust a little bit, and then interact with the children in their original (non-zoom) location, and see whether the children reacts.

To really zoom a view/viewgroup, you need to transform the actual view, and not only the drawing, i.e. transform the (l,t,r,b) of the view, then requestLayout() invalidate(), but this might hit on the performance.

这篇关于在相对布局中缩放内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆