如何使文字淡入和淡出的Andr​​oid的?文字、Andr、oid

2023-09-04 12:20:11 作者:至少、我们还有一丝丝纯真

我有一段文字和一个按钮被按下时,我想的文本淡出,切换到其他的文本,然后渐退。我有一些code,但它不会做淡出动画只是淡入。

 最后的TextView mSwitcher =(TextView中)findViewById(R.id.bookContent);
    mSwitcher.setText(旧文本);

    在=新AlphaAnimation(0.0,1.0F)最终动画;
    in.setDuration(3000);

    最终的动画了=新AlphaAnimation(1.0F,0.0);
    out.setDuration(3000);

    巴顿这个MoveOn =(按钮)findViewById(R.id.moveOn);
    moveOn.setOnClickListener(新OnClickListener(){
        公共无效的onClick(视图v){

            mSwitcher.startAnimation(出);
            mSwitcher.setText(新文本);
            mSwitcher.startAnimation(在);

        }
    });
 

解决方案

您似乎动画被设置在右侧,您已经将其设为了之后。这使得只有在动画作品。

要做出正确的之后的第一个第二个动画开始,你可以添加一个监听到你的第一个动画:

  out.setAnimationListener(新AnimationListener(){

    @覆盖
    公共无效onAnimationEnd(动画动画){
        mSwitcher.setText(新文本);
        mSwitcher.startAnimation(在);

    }
});
 

然后,在你的的onClick()方法:

 公共无效的onClick(视图v){

    mSwitcher.startAnimation(出);

}
 

这是应该做的伎俩。

另一种方法是使用 AnimationSet

 在=新AlphaAnimation(0.0,1.0F)最终动画;
in.setDuration(3000);

最终的动画了=新AlphaAnimation(1.0F,0.0);
out.setDuration(3000);

AnimationSet为=新AnimationSet(真正的);
as.addAnimation(出);
in.setStartOffset(3000);
as.addAnimation(在);
 

而不是启动退出

然后,启动

我希望这有助于!

I have a paragraph of text and when a button is clicked I want that text to fade out, change to some other text, then fade back in. I have some code but it doesn't do the fade out animation just the fade in.

    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);

        }
    });

解决方案

You seem to be setting the animation to in right after you had set it to out. This makes only the "in" animation work.

To make the second animation start right after the first, you can add a listener to your first animation:

out.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationEnd(Animation animation) {
        mSwitcher.setText("New Text");
        mSwitcher.startAnimation(in);

    }
});

Then, in your onClick() method:

public void onClick(View v) {

    mSwitcher.startAnimation(out);

}

That should do the trick.

Another approach is to use 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);

Then, instead of starting out, start as.

I hope this helps!