我已阅读此 stackoverflow 问题和答案,并尝试实现文本淡入和淡出:
Android中如何让文字淡入淡出? https://stackoverflow.com/questions/8627211/how-to-make-text-fade-in-and-out-in-android
这是我的实现:
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,并且它只淡入而不淡出。可能出什么问题了?
问题是每次开始淡出动画时都会立即开始淡入动画。
我能够修改您的代码并获得一个简单的示例,代码如下:
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);
}
};
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)