如何在视图中放大视图时滚动视图

时间:2016-10-21 14:14:57

标签: scale gesture android-scrollview horizontalscrollview

活动布局:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:id="@+id/vertical">
    <HorizontalScrollView
        android:id="@+id/horizontal"
        android:layout_width="match_parent" 
        android:layout_height="wrap_content">
        <RelativeLayout 
            android:id="@+id/linearparent"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:gravity="center">
            <ImageView 
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/tulips"/>
            </RelativeLayout>
    </HorizontalScrollView>
</ScrollView>

Activity.java

    public class MainActivity extends Activity {


    enter code here
    ScaleGestureDetector mScaleDetector;
    float mScaleFactor = 1.f;
    RelativeLayout parentLayout; 
    ImageView image;
    HorizontalScrollView horizontal;
    ScrollView vertical;
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main_2);
        mScaleDetector = new ScaleGestureDetector(this, new ScaleListener());
        parentLayout = (RelativeLayout)findViewById(R.id.linearparent);
        horizontal = (HorizontalScrollView)findViewById(R.id.horizontal);
        image = (ImageView)findViewById(R.id.imageView1);
        vertical = (ScrollView)findViewById(R.id.vertical);
    }

    public class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener 
        {
            @Override
            public boolean onScale(ScaleGestureDetector detector) {

                mScaleFactor *= detector.getScaleFactor();
                mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
                image.setScaleX(mScaleFactor);
                image.setScaleY(mScaleFactor);

                ViewGroup vg = (ViewGroup) findViewById (R.id.horizontal);
                vg.requestLayout();
                return true;
            }
        }

     @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
         if(ev.getPointerCount() == 2){
             mScaleDetector.onTouchEvent(ev);
             return true;
         }else{
             Log.d("TOUCH", "dispatchTouchEvent to horizonal");
            horizontal.dispatchTouchEvent(ev);
            vertical.dispatchTouchEvent(ev);

             return true;
         }
    }

}

如果图像视图的图像源尺寸较大,则默认情况下它也支持滚动和缩放。但是如果图像与设备屏幕相比较小,那么在缩放后我无法滚动。这最初不会有任何滚动支持,因为内容不大,并且在缩放后内容也不会滚动。

1 个答案:

答案 0 :(得分:0)

我们缩放视图后滚动视图的方法是更改​​轴心点。我们可以通过根据用户手势改变枢轴点来实现滚动效果。请在下面找到代码段:

public boolean onTouchEvent(MotionEvent event){

    if(event.getActionMasked() == MotionEvent.ACTION_DOWN){
        Log.d("EVENT", "ACTION_DOWN");
        startX = event.getX();
        startY = event.getY();
    }
    else if(event.getActionMasked() == MotionEvent.ACTION_UP){
        Log.d("EVENT", "ACTION_UP");
        return true;

    }
    else if(event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN ){
        Log.d("EVENT", "ACTION_POINTER_DOWN");
        startX = event.getX();
        startY = event.getY();
        block_action_move = true;
        enable_zoom = true;

    }
    else if(event.getActionMasked() == MotionEvent.ACTION_POINTER_UP){
        Log.d("EVENT", "ACTION_POINTER_UP");
        block_action_move = false;
        enable_zoom = false;
        return true;

    }
    else if(event.getActionMasked() == MotionEvent.ACTION_MOVE ){
        Log.d("EVENT", "ACTION_MOVE");
        if(enable_zoom){
            mScaleDetector.onTouchEvent(event);
        }
        if(!block_action_move){
            Log.d("EVENT", "SWIPE_LEFT");
            if(startX >event.getX() && (startX - event.getX()) > SWIPE_MIN_DISTANCE){
                int[] screen = new int[4];
                Rect rect = new Rect();
                mEpub3RelativeLayout.getLocalVisibleRect(rect);
                mEpub3RelativeLayout.getLocationOnScreen(screen);
                if(screen[0] >= (-1*(rect.right - mEpub3RelativeLayout.getRight())) ){
                    mEpub3RelativeLayout.setPivotX((float) (mEpub3RelativeLayout.getPivotX() + ( ( startX - event.getX() ) - SWIPE_MIN_DISTANCE)*0.2) );
                }else{
                    return true;
                }
            }

            if(startX < event.getX() && (event.getX() - startX) > SWIPE_MIN_DISTANCE){
                Log.d("EVENT", "SWIPE_RIGHT");
                int[] screen = new int[4];
                mEpub3RelativeLayout.getLocationOnScreen(screen);
                if(screen[0] <= 0 ){
                    mEpub3RelativeLayout.setPivotX((float) (mEpub3RelativeLayout.getPivotX() - ( ( event.getX() - startX ) - SWIPE_MIN_DISTANCE)*0.2) );
                }else{
                    return true;
                }
            }

            if(startY >event.getY() && (startY - event.getY()) > SWIPE_MIN_DISTANCE){
                Log.d("EVENT", "SWIPE_UP");
                int[] screen = new int[2];
                mEpub3RelativeLayout.getLocationOnScreen(screen);
                Rect rect = new Rect();
                mEpub3RelativeLayout.getLocalVisibleRect(rect);
                if(rect.bottom <= (mEpub3RelativeLayout.getMeasuredHeight()*mScaleFactor) &&
                        screen[1] >= -1*((mEpub3RelativeLayout.getMeasuredHeight()*mScaleFactor) - epub3_reader_parent.getBottom())){
                    mEpub3RelativeLayout.setPivotY((float) (mEpub3RelativeLayout.getPivotY() + ( ( startY - event.getY() ) - SWIPE_MIN_DISTANCE)*0.2) );
                }else{
                    return true;
                }                    

            }

            if(startY < event.getY() && (event.getY() - startY) > SWIPE_MIN_DISTANCE){
                Log.d("EVENT", "SWIPE_DOWN");
                int[] screen = new int[2];
                mEpub3RelativeLayout.getLocationOnScreen(screen);
                if((actionBarLayout.getBottom() + actionBarLayout.getPaddingBottom()) > screen[1]){
                    mEpub3RelativeLayout.setPivotY((float) (mEpub3RelativeLayout.getPivotY() - ( ( event.getY() - startY ) - SWIPE_MIN_DISTANCE)*0.2) );
                }else{
                    return true;
                }
            }

         }
    }

    return true;
}