我应该如何实现滚动? [英] How should I implement Scrolling ?

查看:130
本文介绍了我应该如何实现滚动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已实施的自定义相对布局其中缩放使用ScaleGestureListener发生也就会放大出来双击监听器。现在我想变焦后实现泛在我的自定义相对布局在这里用户可以缩放后平移?这里是我的自定义相对layout..`

I have implemented custom relative layout where zoom is happening using ScaleGestureListener also it zooms out on double tap listener. Now I want to implement Pan in my custom relative layout after zoom where user can pan after the zoom ? Here is my custom relative layout..`

下面是我的code

public class ZoomLayout extends RelativeLayout implements OnDoubleTapListener, OnGestureListener{


//ScalingFactor i.e. Amount of Zoom
static float mScaleFactor = 1.0f;

// Maximum and Minimum Zoom
private static float MIN_ZOOM = 1.0f;
private static float MAX_ZOOM = 2.0f;

//Different Operation to be used 
    private final int NONE_OPERATION=0;
    private final int DRAG_OPERATION=1;
    private final int ZOOM_OPERATION=2;
    private float mWidth= 1280;
    private float mHeight=800; 

// Mode to select the operation
    private int mode;

//Track X and Y coordinate of the finger when it first touches the screen
    private float mInitialX = 0f;
    private float mInitialY = 0f;

// Track the Bound of the Image after zoom to calculate the offset  
static Rect mClipBound;

// mDetector to detect the scaleGesture for the pinch Zoom  
private ScaleGestureDetector mDetector;

// mDoubleTapDetector to detect the double tap 
private GestureDetector mDoubleTapDetector;

//Pivot point for Scaling
static float gx=0,gy=0;

boolean mdrag=false,mZoom=false;

public ZoomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
        mClipBound = new Rect();
        // Intialize ScaleGestureDetector
        mDetector = new ScaleGestureDetector(getContext(), new ZoomListener());
        mDoubleTapDetector = new GestureDetector(context,this);
        mDoubleTapDetector.setOnDoubleTapListener(this);
    }


    public ZoomLayout(Context context) {
        super(context);
        setWillNotDraw(false);
        mClipBound = new Rect();
        // Intialize ScaleGestureDetector
        mDetector = new ScaleGestureDetector(getContext(), new ZoomListener());
        mDoubleTapDetector = new GestureDetector(context,this);
        mDoubleTapDetector.setOnDoubleTapListener(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        // Handles all type of motion-events possible
        switch(event.getAction() ) {

        case MotionEvent.ACTION_DOWN:
            // Event occurs when the first finger is pressed on the Screen

            Log.d("ZoomPrint", "Event: Action_Down " );
            mInitialX = event.getX();
            mInitialY = event.getY();

        break;
        case MotionEvent.ACTION_POINTER_DOWN:
            //Event occurs when the second finger is pressed down

            Log.d("ZoomPrint", "Event: Action_Pointer_Down " );
            // If second finger is pressed on the screen with the first set the Mode to Zoom operation
            mode=ZOOM_OPERATION;

            break;  
        case MotionEvent.ACTION_POINTER_UP:
            Log.d("ZoomPrint", "Event: Action_Pointer_UP " );
            mdrag=true;

        case MotionEvent.ACTION_UP: 
            //Event occurs when all the finger are taken of the screen
            Log.d("ZoomPrint", "Event: Action_UP " );
            //If all the fingers are taken up there will be no operation 
            mode = NONE_OPERATION;
            mdrag=false;

            break;  


        }
        // give the event to the mDetector to get the scaling Factor
            mDetector.onTouchEvent(event);


        // give the event to the mDoubleTapDetector for the doubleTap 
            mDoubleTapDetector.onTouchEvent(event);

        if(!mdrag)
            invalidate();

    return true;
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        onTouchEvent(ev);
        return super.onInterceptTouchEvent(ev);
    //  return true;
    }







    @Override
    public ViewParent invalidateChildInParent(int[] location, Rect dirty) {
        return super.invalidateChildInParent(location, dirty);
    }




    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        int count = getChildCount();
        for(int i=0;i<count;i++){
            View child = getChildAt(i); 
            if(child.getVisibility()!=GONE){
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)child.getLayoutParams();
                child.layout(
                    (int)(params.leftMargin ), 
                    (int)(params.topMargin ), 
                    (int)((params.leftMargin + child.getMeasuredWidth()) ), 
                    (int)((params.topMargin + child.getMeasuredHeight())) 
                    );
            }
        }
    }



    @Override
    protected void dispatchDraw(Canvas canvas) {

        //Save the canvas to set the scaling factor returned from detector
        canvas.save(Canvas.MATRIX_SAVE_FLAG);

        canvas.scale(mScaleFactor, mScaleFactor,gx,gy);     

        super.dispatchDraw(canvas);

        mClipBound = canvas.getClipBounds();

          canvas.restore();
    }



    private class ZoomListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {


            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                // getting the scaleFactor from the detector
                mScaleFactor *= detector.getScaleFactor();              // gives the scaling factor from the previous scaling to the current
            //  Log.d("ZoomPrint", "detector scaling Factor" + mScaleFactor);

                gx = detector.getFocusX();
                gy = detector.getFocusY();
                // Limit the scale factor in the MIN and MAX bound
                mScaleFactor= Math.max(Math.min(mScaleFactor, MAX_ZOOM),MIN_ZOOM);
            //  Log.d("ZoomPrint", "Bounded scaling Factor" + mScaleFactor);

                /*//Force canvas to redraw itself only if the one event is to happen (say Zooming only ) else do not invalidate here for multi operations
                   As what we de for scrolling or panning will not reflect here. So we will add this in onDraw method 
                invalidate();*/
                 // Here we are only zooming so invalidate has to be done
            //  invalidate();
             //  requestLayout();

                // we have handle the onScale 
                return true;
            }



        }


        @Override
        public boolean onDoubleTap(MotionEvent e) {

        // Make the mScaleFactor to its normal value
            if(mScaleFactor>1.0f)
            {
                    mScaleFactor=1.0f;
            }
        /*  else{
                mScaleFactor = 2.0f;
                gx=mInitialX;
                gy=mInitialY;

            }*/

        // Force the canvas to redraw itself again as the changes has been occured.
        invalidate();
        requestLayout();
            return false;
        }


        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
        //  Log.d("ZoomPrint", "OnDoubleTapEvent");
            return false;
        }


        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
        //  Log.d("ZoomPrint", "OnSingleTap");
            return false;
        }


        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }


        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2,
                float velocityX, float velocityY) {
            return false;
        }


        @Override
        public void onLongPress(MotionEvent e) {

        }


        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                float distanceX, float distanceY) {
                return false;
        }


        @Override
        public void onShowPress(MotionEvent e) {

        }


        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            return false;
        }

}

如何实现变焦后势必在屏幕内平移?
谢谢,

How to achieve Panning within the screen bound after zooming ? Thanks ,

推荐答案

我已经实现了我自己只能采用onScroll方法,这也保持图像的边界地区。这里是code

I have achieved it by myself only using onScroll method this also keeps image in a bound area.. Here is the code

     @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2,
                    float distanceX, float distanceY) {

                    int distX= (int) distanceX, distY =(int) distanceY;

        //Log.d("Print"," X " + this.mClipBound.left +" Y " + this.mClipBound.right + " b "+ this.mClipBound.bottom + " g" + this.mClipBound.top) ;

        Log.d("Print", "Scroll X " + distanceX + " Y " + distanceY);    

                if(this.mClipBound.left<=0)
                    this.scrollTo(-280, 0);
                else if(this.mClipBound.top<=0)
                    this.scrollTo(0, -250);
                    else if (this.mClipBound.right>=1047)
                        this.scrollTo(280, 0);
                    else if (this.mClipBound.bottom>=800)
                        this.scrollTo(0, 250);
                    else
                    this.scrollBy((int)distanceX,(int)distanceY);


                    return true;

            }

偏移可以计算变焦后点击区域 - (event.getX()/ ZoomLayout.mScaleFactor + ZoomLayout.mClipBound.left)
但我还没有还是想通了灾区的偏移缩放和滚动后。

Offset of the hit area after zoom can be calculated - (event.getX() / ZoomLayout.mScaleFactor + ZoomLayout.mClipBound.left) But i have not still figured out the offset of hit area after zooming and scrolling .

这篇关于我应该如何实现滚动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆