You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TextView文本大小动画时保持居中的实现求助

解决TextView中心缩放动画的问题

我太懂这种困扰了!TextView 默认缩放原点是左上角,你之前尝试的 setPivotX/Y 和 Gravity 思路方向没错,但问题大概率出在设置时机或者参数计算方式上。下面给你几个靠谱的解决方案:

方案一:用属性动画(ObjectAnimator)+ 正确设置Pivot时机

如果直接在 onCreate 里调用 setPivotX/Y,TextView 还没完成测量(宽高都是0),相当于把 pivot 设到了(0,0),自然从左上角缩放。你需要等 View 布局完成后再设置:

textView.post(new Runnable() {
    @Override
    public void run() {
        // 将 pivot 设置为 TextView 的中心位置
        textView.setPivotX(textView.getWidth() / 2f);
        textView.setPivotY(textView.getHeight() / 2f);
        
        // 创建缩放动画:从1倍放大到2倍
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(textView, "scaleX", 1f, 2f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(textView, "scaleY", 1f, 2f);
        // 创建颜色切换动画:黑→绿
        ObjectAnimator colorAnim = ObjectAnimator.ofArgb(textView, "textColor", Color.BLACK, Color.GREEN);
        
        // 组合动画同时执行
        AnimatorSet animSet = new AnimatorSet();
        animSet.playTogether(scaleX, scaleY, colorAnim);
        animSet.setDuration(1000); // 动画时长1秒
        animSet.start();
    }
});

post() 方法会把 Runnable 放到 View 的消息队列里,等 View 完成绘制、拿到正确宽高后再执行,这时候设置的 pivot 才是真正的中心。

方案二:用ScaleAnimation(无需等待View测量)

如果不想处理测量时机,可以直接用 ScaleAnimation,通过设置相对比例来指定中心缩放,这种方式更省心:

// 创建缩放动画:以自身中心为原点,从1倍到2倍
ScaleAnimation scaleAnim = new ScaleAnimation(
    1f, 2f, // X轴从1倍到2倍
    1f, 2f, // Y轴从1倍到2倍
    Animation.RELATIVE_TO_SELF, 0.5f, // X轴原点:自身宽度的50%(中心)
    Animation.RELATIVE_TO_SELF, 0.5f  // Y轴原点:自身高度的50%(中心)
);
scaleAnim.setDuration(1000);

// 颜色动画和之前一样
ObjectAnimator colorAnim = ObjectAnimator.ofArgb(textView, "textColor", Color.BLACK, Color.GREEN);
colorAnim.setDuration(1000);

// 组合执行
AnimatorSet animSet = new AnimatorSet();
animSet.playTogether(scaleAnim, colorAnim);
animSet.start();

这里的 RELATIVE_TO_SELF + 0.5f 直接指定了缩放原点为 View 自身的中心,不管 View 有没有完成测量,动画都会从中心开始。

方案三:XML定义动画(适合复用)

如果需要复用动画,可以用 XML 来定义,同样支持中心缩放:

在 res/anim/scale_center_color.xml 里写:

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:fromXScale="1.0"
        android:toXScale="2.0"
        android:fromYScale="1.0"
        android:toYScale="2.0"
        android:pivotX="50%" <!-- 相对自身宽度50% -->
        android:pivotY="50%" <!-- 相对自身高度50% -->
        android:duration="1000"/>
    <objectAnimator
        android:propertyName="textColor"
        android:fromValue="#000000" <!-- 黑色 -->
        android:toValue="#00FF00" <!-- 绿色 -->
        android:duration="1000"/>
</set>

然后在代码里加载执行:

AnimatorSet animSet = (AnimatorSet) AnimatorInflater.loadAnimation(this, R.anim.scale_center_color);
animSet.setTarget(textView);
animSet.start();

为什么之前的方法没用?

  • setGravity(Gravity.Center) 只是控制文本在 TextView 内部的对齐方式,和缩放原点无关;
  • 直接在 onCreate 里设 setPivotX/Y 时,TextView 还没测量,宽高为0,等于把 pivot 设到了左上角;
  • Translate 动画是移动整个 View,不是调整缩放原点,所以会导致整个控件移位,不是你要的效果。

内容的提问来源于stack exchange,提问作者Patrick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:48:10