简体   繁体   English

如何在Android中使线性布局可滚动和缩放?

[英]How to make a linear layout scrollable and zoomable in Android?

I have a LinearLayout in my Android app and I would like to make it scrollable in all directions. 我的Android应用程序中有一个LinearLayout ,我想使其在所有方向上均可滚动。 I simply implemented this with a HorizontalScrollView and ScrollView under which I placed my layout. 我只是使用HorizontalScrollViewScrollView来实现此目的,并在其中放置了布局。

This works great, but I would like to implement pinch-zoom as well. 这很好用,但是我也想实现捏缩放。 So I was inspired by this question: Android - zoom in/out RelativeLayout with spread/pinch 因此,我受到以下问题的启发: Android-通过展开/捏放大/缩小RelativeLayout

and I came up with the following layout: 我想出了以下布局:

<com.example.ZoomableScrollView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="0.2"
    android:scrollbars="none"
    android:background="@android:color/black">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">
        <LinearLayout
            android:id="@+id/sheet_layout"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".MainActivity">
        </LinearLayout>
    </HorizontalScrollView>
</com.example.ZoomableScrollView>

The children of the inner LinearLayout are dynamically added (they are simple TextView s inside horizontal LinearLayout s to make a simple table). 内部LinearLayout是动态添加的(它们是水平LinearLayout的简单TextView以构成一个简单的表)。

The ZoomableScrollView is quite simple and looks like this: ZoomableScrollView非常简单,看起来像这样:

public class ZoomableScrollView extends ScrollView {
    float mScaleFactor = 1.f;
    private final ScaleGestureDetector mScaleDetector;

    static final float MIN_SCALE = 1f;
    static final float MAX_SCALE = 4f;

    public ZoomableScrollView(Context context) {
        this(context, null, 0);
    }

    public ZoomableScrollView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZoomableScrollView(Context context, AttributeSet attrs,
                              int defStyle) {
        super(context, attrs, defStyle);
        mScaleDetector = new ScaleGestureDetector(context, new OnScaleListener());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.w("DEBUG", "On touch event");
        boolean retVal = mScaleDetector.onTouchEvent(event);
        return super.onTouchEvent(event) || retVal;
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        Log.w("DEBUG", String.format("Scaling with scale factor %f", mScaleFactor));
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        canvas.scale(mScaleFactor, mScaleFactor);
        super.dispatchDraw(canvas);
        canvas.restore();
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    private class OnScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
    {
        @Override
        public boolean onScale(ScaleGestureDetector detector)
        {
            mScaleFactor *= detector.getScaleFactor();

            // Don't let the object get too small or too large.
            mScaleFactor = Math.max(MIN_SCALE, Math.min(mScaleFactor, MAX_SCALE));
            Log.w("DEBUG", "OnScale: scale factor: " + mScaleFactor);
            invalidate();
            return true;
        }
    }
}

I have 2 issues with this: 我有两个问题:

  1. The zooming is not very smooth, sometimes the View will scroll when it should zoom. 缩放不是很平滑,有时视图会在缩放时滚动。 That's not such a big deal atm. 那不是很大的atm。
  2. When zooming, the LinearLayout becomes smaller and you can't scroll everywhere anymore. 缩放时, LinearLayout会变小,您无法再滚动到任何地方。 I think I understand why this is the case, but I don't know how to change that and I'm thinking that I went into the wrong direction here. 我想我理解为什么会这样,但是我不知道该如何更改,我想我在这里走错了方向。

I don't mind changing the layout completely. 我不介意完全更改布局。 In the end, I would like to have it behave a bit like a browser, where you can zoom and scroll everywhere (not as complex though). 最后,我希望它的行为有点像浏览器,您可以在其中缩放和滚动所有位置(虽然不那么复杂)。 Is there a simple solution for this ? 有一个简单的解决方案吗?

As for two dimensional scrollView I had used this one . 至于二维scrollView,我已经使用了这个 Maybe you should try to add pinch for it. 也许您应该尝试为此添加一点点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM