2010-04-08 66 views
37

Tôi muốn hiển thị ảnh trên màn hình Hoạt động trên Android bằng cách thực hiện dần dần và liên tục mờ dần từ màu nâu đỏ đơn điệu đến màu đầy đủ cuối cùng. Tôi biết làm thế nào để làm điều đó trên một hình ảnh Java/BufferedImage cho đối tượng đồ họa nhưng tiếc là tôi không biết gì cho môi trường lập trình Android. Bất cứ ai có thể giúp đỡ?Làm cách nào để thực hiện fadein của hình ảnh trên màn hình Hoạt động Android?

Trả lời

6

Một phương pháp cho việc này là sử dụng bộ hoạt ảnh. Xem ở đây;

http://developer.android.com/guide/topics/resources/available-resources.html#animation

Một số mã ví dụ tôi đã làm (vòng lặp vô hạn mờ dần trong ví dụ này);

Trong tệp .xml hoạt ảnh;

<alpha android:fromAlpha="1.0" 
     android:toAlpha="0.3" 
     android:duration="7000" 
     android:repeatMode="restart" 
     android:repeatCount="infinite"/> 

Trong tệp java;

ImageView introanim = (ImageView) findViewById(R.id.introanim); 
    Animation StoryAnimation = AnimationUtils.loadAnimation(this, R.anim.intro_anim); 
    introanim.startAnimation(StoryAnimation); 

Bạn có thể mờ dần từ màu nâu đỏ nền/hình ảnh của bạn để bất cứ điều gì bạn muốn ...

+0

Cảm ơn Jorge và Mike. Tôi sẽ thử cách làm của bạn. –

76

Hi Hiroshi bạn có thể làm điều này cho các phai trong:

ImageView myImageView= (ImageView)findViewById(R.id.myImageView); 
    Animation myFadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.fadein); 
    myImageView.startAnimation(myFadeInAnimation); //Set animation to your ImageView 

và bên trong res của bạn \ anim \ thư mục các tập tin hình ảnh động fadein.xml

<?xml version="1.0" encoding="UTF-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
     <alpha 
      android:fromAlpha="0.0" 
      android:toAlpha="1.0" 
      android:interpolator="@android:anim/accelerate_interpolator" 
      android:duration="3000"/> 
</set> 

nhưng đối với dần dần mờ dần từ màu nâu đỏ đến màu đầy đủ, yo u phải sử dụng TransitionDrawable

+4

Hoạt động tốt. Có thể bạn sẽ không cần 'android: repeatCount =" infinite "', mặc dù ... –

50

tôi muốn một hình ảnh mờ dần (và sau đó biến mất) một lần nhấp từ opacity đầy đủ để 0. Sau đây là cách tôi đã làm nó:

Animation a = new AlphaAnimation(1.00f, 0.00f); 

a.setDuration(1000); 
a.setAnimationListener(new AnimationListener() { 

    public void onAnimationStart(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

    public void onAnimationRepeat(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

    public void onAnimationEnd(Animation animation) { 
     yourView.setVisibility(View.GONE); 

    } 
}); 

yourView.startAnimation(a); 
+0

cảm ơn điều này đã giúp tôi với một hoạt hình bên trong một recyclerview – AndyRoid

Các vấn đề liên quan