为了补充eboix的答案...这是我如何淡入文本和淡出文本,每次淡入和淡出之间都有延迟(即在淡入后立即)。
我的XML看起来像这样。
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="Retrieving Result"
android:textColor="@color/general_app_colour"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/blobText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="Please Wait" />
</LinearLayout>
当您在活动/片段/对话框片段中使用这些变量时,以下是我在我的项目中使用的变量...
public class Loading_Dialog extends DialogFragment {
public String[] text = new String[]{""};
TextView blobText;
Animation inAnimation;
Animation displayLength;
Animation delayAnimation;
Animation outAnimation;
int fadeEffectDuration = 700;
int delayDuration = 1000;
int displayFor = 2000;
public String[] text = new String[]{""};
现在初始化的对象和变量可以像这样使用,我在我的对话框片段中使用了它,在oncreateDialog方法中。
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity(),R.style.LoadingDialogAnimation);
dialog.getWindow().setContentView(R.layout.dialog_loading);
blobText = (TextView) dialog.findViewById(R.id.blobText);
inAnimation = new AlphaAnimation(0f, 1f);
inAnimation.setDuration(fadeEffectDuration);
displayLength = new AlphaAnimation(1f, 1f);
displayLength.setDuration(displayFor);
delayAnimation = new AlphaAnimation(0f, 0f);
delayAnimation.setDuration(delayDuration);
outAnimation = new AlphaAnimation(1f, 0f);
outAnimation.setDuration(fadeEffectDuration);
inAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
position++;
if(position>=text.length)
{
position = 0;
}
blobText.setText(text[position]);
}
@Override
public void onAnimationRepeat(Animation animation) {}
@Override
public void onAnimationEnd(Animation animation) {
blobText.startAnimation(displayLength);
}
});
displayLength.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
blobText.startAnimation(outAnimation);
}
});
outAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
blobText.startAnimation(delayAnimation);
}
});
delayAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
blobText.startAnimation(inAnimation);
}
});
blobText.startAnimation(outAnimation);
AnimationListener
实现中设置淡入动画之前或之后的文本会防止淡入动画发生--新文本只是立即出现。 - shoe