Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animation of a TextView's text size and not the entire TextView

Tags:

Is there a way to animate only the TextView's text size without scaling the entire TextView's layout?

enter image description here

Am trying to achieve a similar effect, Note the text re-sizes to a single line while its size becomes smaller.

like image 561
cozeJ4 Avatar asked May 19 '15 11:05

cozeJ4


People also ask

Which method is used to change the text size of the TextView?

To use preset sizes to set up the autosizing of TextView programmatically, call the setAutoSizeTextTypeUniformWithPresetSizes(int[] presetSizes, int unit) method. Provide an array of sizes and any TypedValue dimension unit for the size.

How do you animate TextView?

To start the animation we need to call the startAnimation() function on the UI element as shown in the snippet below: sampleTextView. startAnimation(animation); Here we perform the animation on a textview component by passing the type of Animation as the parameter.

How do I limit text in TextView?

you can extend the TextView class and overwrite the setText() function. In this function you check for text length or word cound. Better than counting the text length or the word cound a better way would be to use the "maxLines" attribute along with "ellipsize" attribute to attain the desired effect.

Which attribute increases or decreases the font size of TextView?

app:autoSizeMinTextSize=”10sp” using this attribute the TextView will be resized up to the size of 10sp and app:autoSizeStepGranularity=”2sp” using this attribute we are uniformly reducing the size of the TextView as 2sp when it goes out of the screen.


1 Answers

This could be achieved with a ValueAnimator and from the top of my head I think it should look something like this:

final TextView tv = new TextView(getApplicationContext());  final float startSize = 42; // Size in pixels final float endSize = 12; long animationDuration = 600; // Animation duration in ms  ValueAnimator animator = ValueAnimator.ofFloat(startSize, endSize); animator.setDuration(animationDuration);  animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {     @Override     public void onAnimationUpdate(ValueAnimator valueAnimator) {         float animatedValue = (float) valueAnimator.getAnimatedValue();         tv.setTextSize(animatedValue);     } });  animator.start(); 
like image 116
korrekorre Avatar answered Sep 21 '22 14:09

korrekorre