How to use magnification controls in TextView in Android?

I want to enlarge the text displayed in the center of the screen at the choice of the user. How can i achieve this? Using pinch multitouch cannot be tested on an emulator, and I want something that I can test on an Android emulator. Can I use the zoom in and out buttons to control only the text view for my layout? Or Can I use webview to place text as webview has default zoom buttons?

+3
source share
3 answers

Can I use the zoom in and out buttons to control only the text view for my layout?

. , , , 2D- API (Canvas) . TextView .

+1

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

To zoom in

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));
            }
        });
    }
}
0
source

Source: https://habr.com/ru/post/1745417/


All Articles