繁体   English   中英

如何从外部将对话框动画化为屏幕

[英]How to animate dialog into screen from outside

我似乎无法将屏幕下方的对话框动画化为屏幕的75%。 我能够使用旧的翻译动画器来完成此操作,但是它不允许视图上的onclick事件,因此我实现了ObjectAnimator。

问题是,无论我做什么,对话框都会从屏幕的中心动起来,并以某种方式消失。

LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.toast_goal_added, null);

dialog = new Dialog(Activity.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(dialoglayout);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

ObjectAnimator animator = ObjectAnimator.ofFloat(dialoglayout, "y", -200f);
animator.setDuration(1000);
animator.start();
dialog.show();

这将使对话框向上移动,但是如您所见,它隐藏在某些东西下面。

在此处输入图片说明

布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rl_customtoast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:clickable="true"
        android:padding="10dp"
        android:background="@drawable/custom_toast_bg" >

    <ImageView
        android:id="@+id/iv_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/icon_add_on" />
       <LinearLayout
          android:id="@+id/ll_row"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical"
          android:layout_centerVertical="true"
          android:layout_toRightOf="@+id/iv_toast"
          android:layout_marginLeft="5dp"
          android:padding="2dp"  >
        <TextView
            android:id="@+id/toast_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Goal added"
            android:textColor="#FDFDFD"
            android:textSize="16sp"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

         <TextView
            android:id="@+id/toast_text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click to see your result"
            android:textColor="#E7E7E7"
            android:textSize="15sp"/>
     </LinearLayout>

   </RelativeLayout>

所以问题

  1. 对话框开始从屏幕中心移动

  2. 对话框在特定点消失

我通过放弃对话框并使用常规布局来使其工作。

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
float displayHeight = size.y;
AnimatorSet animSet = new AnimatorSet();
ObjectAnimator mover = ObjectAnimator.ofFloat(include_toast, "y", displayHeight, 15*displayHeight/20);
mover.setDuration(200);
ObjectAnimator mover2 = ObjectAnimator.ofFloat(include_toast, "y", 15*displayHeight/20, displayHeight);
mover2.setDuration(200);
mover2.setStartDelay(2000);
animSet.play(mover2).after(mover);
animSet.start();

其中include_toast是一个布局,该布局在主布局加载时不可见,仅在动画期间可见。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM