请帮助!我想在我的滚动视图中缩放文本,我尝试了但应用程序崩溃了,滚动视图中有一个文本视图,而我自己关心的只是让用户更好地查看文本,因此我想实现一个缩小的缩放甚至连双击缩放都没问题……这是与缩放有关的部分可以通过// start //和// stop //结束识别的代码,我不知道它为什么崩溃:
class MainActivity extends AppCompatActivity {
private LayoutInflater layoutInflater;
private RelativeLayout relalayout
private LinearLayout linelayout;
private ScrollView scrollayout;
public Toast toast;
private ImageView backgroundimg;
//start//
private float mScale = 1f;
private ScaleGestureDetector mScaleGestureDetector;
GestureDetector gestureDetector;
//stop//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activitymain);
Bundle extradata1 = getIntent().getExtras();
String textString = extradata1.getString("ImportedData");
TextView mytext = (TextView) findViewById(R.id.text);
relalayout = (RelativeLayout) findViewById(R.id.relalayout);
linelayout = (LinearLayout) findViewById(R.id.linelayout);
scrollayout = (ScrollView) findViewById(R.id.scrolllayout);
backgroundimg = (ImageView) findViewById(R.id.imageView2);
toast = new Toast(this);
//start//
gestureDetector = new GestureDetector(this, new GestureListener());
mScaleGestureDetector = new ScaleGestureDetector(this, new
ScaleGestureDetector.SimpleOnScaleGestureListener(){
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scale = 1 - detector.getScaleFactor();
float prevScale = mScale;
mScale += scale;
if (mScale > 10f)
mScale = 10f;
ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f /
mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(),
detector.getFocusY());
scaleAnimation.setDuration(0);
scaleAnimation.setFillAfter(true);
scrollayout.startAnimation(scaleAnimation);
return true;
}
});
//stop//
if (textString.equals("firstimported")) {
String mytxt = "";
StringBuffer sbuffer = new StringBuffer();
InputStream is = this.getResources().openRawResource(R.raw.firsttext);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
try {
while ((mytxt = reader.readLine()) != null) {
sbuffer.append(mytxt + "\n");
}
mytext.setText(sbuffer);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
String text = sbuffer.toString();
SpannableString ss = new SpannableString(text);
ClickableSpan clickspan1 = new ClickableSpan() {
@Override
public void onClick(View widget) {
Toast toast = Toast.makeText(MainActivity.this, "this is the toast",
Toast.LENGTH_SHORT);
toast.show();}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.WHITE);
ds.setUnderlineText(false);}
};
scrollayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
toast.cancel();
return false;
}
});
ss.setSpan(clickspan1, 52, 53, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mytext.setText(ss);
mytext.setMovementMethod(LinkMovementMethod.getInstance());
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
super.dispatchTouchEvent(event);
mScaleGestureDetector.onTouchEvent(event);
gestureDetector.onTouchEvent(event);
return gestureDetector.onTouchEvent(event);
}
//start//
private class GestureListener extends
GestureDetector.SimpleOnGestureListener{
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}//stop//
public void onBackPressed() {
Intent intent = new Intent(MainActivity.this, OtherActivity.class);
startActivity(intent);
finish();
}