使用“运行”方法和处理程序在 TextView 上不断设置动画并在 TextView 上不断更改文本
Constantly setting animation on TextView and Constantly changing text on TextView Using “run” Method and Handler
我有一个包含字符串值的字符串数组,它会在一段时间后一个一个地显示在 TextView 上。它在数组的最后一个索引处停止。我想重新开始将 TextView 的文本从索引零更改为最后一个索引。
我有一个应用于上述 TextView 的动画。动画在开始时只显示一次结果,之后就不再显示结果。
这是我的 Java 代码。
Animation animMove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);
doTask();
animMove = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.move);
}
public void doTask() {
final String[] my_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
final android.os.Handler handler = new android.os.Handler();
handler.post(new Runnable() {
int i = 0;
@Override
public void run() {
animMove.setRepeatCount(Animation.INFINITE);
intelStoryTextView.setAnimation(animMove);
intelStoryTextView.setText(my_string[i++]);
if (i == my_string.length) {
i = 0;
handler.removeCallbacks(this);
} else {
handler.postDelayed(this, 1000 * 5);
}
}
});
}
这是我的动画代码。
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
<translate
android:duration="2000"
android:fromXDelta="70%p"
android:toXDelta="0%p"
/>
</set>
试试下面的代码,我已经测试过了,它应该可以正常工作:
你的move.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<alpha
android:duration="2000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0"
android:repeatCount="infinite"
android:repeatMode="restart" /> <!-- i've added repeatCount and repeatMode attr -->
<translate
android:duration="2000"
android:fromXDelta="70%p"
android:toXDelta="0%p"
android:repeatCount="infinite"
android:repeatMode="restart"/> <!-- i've added repeatCount and repeatMode attr -->
</set>
你的activity:
AnimationSet animMove; // i changed Animation to AnimationSet because i need the list of animations
TextView intelStoryTextView;
String[] my_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
int position = 0; // a counter for your list of strings
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);
animMove = (AnimationSet) AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.move);
// I get an animation from the animations list (i choose randomly the first one)
Animation alphaAnim = animMove.getAnimations().get(0);
// I add a listener to this animation to update text when repeating
alphaAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
// here i do the update
if (position >= my_string.length) {
position = 0;
}
intelStoryTextView.setText(my_string[position]);
position++;
}
});
// I start the animation
intelStoryTextView.startAnimation(animMove);
}
更新
如果你想在重复之前停止无限动画一段时间,你可以使用 Handler
而不是 repeartCount
标签:
move.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<alpha
android:duration="2000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
<translate
android:duration="2000"
android:fromXDelta="70%p"
android:toXDelta="0%p"/>
</set>
你的activity
Animation mAnimMove;
TextView mIntelStoryTextView;
String[] mMy_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
int mPosition = 0;
int mInterval = 5000;
Handler mHandler = new Handler();
Runnable mRunnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);
mAnimMove = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.move);
mRunnable = new Runnable() {
@Override
public void run() {
if (mPosition >= mMy_string.length) {
mPosition = 0;
}
mIntelStoryTextView.setText(mMy_string[mPosition]);
mPosition++;
mIntelStoryTextView.startAnimation(mAnimMove);
mHandler.postDelayed(mRunnable, mInterval);
}
};
mRunnable.run();
}
我建议不要使用'move'作为动画名称。这可能会覆盖 android 默认移动动画并破坏默认底部 sheet 对话动画。
我有一个包含字符串值的字符串数组,它会在一段时间后一个一个地显示在 TextView 上。它在数组的最后一个索引处停止。我想重新开始将 TextView 的文本从索引零更改为最后一个索引。
我有一个应用于上述 TextView 的动画。动画在开始时只显示一次结果,之后就不再显示结果。 这是我的 Java 代码。
Animation animMove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);
doTask();
animMove = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.move);
}
public void doTask() {
final String[] my_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
final android.os.Handler handler = new android.os.Handler();
handler.post(new Runnable() {
int i = 0;
@Override
public void run() {
animMove.setRepeatCount(Animation.INFINITE);
intelStoryTextView.setAnimation(animMove);
intelStoryTextView.setText(my_string[i++]);
if (i == my_string.length) {
i = 0;
handler.removeCallbacks(this);
} else {
handler.postDelayed(this, 1000 * 5);
}
}
});
}
这是我的动画代码。
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
<translate
android:duration="2000"
android:fromXDelta="70%p"
android:toXDelta="0%p"
/>
</set>
试试下面的代码,我已经测试过了,它应该可以正常工作:
你的move.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<alpha
android:duration="2000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0"
android:repeatCount="infinite"
android:repeatMode="restart" /> <!-- i've added repeatCount and repeatMode attr -->
<translate
android:duration="2000"
android:fromXDelta="70%p"
android:toXDelta="0%p"
android:repeatCount="infinite"
android:repeatMode="restart"/> <!-- i've added repeatCount and repeatMode attr -->
</set>
你的activity:
AnimationSet animMove; // i changed Animation to AnimationSet because i need the list of animations
TextView intelStoryTextView;
String[] my_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
int position = 0; // a counter for your list of strings
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);
animMove = (AnimationSet) AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.move);
// I get an animation from the animations list (i choose randomly the first one)
Animation alphaAnim = animMove.getAnimations().get(0);
// I add a listener to this animation to update text when repeating
alphaAnim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
// here i do the update
if (position >= my_string.length) {
position = 0;
}
intelStoryTextView.setText(my_string[position]);
position++;
}
});
// I start the animation
intelStoryTextView.startAnimation(animMove);
}
更新
如果你想在重复之前停止无限动画一段时间,你可以使用 Handler
而不是 repeartCount
标签:
move.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<alpha
android:duration="2000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
<translate
android:duration="2000"
android:fromXDelta="70%p"
android:toXDelta="0%p"/>
</set>
你的activity
Animation mAnimMove;
TextView mIntelStoryTextView;
String[] mMy_string = {"Relief Rally", "OEDC Upgrades ", "US Fed to HIKE ", "Probability "};
int mPosition = 0;
int mInterval = 5000;
Handler mHandler = new Handler();
Runnable mRunnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIntelStoryTextView = (TextView) findViewById(R.id.intel_story_textview);
mAnimMove = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.move);
mRunnable = new Runnable() {
@Override
public void run() {
if (mPosition >= mMy_string.length) {
mPosition = 0;
}
mIntelStoryTextView.setText(mMy_string[mPosition]);
mPosition++;
mIntelStoryTextView.startAnimation(mAnimMove);
mHandler.postDelayed(mRunnable, mInterval);
}
};
mRunnable.run();
}
我建议不要使用'move'作为动画名称。这可能会覆盖 android 默认移动动画并破坏默认底部 sheet 对话动画。