2012-07-10 105 views
5

我目前正在为我的对话框工作过渡效果。请参考下图:enter image description here对话框过渡效果

我的对话框的入口动画应该是从上到中。而退出动画应该是中间到顶部。我正在使用以下XML动画,但不幸的是,它们不起作用。

slide_down.xml

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<translate android:fromYDelta="100%p" android:toYDelta="0" 
android:duration="1000"/> 
</set> 

slide_up.xml

<?xml version="1.0" encoding="utf-8"?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android" 
android:fromYDelta="0%p" android:toYDelta="50%p" 
android:duration="1000"/> 

编辑:这不是一个平常Dialog。它是在AndroidManifest.xml

+0

你是如何将这些动画? – Joru 2012-07-10 13:09:26

+0

我想如果你打开“对话活动”,那么这个过渡很容易实现。 – 2012-07-10 13:09:44

+0

你在用什么? DialogFragment?将代码粘贴到您应用转换的地方并提交交易。 – 2012-07-10 13:11:41

回答

1

Theme.Dialog应用,如果你正在创建的对话作为一项活动,那么你可以按照这个方法

您可以创建动画类的activity

public class DropDownToMiddleAnimation extends Animation { 
    public int height, width; 

    @Override 
    public void initialize(int width, int height, int parentWidth, 
      int parentHeight) { 
     // TODO Auto-generated method stub 
     super.initialize(width, height, parentWidth, parentHeight); 
     this.width = width; 
     this.height = height; 
     setDuration(500); 
     setFillAfter(true); 
     setInterpolator(new LinearInterpolator()); 
    } 

    Camera camera = new Camera(); 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     // TODO Auto-generated method stub 
     super.applyTransformation(interpolatedTime, t); 

     Matrix matrix = t.getMatrix(); 
     camera.save(); 

     camera.getMatrix(matrix); 
     matrix.setTranslate(0, ((height/2) * interpolatedTime))); 

     matrix.preTranslate(0, -height); 
     camera.restore(); 

     this.setAnimationListener(this); 
    } 

和:

public class MiddleToTopAnimation extends Animation { 
    public int height, width; 

    @Override 
    public void initialize(int width, int height, int parentWidth, 
      int parentHeight) { 
     // TODO Auto-generated method stub 
     super.initialize(width, height, parentWidth, parentHeight); 
     this.width = width; 
     this.height = height; 
     setDuration(500); 
     setFillAfter(true); 
     setInterpolator(new LinearInterpolator()); 
    } 

    Camera camera = new Camera(); 

    @Override 
    protected void applyTransformation(float interpolatedTime, Transformation t) { 
     // TODO Auto-generated method stub 
     super.applyTransformation(interpolatedTime, t); 

     Matrix matrix = t.getMatrix(); 
     camera.save(); 

     camera.getMatrix(matrix); 
     matrix.setTranslate(0, -((height/2) * interpolatedTime)));//here is the change 

     matrix.preTranslate(0, -height); 
     camera.restore(); 

     this.setAnimationListener(this); 
    } 

,并与你的对话

使用它们3210
LinearLayout ll = (LinearLayout) findViewById(R.id.parentLayout);//parent layout in the xml, which serves as the background in the custom dialog 

ll.startAnimation(new DropDownToMiddleAnimation());//use with launching of the dialog 

ll.startAnimation(new MiddleToTopAnimation());//use while dismissing the dialog/finishing the dialog activity 
+1

我喜欢XML动画而不是硬编码动画。 – 2012-07-10 13:54:13

+0

好吧,那就是您的设计调用。我使用硬编码的动画类,因为它们可以在其他地方重新使用。 – 2012-07-10 14:00:50

+0

XML动画也可以在任何地方重复使用.. – 2012-07-10 14:02:14

2

slide_down.xml

<?xml version="1.0" encoding="utf-8"?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="@android:integer/config_longAnimTime" 
    android:fromYDelta="-50%p" 
    android:toYDelta="0%p" /> 

slide_up.xml

<?xml version="1.0" encoding="utf-8"?> 
<translate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="@android:integer/config_longAnimTime" 
    android:fromYDelta="0%p" 
    android:toYDelta="-100%p" />