13

I'm trying to implement a pinch to zoom in a listview on Android and I'm facing a problem when I click and drag the zoomed list view. First, here's the code:

public class ScaleListView extends ListView {

private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
private float x = 0;
private float y = 0;
private float startx = 0;
private float starty = 0;
private boolean canClick = true;
private float xtranslation = 0;

public ScaleListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_DOWN) {
        startx = ev.getX();
        starty = ev.getY();
    }

    if (ev.getPointerCount() > 1) {
        y = ev.getY();
        x = ev.getX();
    } else if (ev.getAction() == MotionEvent.ACTION_UP) {
        canClick = (ev.getX() - startx) == 0 && (ev.getY() - starty) == 0 && mScaleFactor == 1;
    } else if(ev.getAction() == MotionEvent.ACTION_MOVE){
        xtranslation = ev.getX() - startx;
    }       
    mScaleDetector.onTouchEvent(ev);
    return super.onTouchEvent(ev);
}   
@Override
public void onDraw(Canvas canvas) {
    canvas.scale(mScaleFactor, mScaleFactor, x , y);
    canvas.translate(xtranslation, 0);
    super.onDraw(canvas);
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        canClick = false;
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        if (mScaleFactor <= 1) {
            mScaleFactor = 1;
            canClick = true;
        }

        xPos = ScaleListView.this.getWidth() - detector.getFocusX();

        ScaleListView.this.invalidate();
        return true;
    }
}   
public boolean canClick(){
    return canClick;
}

The problem is with the translation. It goes infinite, I can drag it horizontally and take the list view off the viewport. So I can't face how to put an edge to the translation... Thanks for any help.

Augusto
  • 1,234
  • 1
  • 16
  • 35
  • 1
    have you found the solution? – Qadir Hussain Jul 16 '14 at 09:32
  • No. I've tried to create some mathematical formula but it didn't work. – Augusto Jul 16 '14 at 10:02
  • is it possble to add the above listview inside ascrollview ? i ink this may create problem – fatboy Oct 26 '14 at 16:17
  • I'm sorry. I don't have access to the code anymore – Augusto Oct 26 '14 at 16:38
  • i think it will be use to you [link for pinch zoom of listview][1] [1]: http://stackoverflow.com/questions/8196503/pinch-zoom-on-a-listview – Alka Jadav Apr 03 '15 at 11:09
  • And what are the semantics of a pinch-zoomed list?! – Bondax Apr 16 '15 at 11:19
  • Well... My client complained their users have vision problems. Android offers native accessibility for this nowadays, but not at the time I've posted this (not that I remember though). And I don't even remember what I did to solve my client's issue. The only thing I remember is that I could not make this work the way I wanted – Augusto Apr 16 '15 at 11:25

3 Answers3

2

I just put together a working class for this. Let me know if it works for you. https://github.com/Xjasz/AndroidZoomableViewGroup

Xjasz
  • 1,238
  • 1
  • 9
  • 21
  • I'm sorry, but I don't have access to the code anymore. Unfortunately I don't even remember what I did with this feature request. :( I'll accept your answer since it seems to be the best solution. – Augusto Jul 29 '15 at 17:14
  • Thats OK I just wanted to post this somewhere. I wasted a day just searching for one that wasn't to broken to manage. – Xjasz Jul 29 '15 at 17:17
1

follwing code restricts my textview to screens edges.. Hope this will help you in your listview ..I had done this code on ACTION:Move

layoutParams.leftMargin = X - _xDelta;
                    layoutParams.topMargin = Y - _yDelta;
                    if (layoutParams.leftMargin < 0) {
                        layoutParams.leftMargin = 20;
                    } else if (layoutParams.leftMargin > mainimage.getWidth()) {
                        layoutParams.leftMargin = mainimage.getWidth() - 20;
                    }
                    if (layoutParams.topMargin < 0) {
                        layoutParams.topMargin = 20;
                    } else if (layoutParams.topMargin > mainimage.getHeight() - 100) {
                        text.setText(text2.getText().toString());
                        layoutParams.topMargin = mainimage.getHeight() - 90
                                - text.getHeight();
                        // layoutParams.leftMargin = 20;
                    }

                    text.setX(layoutParams.leftMargin);
                    text.setY(layoutParams.topMargin);

                    v.setLayoutParams(text.getLayoutParams());
vaibhav
  • 634
  • 1
  • 4
  • 23
0

Please find zoomListView class here. You can set pinchZoom as well as doubletap to zoom/unzoom.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122