Android:文字淡入淡出 [英] Android: Text fade in and out
本文介绍了Android:文字淡入淡出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经阅读了这个stackoverflow问题和答案,并尝试实现文本淡入和淡出:
I've read this stackoverflow question and answer and tried to implement a text fade in and out:
这是我的实现:
public class ShowActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
final TextView mSwitcher = (TextView) findViewById(R.id.textFade);
mSwitcher.setText("old text");
final Animation in = new AlphaAnimation(0.0f, 1.0f);
in.setDuration(5000);
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(5000);
out.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
mSwitcher.setText("New Text");
mSwitcher.startAnimation(in);
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
mSwitcher.startAnimation(out);
mSwitcher.setText("Text 1.");
mSwitcher.startAnimation(in);
mSwitcher.startAnimation(out);
mSwitcher.setText("Text 2.");
mSwitcher.startAnimation(in);
}
}
问题是,只有文字2出现,它只会淡入而不淡出。可能有什么问题?
The problem is, that only text 2 appears and it only fade in and not fade out. What could be wrong?
推荐答案
问题是你每次开始淡出动画时都会立即开始淡出动画。
The problem is that you are starting a fade in animation immediately every time you start a fade out animation.
我能够修改你的代码并得到一个简单的例子,这里是代码:
I was able to modify your code and get a simple example working, here's the code:
import android.os.Handler;
public class ShowActivity extends Activity
{
Handler handler;
TextView mSwitcher;
Animation in;
Animation out;
int fadeCount;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show);
fadeCount = 0;
handler = new Handler();
mSwitcher = (TextView) findViewById(R.id.textView);
mSwitcher.setText("old text");
in = new AlphaAnimation(0.0f, 1.0f);
in.setDuration(5000);
out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(5000);
out.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
fadeCount++;
if (fadeCount == 3){
mSwitcher.setText("");
Intent i = new Intent(getApplication() , MainActivity.class);
startActivity(i);
}
else {
if (fadeCount == 1) {
mSwitcher.setText("Text 2.");
} else {
mSwitcher.setText("New Text");
}
mSwitcher.startAnimation(in);
handler.postDelayed(mFadeOut, 5000);
}
}
@Override
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}
});
//mSwitcher.startAnimation(out);
mSwitcher.setText("Text 1.");
mSwitcher.startAnimation(in);
/*
mSwitcher.startAnimation(out);
mSwitcher.setText("Text 2.");
mSwitcher.startAnimation(in);
*/
handler.postDelayed(mFadeOut, 5000);
}
private Runnable mFadeOut =new Runnable(){
@Override
public void run() {
//Speed up the last fade-out so that the Activity opens faster
if (fadeCount == 2){
out.setDuration(2000);
}
mSwitcher.startAnimation(out);
}
};
}
这篇关于Android:文字淡入淡出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文