Zoom with LinearLayout

3
我有一个包含其他视图的LinearLayout。我希望能够整体缩放LinearLayout。有没有办法实现这个功能?
谢谢。

请查看以下链接:https://dev59.com/DWkw5IYBdhLWcg3wQ4dc 并将相对布局替换为线性布局。 - darkbobo
2个回答

3
抱歉,据我所知,普通小部件没有内置的缩放功能。但是,WebViewMapView可以进行缩放。其他小部件需要自己实现。

0

我是用那种方式做的

MainActivity.java:

public class MainActivity extends Activity
{


Button button;
LinearLayout linearLayout;
Float scale = 1f;
ScaleGestureDetector SGD;
int currentX;
int currentY;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    SGD = new ScaleGestureDetector(this, new ScaleListener());



    linearLayout = (LinearLayout) findViewById(R.id.main_container);

}

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

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            currentX = (int) event.getRawX();
            currentY = (int) event.getRawY();
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            int x2 = (int) event.getRawX();
            int y2 = (int) event.getRawY();
            linearLayout.scrollBy(currentX - x2 , currentY - y2);
            currentX = x2;
            currentY = y2;
            break;
        }
        case MotionEvent.ACTION_UP: {
            break;
        }
    }

    return true;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener
{
    @Override
    public boolean onScale(ScaleGestureDetector detector)
    {
        scale = scale * detector.getScaleFactor();
        scale = Math.max(1f, Math.min(scale, 5f)); //0.1f und 5f  //First: Zoom in __ Second: Zoom out
        //matrix.setScale(scale, scale);
        linearLayout.setScaleX(scale);
        linearLayout.setScaleY(scale);

        linearLayout.invalidate();

        return true;
    }

  }

}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">


<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="zoom_test" />
</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接