Zoom with LinearLayout
I have a LinearLayout that would contain some other views. I would like to have the ability 开发者_StackOverflow中文版to zoom in and out on the actual LinearLayout as a whole. Is there a way to do that?
thanks in
No, sorry, there is nothing built in for zoom on normal widgets, AFAIK. WebView
and MapView
know how to zoom. Anything else you are on your own.
I do it that way
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>
精彩评论