android如何放大滚动视图布局

问题描述 投票:0回答:2

我有一个非常简单的问题,如何放大或缩小显示包含相对布局的滚动视图。有什么让我入门的吗?

android zoom
2个回答
0
投票

您可以尝试使用dispatchTouchEvent()

示例代码:

  // step 1: add some instance
private float mScale = 1f;
private ScaleGestureDetector mScaleDetector;
GestureDetector gestureDetector;

//step 2: create instance from GestureDetector(this step sholude be place into onCreate())
gestureDetector = new GestureDetector(this, new GestureListener());

// animation for scalling
mScaleDetector = new ScaleGestureDetector(this, new ScaleGestureDetector.SimpleOnScaleGestureListener() 
    {                                   
        @Override
        public boolean onScale(ScaleGestureDetector detector) 
        {
            float scale = 1 - detector.getScaleFactor();

            float prevScale = mScale;
            mScale += scale;

            if (mScale < 0.1f) // Minimum scale condition:
                mScale = 0.1f;

            if (mScale > 10f) // Maximum scale condition:
                mScale = 10f;
            ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(), detector.getFocusY());
            scaleAnimation.setDuration(0);
            scaleAnimation.setFillAfter(true);
            ScrollView layout =(ScrollView) findViewById(R.id.scrollViewZoom);
            layout.startAnimation(scaleAnimation);
            return true;
        }
    });


// step 3: override dispatchTouchEvent()
 @Override
 public boolean dispatchTouchEvent(MotionEvent event) {
    super.dispatchTouchEvent(event);
    mScaleDetector.onTouchEvent(event);
    gestureDetector.onTouchEvent(event);
    return gestureDetector.onTouchEvent(event);
 }

//step 4: add private class GestureListener

private class GestureListener extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }
    // event when double tap occurs
    @Override
    public boolean onDoubleTap(MotionEvent e) {
        // double tap fired.
        return true;
    }
}

0
投票

使用TwoDScrollView并添加ScaleGestureDetector,然后使用

ObjectAnimator anim= ObjectAnimator.ofPropertyValuesHolder(childLayout,
PropertyValuesHolder.ofFloat(View.SCALE_X,prevScale,mScale),
PropertyValuesHolder.ofFloat(View.SCALE_Y,prevScale,mScale));
anim.setDuration(0);
anim.start();

设置子透视图并请求子布局以刷新父项。在mScale TwoDScrollView中使用相同的onTouchEvent来计算availableToScroll

© www.soinside.com 2019 - 2024. All rights reserved.