2014-09-11 20 views
10

Tôi mới sử dụng Android và tôi không biết nhiều về hoạt ảnh. Tôi có một trình xem ảnh và tôi muốn tạo hiệu ứng động giữa các hình ảnh bên trong nó. này là mã:android- đơn giản mờ dần và mờ dần trong hoạt ảnh cho màn hình xem

 runnable = new Runnable() { 
      public void run() { 
      handler.postDelayed(runnable, 3000); 
      imageViewFlipper.setInAnimation(fadeIn); 
      imageViewFlipper.setOutAnimation(fadeOut); 
      imageViewFlipper.showNext(); 
      } 
      }; 
      handler = new Handler(); 
      handler.postDelayed(runnable, 500); 
    } 

2 tập phim hoạt hình không tốt, họ động rất nặng nề. Tôi chỉ cần một mã để làm mờ hình ảnh phía trước và mờ dần trong hình ảnh tiếp theo và làm tương tự cho tất cả các hình ảnh bên trong nó.

Bạn có thể giúp tôi và cho tôi một cái gì đó như thế không?

nhờ bạn

+0

thử hướng dẫn này: http: //www.androidhive.info/2013/06/android-working-with-xml-animations/ – prakash

Trả lời

24

Tham khảo những liên kết này, họ cũng có những hình ảnh động xml của:

Link 1 & Link 2

Ví dụ Class:

public class ViewFlipperMainActivity extends Activity 
{ 
    private ViewFlipper viewFlipper; 
    private float lastX; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.view_flipper_main); 
       viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper); 
    } 



    // Method to handle touch event like left to right swap and right to left swap 
    public boolean onTouchEvent(MotionEvent touchevent) 
    { 
    switch (touchevent.getAction()) 
    { 
     // when user first touches the screen to swap 
     case MotionEvent.ACTION_DOWN: 
     { 
      lastX = touchevent.getX(); 
      break; 
     } 
     case MotionEvent.ACTION_UP: 
     { 
     float currentX = touchevent.getX(); 

     // if left to right swipe on screen 
     if (lastX < currentX) 
     { 
       // If no more View/Child to flip 
      if (viewFlipper.getDisplayedChild() == 0) 
       break; 

      // set the required Animation type to ViewFlipper 
      // The Next screen will come in form Left and current Screen will go OUT from Right 
      viewFlipper.setInAnimation(this, R.anim.in_from_left); 
      viewFlipper.setOutAnimation(this, R.anim.out_to_right); 
      // Show the next Screen 
      viewFlipper.showNext(); 
     } 

     // if right to left swipe on screen 
     if (lastX > currentX) 
     { 
      if (viewFlipper.getDisplayedChild() == 1) 
       break; 
      // set the required Animation type to ViewFlipper 
      // The Next screen will come in form Right and current Screen will go OUT from Left 
      viewFlipper.setInAnimation(this, R.anim.in_from_right); 
      viewFlipper.setOutAnimation(this, R.anim.out_to_left); 
      // Show The Previous Screen 
      viewFlipper.showPrevious(); 
     } 
     break; 
     } 
     } 
    return false; 
    } 
} 

Fade In-Out Usiong Mã Java :

Animation fadeIn = new AlphaAnimation(0, 1); 
fadeIn.setInterpolator(new DecelerateInterpolator()); //add this 
fadeIn.setDuration(1500); //time in milliseconds 

Animation fadeOut = new AlphaAnimation(1, 0); 
fadeOut.setInterpolator(new AccelerateInterpolator()); //and this 
fadeOut.setStartOffset(1000); 
fadeOut.setDuration(1500); //time in milliseconds 

AnimationSet animation = new AnimationSet(false); //change to false 
animation.addAnimation(fadeIn); 
animation.addAnimation(fadeOut); 
this.setAnimation(animation); 

Animation Xml:

Fade In:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/linear_interpolator"> 
    <alpha 
     android:fromAlpha="0.1" 
     android:toAlpha="1.0" 
     android:duration="2000" 
     /> 
</set> 

Fade Out:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/linear_interpolator"> 
    <alpha 
     android:fromAlpha="1.0" 
     android:toAlpha="0.1" 
     android:duration="2000" 
     /> 
</set> 

in_from_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <translate 
     android:fromXDelta="-100%" android:toXDelta="0%" 
      android:fromYDelta="0%" android:toYDelta="0%" 
      android:duration="1400" /> 
</set> 

in_from_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <translate 
     android:fromXDelta="100%" android:toXDelta="0%" 
      android:fromYDelta="0%" android:toYDelta="0%" 
      android:duration="1400" /> 
</set> 

out_to_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
     <translate android:fromXDelta="0%" android:toXDelta="-100%" 
     android:fromYDelta="0%" android:toYDelta="0%" 
     android:duration="1400"/> 
</set> 

out_to_right.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
     <translate android:fromXDelta="0%" android:toXDelta="100%" 
     android:fromYDelta="0%" android:toYDelta="0%" 
     android:duration="1400"/> 
</set> 

view_flipper_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ViewFlipper 
     android:id="@+id/view_flipper" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <!-- The child Views/Layout to flip --> 
     <ImageView 
      android:layout_marginTop="15dp" 
      android:id="@+id/imageView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:src="@drawable/image1" /> 
     <ImageView 
      android:layout_marginTop="15dp" 
      android:id="@id/imageView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:src="@drawable/image2" /> 
    </ViewFlipper> 
</LinearLayout> 

EDIT:

Đã hơn 2 năm và rất nhiều người dường như được đề cập đến câu trả lời này, vì vậy để giúp tất cả các hiệu ứng chuyển tiếp với thiết kế Vật liệu và một số các chuyển đổi mới ở đây là một vài liên kết tham chiếu.

Animate all the things. Transitions in Android

Material-Animations

+1

rất tiện dụng để có tất cả ở đây:) – rupps

+1

@Sagar Pilkhwal bạn đã tiết kiệm thời gian của tôi +1 phiếu bầu cho bạn. –

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