android如何放大scrollview布局

时间:2014-12-10 22:46:35

标签: android zoom

我有一个非常简单的问题,如何放大包含相对布局的滚动视图,无论如何,捏或按钮。什么能让我入手?

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分: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