我愿意在 android 应用程序中创建一个新闻栏,并使用动画将 TextView 从左向右移动,并在每一轮中用字符串数组中的新文本填充它。我遇到了很多问题,我使用了超过 5 种方法,但每一种都有不好的行为。主要问题是动画结束时它会刷新 textView 并且看起来像闪烁,这不是用户友好的行为。
这是一个代码片段:
TextView my_text;
Animation slideRight;
Animation slideLeft;
String [] text = {"TwoOneOneOneOneOneOneOneOneOneTwo","OneTwoTwoTwoTwoTwoTwoTwoTwoTwoTwoOne",
"OneThreeThreeThreeThreeThreeThreeThreeOne","OneFourFourFourFourFourFourFourOne",
"OneFiveFiveFiveFiveFiveFiveFiveOne","OneSixSixSixSixSixSixSixOne",
"OneSevenSevenSevenSevenSevenSevenSeveOne","OneEightEightEightEightEightEightEightOne",
"OneNineNineNineNineNineNineNineOne"};
int arr_length;
int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
arr_length = text.length;
slideRight = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
slideRight.setDuration(2000);
slideRight.setRepeatCount(Animation.INFINITE);
slideRight.setAnimationListener(slideRightListener);
slideLeft = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
slideLeft.setDuration(2000);
slideLeft.setAnimationListener(slideLeftListener);
my_text = (TextView) findViewById(R.id.textView1);
my_text.setVisibility(TextView.VISIBLE);
my_text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), my_text.getText(), Toast.LENGTH_SHORT).show();
}
});
slideLeft.setAnimationListener(slideLeftListener);
my_text.startAnimation(slideLeft);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
AnimationListener slideLeftListener = new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
my_text.startAnimation(slideRight);
}
};
AnimationListener slideRightListener = new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
my_text.setVisibility(TextView.VISIBLE);
if(count<arr_length)
{
my_text.setText(text[count]);
count+=1;
my_text.startAnimation(slideLeft);
}
else
{
count=0;
my_text.setText(text[count]);
my_text.startAnimation(slideLeft);
}
}
};
}