我有一段文字,当点击一个按钮时,我希望该文本淡出,更改为其他一些文本,然后淡入。我有一些代码,但它不做淡出动画只是淡出。
final TextView mSwitcher = (TextView) findViewById(R.id.bookContent);
mSwitcher.setText("old text");
final Animation in = new AlphaAnimation(0.0f, 1.0f);
in.setDuration(3000);
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(3000);
Button moveOn = (Button) findViewById(R.id.moveOn);
moveOn.setOnClickListener( new OnClickListener() {
public void onClick(View v) {
mSwitcher.startAnimation(out);
mSwitcher.setText("new text");
mSwitcher.startAnimation(in);
}
});
答案 0 :(得分:80)
在将动画设置为out之后,您似乎正在设置动画。这只会让“in”动画起作用。
要使第二个动画在第一个动画之后立即开始,您可以为第一个动画添加一个侦听器:
out.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationEnd(Animation animation) {
mSwitcher.setText("New Text");
mSwitcher.startAnimation(in);
}
});
然后,在您的onClick()
方法中:
public void onClick(View v) {
mSwitcher.startAnimation(out);
}
这应该可以解决问题。
另一种方法是使用AnimationSet
。
final Animation in = new AlphaAnimation(0.0f, 1.0f);
in.setDuration(3000);
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(3000);
AnimationSet as = new AnimationSet(true);
as.addAnimation(out);
in.setStartOffset(3000);
as.addAnimation(in);
然后,不要启动out
,而是启动as
。
我希望这有帮助!
答案 1 :(得分:3)
如果您想使用Animation
,可以使用AnimatorListener
侦听第一个动画完成后,然后启动第二个动画。那将是onAnimationEnd()
。
此处提供了更多信息:http://developer.android.com/reference/android/animation/Animator.AnimatorListener.html
使用AnimationSet
可能有更好的方法,但这肯定有用。
答案 2 :(得分:2)
你应该考虑使用类似TextSwitcher
的东西。 Android文档中TextSwitcher
上有一个简短的document。我最好推荐的是看看API演示,使用TextSwitcher
之一是非常简单的。下载API演示并亲自查看,或参阅here。
答案 3 :(得分:2)
添加到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>
你在你的activity / fragment / dialogfragment中使用这些变量,以下是我在我的使用中的变量......
public class Loading_Dialog extends DialogFragment {
public String[] text = new String[]{""};
TextView blobText;
Animation inAnimation;
Animation displayLength;
Animation delayAnimation;
Animation outAnimation;
//duration for fade effects
int fadeEffectDuration = 700;
//duration for delay between fadeout and fadein
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) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
blobText.startAnimation(outAnimation);
}
});
outAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
blobText.startAnimation(delayAnimation);
}
});
delayAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
blobText.startAnimation(inAnimation);
}
});
blobText.startAnimation(outAnimation);
答案 4 :(得分:0)
当我有一些FadeIn / FadeOut的文本时,我首选使用一个函数来执行此操作:
public class CodenameOneTest {
private Form current;
private Resources theme;
private WaitingClass w;
private String[] properties = {"1 MAIN STREET", "123 E MAIN STREET", "12 EASTER ROAD", "24 MAIN STREET"};
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
}
public void start() {
if(current != null) {
current.show();
return;
}
Form form = new Form("AutoCompleteTextField");
form.setLayout(new BorderLayout());
final DefaultListModel<String> options = new DefaultListModel<>();
AutoCompleteTextField ac = new AutoCompleteTextField(options) {
protected boolean filter(String text) {
if(text.length() == 0) {
options.removeAll();
return false;
}
String[] l = searchLocations(text);
if(l == null || l.length == 0) {
return false;
}
options.removeAll();
for(String s : l) {
options.addItem(s);
}
return true;
};
};
Container container = new Container(BoxLayout.y());
container.setScrollableY(true); // If you comment this out then the field works fine
container.add(ac);
form.addComponent(BorderLayout.CENTER, container);
form.show();
}
String[] searchLocations(String text) {
try {
if(text.length() > 0) {
if(w != null) {
w.actionPerformed(null);
}
w = new WaitingClass();
String[] properties = getProperties(text);
if(Display.getInstance().isEdt()) {
Display.getInstance().invokeAndBlock(w);
}
else {
w.run();
}
return properties;
}
}
catch(Exception e) {
Log.e(e);
}
return null;
}
private String[] getProperties(String text) {
List<String> returnList = new ArrayList<>();
List<String> propertyList = Arrays.asList(properties);
for(String property : propertyList) {
if(property.startsWith(text)) {
returnList.add(property);
}
}
w.actionPerformed(null);
return returnList.toArray(new String[returnList.size()]);
}
class WaitingClass implements Runnable, ActionListener<ActionEvent> {
private boolean finishedWaiting;
public void run() {
while(!finishedWaiting) {
try {
Thread.sleep(30);
}
catch(InterruptedException ex) {
ex.printStackTrace();
}
}
}
public void actionPerformed(ActionEvent e) {
finishedWaiting = true;
return;
}
}
public void stop() {
current = Display.getInstance().getCurrent();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = Display.getInstance().getCurrent();
}
}
public void destroy() {
}
}
答案 5 :(得分:0)
有一种更好的方法可以达到相同的效果。
您应该将动画配置为在反向模式下重复。
这里是一个例子:
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setRepeatCount(Animation.INFINITE);
out.setRepeatMode(Animation.REVERSE);
out.setDuration(3000);
mSwitcher.startAnimation(out);