android动画中的平滑文本更改 [英] Smooth text change in android animation

查看:91
本文介绍了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

use 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中显示的字符串数组

Array of Strings to show in 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文本)

// 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(字符序列文本)

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆