我是 Android 开发的新手,并试图弄清楚。我有使用捏缩放的测试应用程序,但在https://stackoverflow.com/questions/14306908/textview-in-a-scrollview-with-pinch-zoom提出了问题
然而,这是有效的
package com.example.pinchzoom;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView scaleGesture;
ScaleGestureDetector scaleGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scaleGesture = (TextView)findViewById(R.id.article);
scaleGesture.setText("this is some text");
scaleGestureDetector = new ScaleGestureDetector(this, new simpleOnScaleGestureListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}
public class simpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub
float size = scaleGesture.getTextSize();
Log.d("TextSizeStart", String.valueOf(size));
float factor = detector.getScaleFactor();
Log.d("Factor", String.valueOf(factor));
float product = size*factor;
Log.d("TextSize", String.valueOf(product));
scaleGesture.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
size = scaleGesture.getTextSize();
Log.d("TextSizeEnd", String.valueOf(size));
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"
tools:context=".MainActivity" >
<TextView
android:id="@+id/article"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>