2011-12-24 63 views
43

Tôi có một đoạn văn bản và khi một nút được nhấp, tôi muốn văn bản đó mờ đi, thay đổi sang một số văn bản khác, sau đó mờ dần. Tôi có một số mã nhưng nó không làm mờ dần hình ảnh động in.Làm thế nào để làm cho văn bản mờ dần trong và ngoài trong Android?

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

     } 
    }); 

Trả lời

73

Dường như bạn đang đặt hoạt ảnh ở ngay sau khi bạn đã đặt nó ra. Điều này chỉ làm cho công việc hoạt hình "trong".

Để thực hiện các hoạt hình thứ hai bắt đầu ngay sau khi người đầu tiên, bạn có thể thêm một người biết lắng nghe để hoạt hình đầu tiên của bạn:

out.setAnimationListener(new AnimationListener() { 

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

    } 
}); 

Sau đó, trong onClick() phương pháp của bạn:

public void onClick(View v) { 

    mSwitcher.startAnimation(out); 

} 

Điều đó sẽ làm lừa.


Cách tiếp cận khác là sử dụng 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); 

Sau đó, thay vì bắt đầu out, hãy bắt đầu as.

Tôi hy vọng điều này sẽ hữu ích!

+2

Tuyệt vời câu trả lời! :) –

+0

@eboxis Cảm ơn bạn đã chia sẻ kiến ​​thức..vì có cách nào để thực hiện hoạt ảnh theo trình tự cho nhiều lượt xem .. Tôi đã thử bắt đầu hoạt ảnh cho chế độ xem thứ hai trong hoạt ảnhEnd của chế độ xem đầu tiên ... nhưng khi tôi làm điều đó .. .first xem lại hoạt ảnh ... bất kỳ đề xuất nào ...? – CoDe

+0

Tuyệt vời! Cách tiếp cận thứ hai đã làm cho các trick cho tôi! Cảm ơn :) –

2

Bạn nên cân nhắc việc sử dụng một cái gì đó như TextSwitcher. Có một số document ngắn gọn trên TextSwitcher trong tài liệu Android. Những gì tôi muốn giới thiệu tốt nhất mặc dù là để xem các bản demo API, có một tuyệt vời và đơn giản để sử dụng một trong số TextSwitcher s. Tải xuống Bản trình diễn API và tự mình kiểm tra hoặc xem here.

3

Nếu bạn muốn sử dụng Animation, bạn có thể sử dụng AnimatorListener để nghe khi hoạt ảnh đầu tiên được thực hiện và sau đó bắt đầu hoạt ảnh thứ hai. Đó sẽ là onAnimationEnd().

biết thêm thông tin có sẵn ở đây: http://developer.android.com/reference/android/animation/Animator.AnimatorListener.html

Có thể có một cách tốt hơn để làm điều đó với AnimationSet, nhưng hoạt động này là chắc chắn.

2

để thêm vào câu trả lời eboix ... đây là cách tôi mờ dần trong văn bản và mờ dần văn bản, với độ trễ giữa mỗi phai và trước khi mờ dần, (nghĩa là ngay sau khi mờ dần).

XML của tôi trông như thế này.

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

Các bạn sử dụng các biến này trong các hoạt động của bạn/mảnh/dialogfragment, sau đây là các biến tôi được sử dụng trong tôi ...

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

Bây giờ các đối tượng và các biến được innitialized là được sử dụng như thế này, tôi đã sử dụng nó cho đoạn hộp thoại của mình, trong phương thức 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); 
0

Khi tôi có một số lượng văn bản để fadeIn/fadeOut, tôi prefere sử dụng một chức năng để làm điều này:

protected void onCreate(Bundle savedInstanceState) { 
{ 
... 
//Declare the array of texts: 
String aSentences[]={"Sentence 1", "Sentence 2", "<b><i>Sentence 3</i></b>"}; 
TextView tView = (TextView)findViewById(R.id.Name_TextView_Object); 

//call the function: 
animateText(tView,aSentences,0,false); 
} 

private void animateText(final TextView textView, final String texts[], final int textIndex, final boolean forever) { 
     //textView <-- The View which displays the texts 
     //texts[] <-- Holds R references to the texts to display 
     //textIndex <-- index of the first text to show in texts[] 
     //forever <-- If equals true then after the last text it starts all over again with the first text resulting in an infinite loop. You have been warned. 

     int fadeInDuration = 1000; // Configure time values here 
     int timeBetween = 5000; 
     int fadeOutDuration = 2000; 

     textView.setVisibility(View.INVISIBLE); //Visible or invisible by default - this will apply when the animation ends 
     textView.setText(Html.fromHtml(texts[textIndex])); 

     Animation fadeIn = new AlphaAnimation(0, 1); 
     fadeIn.setInterpolator(new DecelerateInterpolator()); // add this 
     fadeIn.setDuration(fadeInDuration); 

     Animation fadeOut = new AlphaAnimation(1, 0); 
     fadeOut.setInterpolator(new AccelerateInterpolator()); // and this 
     fadeOut.setStartOffset(fadeInDuration + timeBetween); 
     fadeOut.setDuration(fadeOutDuration); 

     AnimationSet animation = new AnimationSet(false); // change to false 
     animation.addAnimation(fadeIn); 
     if((texts.length-1) != textIndex) animation.addAnimation(fadeOut); 
     animation.setRepeatCount(1); 
     textView.setAnimation(animation); 

     animation.setAnimationListener(new Animation.AnimationListener() { 
      public void onAnimationEnd(Animation animation) { 
       if (texts.length -1 > textIndex) { 
        animateText(textView, texts, textIndex + 1,forever); //Calls itself until it gets to the end of the array 
       } 
       else { 
        textView.setVisibility(View.VISIBLE); 
        if (forever == true){ 
         animateText(textView, texts, 0,forever); //Calls itself to start the animation all over again in a loop if forever = true 
        } 
        else 
         {//do something when the end is reached} 
       } 
      } 
      public void onAnimationRepeat(Animation animation) { 
       // TODO Auto-generated method stub 
      } 
      public void onAnimationStart(Animation animation) { 
       // TODO Auto-generated method stub 
      } 
     }); 
    } 
Các vấn đề liên quan