捏缩放视图

时间:2012-05-12 17:32:52

标签: android android-layout

我正在使用第一个使用显示名称的列表活动的应用程序。

当我点击其中一个列表项时,它会打开一个新的意图(活动?),它调用包含文本视图的xml布局。

我想要实现的是让pinchzoom使用textview(或类似的东西)

今天,最后,我遇到了一个用textview来做这个版本的版本,但是我还不够用这种语言自己实现整个课程。

我找到了这个教程,但我似乎无法弄清楚如何使用它: http://www.zdnet.com/blog/burnette/how-to-use-multi-touch-in-android-2-part-6-implementing-the-pinch-zoom-gesture/1847

任何人都可以帮我解决此代码的MyGestureDetector():

private GestureDetector gestureDetector;
private View.OnTouchListener gestureListener;

在onCreate:

// Zoom handlers
gestureDetector = new GestureDetector(new MyGestureDetector());
gestureListener = new View.OnTouchListener() {

    // We can be in one of these 2 states
    static final int NONE = 0;
    static final int ZOOM = 1;
    int mode = NONE;

    static final int MIN_FONT_SIZE = 10;
    static final int MAX_FONT_SIZE = 50;

    float oldDist = 1f;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        TextView textView = (TextView) findViewById(R.id.text);

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                Log.d(TAG, "oldDist=" + oldDist);
                if (oldDist > 10f) {
                   mode = ZOOM;
                   Log.d(TAG, "mode=ZOOM" );
                }
                break;
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == ZOOM) {
                    float newDist = spacing(event);
                    // If you want to tweak font scaling, this is the place to go.
                    if (newDist > 10f) {
                        float scale = newDist / oldDist;

                        if (scale > 1) {
                            scale = 1.1f;
                        } else if (scale < 1) {
                            scale = 0.95f;
                        }

                        float currentSize = textView.getTextSize() * scale;
                        if ((currentSize < MAX_FONT_SIZE && currentSize > MIN_FONT_SIZE)
                                ||(currentSize >= MAX_FONT_SIZE && scale < 1)
                                || (currentSize <= MIN_FONT_SIZE && scale > 1)) {
                            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, currentSize);
                        }
                    }
                }
                break;
            }
        return false;
    }

谢谢!

1 个答案:

答案 0 :(得分:0)

查看ScaleGestureDetector(ScaleGestureDetector)。它可以很好地处理捏合,为您提供比例因子,焦点和其他内容。