Java 收缩缩放和平移

Java 收缩缩放和平移,java,android,layout,pinchzoom,pan,Java,Android,Layout,Pinchzoom,Pan,我有一个以LinearLayout为主要布局的活动。在该布局中,有一个按钮将视图(R.layout.motor_块)添加到主布局(R.id.layout): 这很好,但是当我添加更多视图时,屏幕上会被它们填满,所以我需要一种方法通过添加平移来“扩展”大小,这样我就可以浏览所有视图。 我还想添加缩放 我尝试使用网络视图: RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout); WebView wv = (W

我有一个以LinearLayout为主要布局的活动。在该布局中,有一个按钮将视图(R.layout.motor_块)添加到主布局(R.id.layout):

这很好,但是当我添加更多视图时,屏幕上会被它们填满,所以我需要一种方法通过添加平移来“扩展”大小,这样我就可以浏览所有视图。 我还想添加缩放

我尝试使用网络视图:

       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);
视图被添加到其中,并且pan可以工作。问题是当我缩放时,webView会缩放背景,而视图的大小不会改变

我还尝试了一些自定义视图,但它们不支持包含子视图的视图


实现这一点的最佳方法是什么?

最简单的方法是在滚动视图中添加所有这些视图。然后,您可以将ScrollView添加到水平ScrollView中,以便在两个方向上进行滚动。为捏缩和缩放手势做个手势,并给听众打电话。对于缩放,您可以通过运行循环或可能使用ScaleGetureDetector来增加每个视图的大小。
这种方法有点不稳定,但很有效。查看以下答案以了解更多信息-

更新:-

这里有更多的答案-


这只是一个例子。在主容器视图上使用setTranslationX/Y和setScaleX/Y。它的孩子们将用它进行缩放和翻译

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>
公共类MainActivity扩展活动{
私人住宅主布局;
私人scalegestruedetector scalegestruedetector;
私人浮动比例=1;
私人接触点;
私人点盘;
私有布尔标度;
私有布尔值endScalingNextUp;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout=(RelativeLayout)findViewById(id.main\u布局);
scalegestruedetector=新的scalegestruedetector(这是一个新的示例scalegestruelistener());
按钮b=((按钮)findViewById(id.btn_reset));
b、 setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
重置();
}
});
重置();
}
@凌驾
公共布尔onTouchEvent(运动事件){
scalegestruedetector.onTouchEvent(事件);
if(比例=1&&!isScaling){
float x=event.getRawX();
float y=event.getRawY();
if(event.getAction()==MotionEvent.ACTION\u向下){
接触点=新的点F(x,y);
}else if(event.getAction()==MotionEvent.ACTION\u MOVE&&touchPoint!=null){
pan.x=x-接触点.x;
pan.y=y-接触点y;
panView();
}
}
如果(isScaling&&endScalingNextUp){
if(event.getAction()==MotionEvent.ACTION\u指针向上){
endScalingNextUp=false;
isScaling=假;
}
}
返回true;
}
私有void setPivot(浮动焦点x、浮动焦点y){
mainLayout.setPivotX(focusX);
主布局。设置数据透视(焦点);
}
私有void scaleView(){
主布局。设置刻度(比例);
主布局。设置比例(比例);
}
私有void panView(){
主布局.setTranslationX(pan.x);
主布局设置平移y(平移y);
}
私有无效重置(){
setPivot(0,0);
比例=1;
scaleView();
pan=新的点F(0,0);
panView();
isScaling=假;
}
私有类ExampleScaleGestureListener在ScalegestureListener上实现{
专用静态最终浮动刻度盘\速度=.02f;
@凌驾
标度仪上的公共空隙(标度仪检测器){
endScalingNextUp=true;
}
@凌驾
公共布尔onScaleBegin(ScaleGetStereDetector检测器){
float focusX=detector.getFocusX();
float focusY=detector.getFocusY();
setPivot(focusX,focusY);
isScaling=true;
endScalingNextUp=false;
返回true;
}
@凌驾
公共布尔标度(scalegestruedetector检测器){
isScaling=true;
endScalingNextUp=false;
if(detector.getScaleActor()<1){
缩放-=缩放速度;
}else if(detector.getScaleFactor()>1){
缩放+=缩放速度;
}
scaleView();
返回true;
}
}
}

当视图重新调整大小时,它们之间是否保持相同的距离?如果您手动缩放它们,它们之间将保持相同的边距,但我不确定是否使用ScalegStereDetector。这个答案是否可以应用于平移和缩放网格视图?
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>