I am creating an application that uses a HorizontalScrollView to display a list of items. Thus, I have several views in the HorizontalScrollView for horizontal scrolling. I resize the view when I detect a zoom increase in accordance with scaleFactor
I implemented it at the presentation level, so when a user clicks on a zoom that has both fingers inside the same view, this view detects a pinch of zoom and changes its size, but when I hold the zoom, the fingers in different points of view do not work.
I want to expand this, so when the user clicks the zoom, having fingers in different views, all views that are between two fingers must dynamically resize it.
Can anyone suggest me how to expand 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; } } }
java android android-layout horizontalscrollview pinchzoom
user3265443 Apr 20 '14 at 21:28 2014-04-20 21:28
source share