how to pass pinch zoom event from a ViewGroup to multiple child Views

a 夏天 提交于 2019-12-11 13:05:29

问题


I am making an app that is using HorizontalScrollView for showing list of items. So I have multiple views inside HorizontalScrollView for horizontal scrolling. I am changing the layout size of a view on detecting pinch zoom according to scaleFactor

I have implemented it at view level so when user pinch zoom having both finger inside a single view, that view is detecting pinch zoom and changing its size but when I pinch zoom with my fingers in different views this is not working.

I want to extend this so when user pinch zoom having fingers on different views, all the views which are between two fingers should change its size dynamically.

Can anyone suggest me on how to extend this idea and make it work.

    public CenterLockHorizontalScrollview(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        this.setSmoothScrollingEnabled(true);
        detector = new ScaleGestureDetector(context, new ScaleListener());


    }


    public boolean onTouchEvent(MotionEvent ev) {

        return super.onTouchEvent(ev);

    }



    public void setAdapter(Context context, CustomListAdapter mAdapter) {

        try {
            this.kk=mAdapter;
            fillViewWithAdapter(mAdapter);
        } catch (ZeroChildException e) {

            e.printStackTrace();
        }
    }

    private void fillViewWithAdapter(CustomListAdapter mAdapter)
            throws ZeroChildException {
        if (getChildCount() == 0) {
            throw new ZeroChildException(
                    "CenterLockHorizontalScrollView must have one child");
        }

        if (getChildCount() == 0 || mAdapter == null)
        {        Log.e("Ronak","Came at zero");
            return;
        }

         parent = (ViewGroup) getChildAt(0);

        for (int i = 0; i < mAdapter.getCount(); i++) {
            parent.addView(mAdapter.getView(i, null, parent));
        }

        //System.out.println("number of child is "+parent.getChildCount());
    }


    public ViewGroup takeparent()
    {
        return parent;
    }


    int start=0; //index of first child view
    int last=0;  //index of last child view

    float x1,x2,y1,y2;

    public boolean dispatchTouchEvent(MotionEvent ev) {


        boolean result = super.dispatchTouchEvent(ev);
        if (inDifferentView(ev)) {
             detector.onTouchEvent(ev);
        }

            return super.dispatchTouchEvent(ev);
        }





    //set the value of 'start' and 'last' variable
    private boolean inDifferentView(MotionEvent ev) {
        Rect outRect = new Rect();


        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            x1=ev.getX(0);
            y1=ev.getY(0);
            Log.e("Ronak","First finger Coordinates "+x1+" "+y1);
        }
        switch (ev.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_POINTER_DOWN:

            visibleRect.clear();
            indices.clear();
            int left=0;

            x2=ev.getX(1);
            y2=ev.getY(1);
            Log.e("Ronak","Second finger Coordinates "+x2+" "+y2);

            ViewGroup parent=takeparent(); //parent viewGroup. This is of type HorizontalScrollView
            for(int i=0; i<parent.getChildCount(); i++)
            {
                View childview =parent.getChildAt(i);
                this.getHitRect(outRect);

                if (childview.getLocalVisibleRect(outRect)){  
                    Log.e("Ronak","Child number is "+i + "Coordinates are "+outRect.flattenToString());
                    visibleRect.add(new Rect(left,outRect.top,outRect.right-outRect.left+left,outRect.bottom));
                    left+=(outRect.right-outRect.left);
                    indices.add(i);
                }

            }

            for(int i=0;i<visibleRect.size();i++)
            {
                Log.e("Ronak","Original  rectangle "+visibleRect.get(i).flattenToString());
                Rect ar=visibleRect.get(i);
                if(x1>=ar.left && x1<=ar.right && y1>=ar.top && y1 <=ar.bottom);
                {
                    Log.e("Ronak","ALSO");
                    int temp=indices.get(i);
                    Log.e("ROnak","Found Index of rectangle "+temp);
                        start=temp;

                }
                if(x2>=ar.left && x2<=ar.right && y2>=ar.top && y2 <=ar.bottom)
                {
                    Log.e("Ronak","ALSO");
                    int temp=indices.get(i);
                    Log.e("ROnak","Second Found Index of rectangle "+temp);

                    last=temp;
                }
            }

        }
        //interchanging start and last
        if(start>last)
        {
            int temp=last;
            last=start;
            start=temp;
        }

        return true;
    } 



    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            mScaleFactor *= detector.getScaleFactor();
            mScaleFactor = Math.max(1.0f, Math.min(mScaleFactor, 5.0f));
            Log.e("Ronak","SF is "+mScaleFactor);
            ViewGroup pp=takeparent();
           /*for(int i=start;i<=last;i++)
            {
                LinearLayout ll=(LinearLayout)pp.getChildAt(i);
                DrawView view=(DrawView)ll.findViewById(R.id.drawview);
                view.width=mScaleFactor*(view.width);
                view.invalidate();
            }
            */

            return true;
        }
    }





}

来源:https://stackoverflow.com/questions/23187772/how-to-pass-pinch-zoom-event-from-a-viewgroup-to-multiple-child-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!