在RelativeLayout的缩放内容 [英] Zoom Content in a RelativeLayout

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

问题描述

我有我的应用程序的的Andr​​oid 2.1 在那里我有孩子的,我可以点击,移动和缩放根布局。一切都很好,只要根布局不缩放。

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.

我有这样的设置;

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

和我想放大的内容在我的 ZoomableRelativeLayout 。我放大我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();
}

我得到了放大的结果我想要的,但问题是,那么我想点击Childviews成 ZoomableRelativeLayout 当画布缩放。

当比例为1(无缩放)与孩子意见的互动是好的,但放大我放大,它就像触摸区域被翻译什么的,因为我无法点击他们了。

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.

我该如何解决这个问题?我试图重写 onMeasure ZoomableRelativeLayout 是这样;

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>

我可以围绕布局移动,一切都很好,但是当我放大,这是孩子们的的 ZoomableRelativeLayout的RelativeLayouts 犯规被放大。我该如何解决这个问题?我一定要继承的RelativeLayouts并重写 onMeasure() onLayout()或什么吗?

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?

推荐答案

你做你的 dispatchDraw(),实际上只是放大了的绘制什么的视图,而没有的视图本身。视图(左,上,右,下)的位置和大小还是一样,虽然你看到画布视图的绘图缩放。试试这个:放大你的 ZoomRelativeLayout 只是一点点,然后在他们的的(非缩放)位置的孩子互动,看看是否孩子们的反应。

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.

要的真正的放大视图/ ViewGroup中,你需要改变实际的观点,而不仅仅是图纸,即改造(L,T,R,B)的观点,那么 requestLayout() 无效(),但这可能击中性能。

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.

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

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