【问题标题】:Zoom with LinearLayout使用线性布局缩放
【发布时间】:2011-06-09 15:54:32
【问题描述】:

我有一个包含其他视图的 LinearLayout。我希望能够放大和缩小整个实际的 LinearLayout。有没有办法做到这一点?

感谢

【问题讨论】:

标签: android


【解决方案1】:

不,抱歉,AFAIK 没有内置任何用于缩放普通小部件的功能。 WebViewMapView 知道如何缩放。其他一切都靠你自己。

【讨论】:

    【解决方案2】:

    我就是这样做的

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多