对话过渡效果 [英] Dialog Transition Effects

查看:324
本文介绍了对话过渡效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为我的对话框中的过渡效果。请参考下图:

I am currently working on the transition effects for my dialog. Please refer to the image below:

入口动画我的对话应该是顶部中间。而出口动画应中间到顶部。我使用下面的XML动画,但不幸的是,他们没有工作。

The entrance animation for my dialog should be top to middle. While the exit animation should be middle to top. I am using the following XML animations, but unfortunately, they're not working.

slide_down.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

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

编辑:这不是一个通常的对话框。这是一个活动应用与 Theme.Dialog 的Andr​​oidManifest.xml

This is not a usual Dialog. It is an activity applied with a Theme.Dialog in the AndroidManifest.xml

推荐答案

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

if you are creating the dialog as an activity then you can follow this approach

您可以创建动画类:

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

这篇关于对话过渡效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆