android - 对话过渡效果

标签 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。它是一个 activity,在 AndroidManifest.xml

中应用了一个 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);
    }

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

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

关于android - 对话过渡效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11413817/

相关文章:

android - 在某些 subview 前面绘制

java - 匿名上传文件对象到 Imgur API (JSON) 给出身份验证错误 401

android - 如何更改 Media Projector API 系统 Activity (MediaProjectionPermissionActivity) 在 android 中显示的警报对话框文本?

android - 如何使整个显示可滚动

android - Jsoup - Android - 从表单数据/输入中解析信息

android - 从 Android 将图像保存在 SQLite 数据库中

android - 无法为锁定文件创建父目录

Android Jetpack 不是 AOSP 平台 API 的一部分

java - android - 倒计时后打开 Activity

android - 成员变量前缀 ("m") 在 Android studio 1.1 中的 getters 和 setters