2010-05-14 95 views
3

我想按照用戶的選擇放大顯示在屏幕中心的文本。我怎樣才能做到這一點? 使用捏多點觸控無法在模擬器上測試,我想要一些我可以在Android模擬器上測試的東西。 我可以使用放大和縮小控件來只控制我的佈局的文本視圖嗎? 或者我可以使用webview來包含文本,因爲webview具有默認放大按鈕嗎?如何在Android中的TextView上使用縮放控件?

回答

1

我可以使用放大和縮小控件來控制 僅用於我的佈局的文本視圖嗎?

沒有內置的支持。您可以通過使用2D圖形API(Canvas)和觸摸事件繪製文本來達到這種效果。或者,攔截TextView上的觸摸事件並更改文字的字體大小。

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; 
    } 

} 
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)); 
      } 
     }); 
    } 
} 
相關問題