对话转换效果

时间:2012-07-10 12:57:38

标签: android

我目前正在处理对话框的过渡效果。请参考下图: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。这是activityTheme.Dialog

中应用了AndroidManifest.xml

2 个答案:

答案 0 :(得分: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" />

答案 1 :(得分:1)

如果您将对话框创建为活动,则可以遵循此方法

您可以创建动画类:

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