捏缩放和平移 [英] Pinch Zoom and Pan

查看:121
本文介绍了捏缩放和平移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有LinearLayout中为主要布局的活动。在该布局,有一个按钮,增加了一个视图(R.layout.motor_block)到主要布局(R.id.layout):

I have an activity with LinearLayout as its main Layout. In that layout, there is a button that adds a View (R.layout.motor_block) to the main layout (R.id.layout):

    LayoutInflater inflater =
    (LayoutInflater)this.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    View iv = inflater.inflate( R.layout.motor_block, null );
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
    rl.addView(iv);

这工作得很好,但是当我添加更多的意见,画面被充满了他们,所以我需要的方式加入泛这样我就可以通过所有的意见,扩展的大小。 我也想补充变焦。

This works fine, but when I add more Views, the screen gets filled up with them so I need a way to "extend" the size by adding pan so I can go through all the views. I also want to add zoom.

我试图用一个web视图:

I tried to use a WebView:

       RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
   WebView wv = (WebView) findViewById(R.id.webView1);
   wv.getSettings().setSupportZoom(true);  
   wv.getSettings().setBuiltInZoomControls(true);
   wv.getSettings().setUseWideViewPort(true);
   wv.getSettings().setLoadWithOverviewMode(true);
   wv.getSettings().setDisplayZoomControls(false);
   wv.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
   wv.addView(iv);

的意见被添加到它,和平移工程。问题是,当我放大,web视图缩放背景,视图不会改变大小。

The Views get added to it, and pan works. The problem is that when I zoom, the webView zooms the background, and the Views don't change size.

我也尝试了一些自定义的意见,但他们不支持的意见与孩子的意见。

I also tried some custom views but they didn't support views with child views.

什么是实现这一目标的最佳方式是什么?

What is the best way to accomplish this?

推荐答案

只是一个例子。使用setTranslationX / Y和setScaleX / Y主容器视图。其子将规模并翻译它。

Just an example. Use setTranslationX/Y and setScaleX/Y on the main container view. Its children will scale and translate with it.

public class MainActivity extends Activity {

    private RelativeLayout mainLayout;
    private ScaleGestureDetector scaleGestureDetector;
    private float scale = 1;
    private PointF touchPoint;
    private PointF pan;

    private boolean isScaling;
    private boolean endScalingNextUp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainLayout = (RelativeLayout) findViewById(id.main_layout);
        scaleGestureDetector = new ScaleGestureDetector(this, new ExampleScaleGestureListener());
        Button b = ((Button) findViewById(id.btn_reset));
        b.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                reset();
            }
        });
        reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        scaleGestureDetector.onTouchEvent(event);

        if (scale != 1 && !isScaling) {
            float x = event.getRawX();
            float y = event.getRawY();
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                touchPoint = new PointF(x, y);
            } else if (event.getAction() == MotionEvent.ACTION_MOVE && touchPoint != null) {
                pan.x = x - touchPoint.x;
                pan.y = y - touchPoint.y;
                panView();
            }
        }
        if (isScaling && endScalingNextUp) {
            if (event.getAction() == MotionEvent.ACTION_POINTER_UP) {
                endScalingNextUp = false;
                isScaling = false;
            }
        }

        return true;
    }

    private void setPivot(float focusX, float focusY) {
        mainLayout.setPivotX(focusX);
        mainLayout.setPivotY(focusY);
    }

    private void scaleView() {
        mainLayout.setScaleX(scale);
        mainLayout.setScaleY(scale);
    }

    private void panView() {
        mainLayout.setTranslationX(pan.x);
        mainLayout.setTranslationY(pan.y);
    }

    private void reset() {
        setPivot(0, 0);
        scale = 1;
        scaleView();
        pan = new PointF(0, 0);
        panView();
        isScaling = false;
    }

    private class ExampleScaleGestureListener implements OnScaleGestureListener {

        private static final float SCALE_SPEED = .02f;

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {
            endScalingNextUp = true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            float focusX = detector.getFocusX();
            float focusY = detector.getFocusY();
            setPivot(focusX, focusY);
            isScaling = true;
            endScalingNextUp = false;
            return true;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            isScaling = true;
            endScalingNextUp = false;
            if (detector.getScaleFactor() < 1) {
                scale -= SCALE_SPEED;
            } else if (detector.getScaleFactor() > 1) {
                scale += SCALE_SPEED;
            }
            scaleView();
            return true;
        }
    }
}


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:id="@+id/main_layout"
        tools:context=".MainActivity" >

        <TextView
            android:id="@+id/tv"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/tv"
            android:layout_marginBottom="69dp"
            android:layout_marginRight="29dp"
            android:layout_toLeftOf="@+id/tv"
            android:src="@drawable/ic_launcher" />

        <Button
            android:id="@+id/btn_reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tv"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="22dp"
            android:text="Reset" />

    </RelativeLayout>

这篇关于捏缩放和平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆