android动画中的平滑文本更改

android动画中的平滑文本更改

本文介绍了android动画中的平滑文本更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 android 文本视图中添加动画,以便当文本更改时它应该平滑缓慢地更改.例如,当文本更改时淡入或淡出.在 Android 中可以使用动画吗?我到目前为止;

I want to add animation in android text view so that when a text changes it should change smoothly and slowly. Like, fade in or fade out when the text changes. Is it possible in Android using animations? I did so far;

主要活动

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>

推荐答案

使用 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"/>
    <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
    <TextView
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />
</TextSwitcher>

显示在 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)

Animation is triggered by calling method 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 == textToShow.length) {
          currentIndex = 0;
       }

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

如果你想设置没有动画的文本,调用方法setCurrentText(CharSequence text).

If you want to set text without animation, call method setCurrentText(CharSequence text).

这篇关于android动画中的平滑文本更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 13:45