Android 在相对视图中缩放内容

Android 在相对视图中缩放内容,android,zooming,scale,android-relativelayout,Android,Zooming,Scale,Android Relativelayout,我有我的Android 2.1应用程序,其中我有一个根布局和儿童的,我可以点击,移动和缩放。只要不缩放根布局,一切正常 我有一个这样的设置 <ZoomableRelativeLayout ...> // Root, Moveable and zoomable <ImageView ....> <RelativeLayout ...> // Clickable, moveable and zoomable <RelativeLay

我有我的Android 2.1应用程序,其中我有一个根布局和儿童的,我可以点击,移动和缩放。只要不缩放根布局,一切正常

我有一个这样的设置

<ZoomableRelativeLayout ...>  // Root, Moveable and zoomable
    <ImageView ....>
    <RelativeLayout ...> // Clickable, moveable and zoomable
    <RelativeLayout ...> // Clickable, moveable and zoomable
</ZoomableRelativeLayout>
@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));
}
我得到了想要的缩放结果,但问题是,当画布缩放时,我想单击子视图以缩放相关布局

当比例为1(无缩放)时,与子视图的交互很好,但随着我缩放,它就像是平移了触摸区域之类,因为我无法再单击它们

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

<ZoomableRelativeLayout ...>  // Root, Moveable and zoomable
    <ImageView ....>
    <RelativeLayout ...> // Clickable, moveable and zoomable
    <RelativeLayout ...> // Clickable, moveable and zoomable
</ZoomableRelativeLayout>
@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));
}
如果有人能帮助我,请帮助我


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

@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));
}
@覆盖
仅受保护的void布局(布尔值已更改、int l、int t、int r、int b){
最终整数计数=getChildCount();
for(int i=0;i
我仍然有我的设置

<ZoomableRelativeLayout ...>  // Root, Moveable and zoomable
    <ImageView ....>
    <RelativeLayout ...> // Clickable, moveable and zoomable
    <RelativeLayout ...> // Clickable, moveable and zoomable
</ZoomableRelativeLayout>
//根目录,可移动和可缩放
//可点击、可移动和可缩放
//可点击、可移动和可缩放

我可以在布局中移动,一切都很好,但当我缩放时,ZoomableRelativeLayout的子级相对位置不会被缩放。。我怎样才能解决这个问题?我是否必须对RelativeLayouts进行子类化并覆盖onMeasure()或onLayout()或其他内容?

您在
dispatchDraw()中所做的实际上只是缩放视图的图形,而不是视图本身。视图的位置和大小(左、上、右、下)仍然相同,尽管您看到画布中的视图图形正在缩放。尝试以下操作:只放大一点
ZoomRelativeLayout
,然后在原始(非缩放)位置与子对象交互,并查看子对象是否有反应


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

嗨,Ricky,是的,我可以在孩子们的原始位置与他们互动。我是否必须以某种方式转换MotionEvent的位置?我还尝试了另一种方法,在onTouchEvent中构建一个矩阵(平移和缩放),在绘图中只构建canvas.setMatrix。如何以正确的方式传输(l,t,r,b)?已经搜索了一些例子,但没有找到任何有用的:(在
onTouchEvent()
中,您需要根据比例因子计算新的(l,t,r,b)。然后用新的维度布局子项。您不再需要设置
canvas.setMatrix()
,因为您现在正在缩放/操作实际视图,而不是绘图画布。您建议如何转换视图的l、t、r、b?我也在使用dispatchDraw方法。@RickyLee您能给我们一个关于如何实现这一点的片段吗?我在这里有同样的问题。我正在使用这个ZoomView: