13

我正在尝试实现捏合以放大 Android 上的列表视图,当我单击并拖动缩放的列表视图时遇到问题。首先,这是代码:

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

问题在于翻译。它是无限的,我可以水平拖动它并将列表视图从视口中取出。所以我无法面对如何在翻译中占据优势......感谢您的帮助。

4

3 回答 3

2

我只是为此组建了一个工人阶级。请让我知道这对你有没有用。 https://github.com/Xjasz/AndroidZoomableViewGroup

于 2015-07-29T17:12:20.790 回答
1

以下代码将我的文本视图限制在屏幕边缘。希望这会对您的列表视图有所帮助..我在 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());
于 2015-05-19T12:21:44.420 回答
0

请在此处找到 zoomListView 类。您可以设置 pinchZoom 以及双击来缩放/取消缩放。

于 2018-12-03T05:06:50.020 回答