【发布时间】:2012-02-29 20:02:50
【问题描述】:
我有一个适用于 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();
}
我得到了我想要的缩放结果,但问题是我想在缩放画布时单击子视图以 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));
}
如果有人可以请帮助我!
好的,所以我从使用矩阵和使用画布比例更改为跟随;
@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() 或其他任何东西?
【问题讨论】:
标签: android zooming scale android-relativelayout