【发布时间】:2011-06-09 15:54:32
【问题描述】:
我有一个包含其他视图的 LinearLayout。我希望能够放大和缩小整个实际的 LinearLayout。有没有办法做到这一点?
感谢
【问题讨论】:
-
见:stackoverflow.com/questions/10013906/… 并用线性布局替换相对布局
标签: android
我有一个包含其他视图的 LinearLayout。我希望能够放大和缩小整个实际的 LinearLayout。有没有办法做到这一点?
感谢
【问题讨论】:
标签: android
不,抱歉,AFAIK 没有内置任何用于缩放普通小部件的功能。 WebView 和 MapView 知道如何缩放。其他一切都靠你自己。
【讨论】:
我就是这样做的
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>
【讨论】: