对话框过渡效果

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中应用了 Theme.Dialog 的一个 activity


你是如何应用这些动画的? - Joru
我认为如果您打开“对话活动”,那么这个转换就很容易实现。 - Mohsin Naeem
你在用什么?DialogFragment吗?请粘贴应用转换和提交事务的代码。 - Ovidiu Latcu
我在启动/关闭一个活动时使用这种方法:overridePendingTransition(R.anim.slide_down, R.anim.slide_up); - Jayson Tamayo
2个回答

2

如果您正在将对话框作为活动创建,则可以按照以下方法进行:

您可以创建动画类:

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

并将它们与您的对话框一起使用

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

好的,这是你的设计决定。我使用硬编码动画类,因为它们可以在其他地方重复使用。 - Vinay W
谢谢。我想我必须使用硬编码动画而不是XML。 :) - Jayson Tamayo
你能帮我解决这个问题吗?http://stackoverflow.com/questions/11444018/frame-by-frame-animation-optimization - Jayson Tamayo
我该如何将LinearLayout链接到对话框(Dialog)中呢?我不太明白 :/ - bofredo

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接