如何从一个ViewGroup到多个子视图通过双指缩放事件(how to pass pinch zoo

2019-10-19 22:39发布

我想提出一个使用Horizo​​ntalScrollView用于显示的产品清单的应用程序。 所以,我对于水平滚动内Horizo​​ntalScrollView多个视图。 我根据比例因子改变视图的布局尺寸上检测捏变焦

我在视图层面实现了它,所以当有一个单一的视图中两个手指捏用户缩放,这一观点被检测双指缩放和改变其大小,但是当我在不同的视图指缩放与我的手指,这是行不通的。

我想这样扩展这个时候有不同的看法手指捏用户缩放,所有这一切都是两个手指之间的意见应动态地改变它的大小。

任何人都可以建议我如何延长这一想法,并使其发挥作用。

    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;
        }
    }





}
文章来源: how to pass pinch zoom event from a ViewGroup to multiple child Views