ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 在RelativeLayout中缩放内容

2019-09-30 08:35:54  阅读:188  来源: 互联网

标签:android zoom scale 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(),但这可能会打击关于表现.

标签:android,zoom,scale,relativelayout
来源: https://codeday.me/bug/20190930/1835392.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有