Is there a way to animate only the TextView's text size without scaling the entire TextView's layout?
Am trying to achieve a similar effect, Note the text re-sizes to a single line while its size becomes smaller.
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.
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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With