我想根据用户选择缩放显示在屏幕中心的文字。我怎样才能做到这一点? 使用捏合多点触控不能在模拟器上测试,我想要一些我可以在Android模拟器上测试的东西。 我可以使用放大和缩小控件来仅控制布局的文本视图吗? 或者我可以使用webview来包含文本,因为webview具有默认的放大按钮吗?
答案 0 :(得分:1)
我可以使用放大和缩小控件吗? 仅控制我的布局的文本视图?
没有内置支持。您可以通过使用2D图形API(Canvas
)和触摸事件绘制文本来自己实现此效果。或者,拦截TextView
上的触摸事件并更改文本的字体大小。
答案 1 :(得分:0)
这是我对此的回答
public class Songs extends SherlockActivity implements OnTouchListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_songs);
context = this;
sett = new SettRenderingEngine(context);
Intent i = getIntent();
song = i.getStringExtra("song");
singer = i.getStringExtra("singer");
lyrics = i.getStringExtra("lyrics");
txt_song_title = (TextViewPlus) findViewById(R.id.song_title);
txt_singer = (TextViewPlus) findViewById(R.id.singer);
txt_song_lyrics = (TextViewPlus) findViewById(R.id.song_lyrics);
txt_song_title.setText(sett.getSettString(song));
txt_singer.setText(sett.getSettString(singer));
txt_song_lyrics.setText(sett.getSettString(lyrics));
txt_song_lyrics.setOnTouchListener(this);
scaleGestureDetector = new ScaleGestureDetector(this,
new simpleOnScaleGestureListener());
}
public class simpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float size = txt_song_lyrics.getTextSize();
float factor = detector.getScaleFactor();
float product = size * factor;
txt_song_lyrics.setTextSize(TypedValue.COMPLEX_UNIT_PX, product);
size = txt_song_lyrics.getTextSize();
return true;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
scaleGestureDetector.onTouchEvent(event);
return true;
}
}
答案 2 :(得分:0)
放大缩小
public class ZoomControls extends AppCompatActivity
{
Button zoomin, zoomout;
TextView text;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zoomcontrols);
zoomin = (Button) findViewById(R.id.zoomin);
zoomout = (Button) findViewById(R.id.zoomout);
text = (TextView) findViewById(R.id.text);
zoomin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float x = text.getScaleX();
float y = text.getScaleY();
text.setScaleX((float) (x + 1));
text.setScaleY((float) (y + 1));
}
});
zoomout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float x1 = text.getScaleX();
float y1 = text.getScaleY();
text.setScaleX((float) (x1 - 1));
text.setScaleY((float) (y1 - 1));
}
});
}
}