How to make text fade in and out in Android?

后端 未结 6 609
说谎
说谎 2021-01-30 12:44

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

6条回答
  •  自闭症患者
    2021-01-30 13:30

    to add to eboix answer... this is how I fade in text and fade out text, with delay between each fade and before fade out, (i.e right after fade in).

    My XML looks like this.

        
    
        
    
    
    

    The you use this variables in in your activity/fragment/dialogfragment, the following are the variables I used in mine...

    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[]{""};
    

    Now the objects and variables are innitialized are used like this, I used this for my dialog fragment, in the oncreateDialog method..

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

提交回复
热议问题