Android动画中的平滑文本更改

时间:2017-01-18 09:04:45

标签: android animation

我想在android文本视图中添加动画,这样当文本发生变化时,它应该平滑而缓慢地变化。喜欢,当文本改变时淡入或淡出。在Android中使用动画是否可行?我做到目前为止;

主要活动

public class MainActivity extends AppCompatActivity {

    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn);
         tv = (TextView)findViewById(R.id.textview);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    tv.setText(String.valueOf(Integer.valueOf((String) tv.getText()) + 1));
            }
        });
    }
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:gravity="center_horizontal"
    tools:context="com.example.namal.smoothtextchange.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:id="@+id/textview"
        android:textSize="150sp"

        android:layout_height="wrap_content"
        android:text="0" />

    <Button
        android:text="Change"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textview"
        android:id="@+id/btn" />
</RelativeLayout>

7 个答案:

答案 0 :(得分:14)

使用TextSwitcher

<TextSwitcher
android:layout_marginTop="50dp"
android:id="@+id/textSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>

要在TextSwitcher中显示的字符串数组

String textToShow[] = {"Main HeadLine", "Your Message", "New In Technology"};

你必须设置动画

mSwitcher.setInAnimation(context, android.R.anim.slide_in_left);
mSwitcher.setOutAnimation(context, android.R.anim.slide_out_right);

通过调用方法setText(CharSequence text)

触发动画
// When clicked on Button TextSwitcher will switch between texts
btnNext.setOnClickListener(new View.OnClickListener() {
    public void onClick (View v) {

       currentIndex++;
       // If index reaches maximum reset it
       if (currentIndex == messageCount) {
          currentIndex = 0;
       }

       mSwitcher.setText(textToShow[currentIndex]);
}):

如果您想设置没有动画的文字,请调用方法setCurrentText(CharSequence text)

答案 1 :(得分:5)

如果您想支持Android 4+。看看Transitions Everywhere lib。您可以通过向后兼容来实现各种不同的动画。

Here你可以找到一些例子。

只需几行就可以了!

TransitionManager.beginDelayedTransition(transitionsContainer,
        new ChangeText().setChangeBehavior(ChangeText.CHANGE_BEHAVIOR_OUT_IN));

现在你要做的就是改变文字,为你完成所有魔法。

答案 2 :(得分:2)

您可以使用 TranslateAnimation

  

控制对象位置的动画。

    btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             TranslateAnimation animObj= new TranslateAnimation(0,tv.getWidth(), 0, 0);
             animObj.setDuration(2000);
             tv.startAnimation(animObj);
             tv.setText(String.valueOf(Integer.valueOf((String) tv.getText()) + 1));
            }
        });

您可以查看下面的演示案例链接

  1. Android Animation Example
  2. Android Working with XML Animations

答案 3 :(得分:2)

将android:animateLayoutChanges = true添加到root视图,然后添加到

后面的代码中
  call this line of code at onCreate


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    ((ViewGroup) findViewById(R.id.llRoot)).getLayoutTransition()
      .enableTransitionType(LayoutTransition.CHANGING);
   }

答案 4 :(得分:1)

最简单的方法是:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                        ViewGroup parent=(ViewGroup) tv.getParent();                 //get parent of the TextView
                        parent.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);            //enable animations
                        tv.setText(String.valueOf(Integer.valueOf((String) tv.getText()) + 1));                   //change the text
        }
    });

现在,您的文字应该变的很漂亮

答案 5 :(得分:0)

onCreate()中使用它,然后在textview中更改文本:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout parentLayout = (LinearLayout)view.findViewById(R.id.container);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            LayoutTransition layoutTransition = new LayoutTransition();
            layoutTransition.enableTransitionType(LayoutTransition.CHANGING);
            parentLayout.setLayoutTransition(layoutTransition);
        }    
        ......    
    }

答案 6 :(得分:0)

这是我对fadOut fadIn的扩展。

// TextViewExtensions

fun TextView.setTextAnimation(text: String, duration: Long = 300, completion: (() -> Unit)? = null) {
    fadOutAnimation(duration) {
        this.text = text
        fadInAnimation(duration) {
            completion?.let {
                it()
            }
        }
    }
}
// ViewExtensions

fun View.fadOutAnimation(duration: Long = 300, visibility: Int = View.INVISIBLE, completion: (() -> Unit)? = null) {
    animate()
            .alpha(0f)
            .setDuration(duration)
            .withEndAction {
                this.visibility = visibility
                completion?.let {
                    it()
                }
            }
}

fun View.fadInAnimation(duration: Long = 300, completion: (() -> Unit)? = null) {
    alpha = 0f
    visibility = View.VISIBLE
    animate()
            .alpha(1f)
            .setDuration(duration)
            .withEndAction {
                completion?.let {
                    it()
                }
            }
}
  • 示例:

textView。 setTextWithAnimation(“ H​​ello world”,500)