【问题标题】:Best way to implement custom view with animation at the center of the screen?在屏幕中心使用动画实现自定义视图的最佳方法?
【发布时间】:2013-04-12 11:15:02
【问题描述】:

我的目标是在我的应用程序中实现自定义 toast,如下所示:

MyCustomToastClass.makeText(context,View,anyDurationInMS);

我想设置“Toast”的重力、布局、持续时间(不仅仅是 Length.SHORT/LONG)

到目前为止我尝试过的事情:

  • 带有 windowManager 对象的类 问题是它必须在以下位置实现android系统动画:

    params.windowAnimations

当我尝试如下实现自定义动画时:

windowManager.addView(mLayout);
Animation AlphaAnimation = new ...

它没有实现我的动画。

  • 带有 rootView 元素的类以将 Toast 的布局添加到其中: 这样,我只有在根视图是 FrameLayout 类型时才成功实现所有内容(对于其他布局,我无法将“Toast”重力设置为中心)。

如果有人实现了此功能,我将不胜感激,或者如果我在其中一种方式中遗漏了什么,请指导我。

谢谢

【问题讨论】:

    标签: android android-animation toast android-framelayout android-windowmanager


    【解决方案1】:

    很抱歉误解了您的问题

    如果你想创建一个像 toast 这样的弹出窗口并且持续时间由你决定,也许你可以尝试创建一个自定义视图,其中包含你想要烤的内容,然后你可以将你的主要布局放在框架中布局然后每次用户触发您的自定义吐司时,您都可以将自定义视图添加到您的框架布局中,以便将其放置在您的主布局前面,并且对于淡入淡出动画,您可以使用它

    Animation fadeIn = new AlphaAnimation(0, 1);
    fadeIn.setInterpolator(new DecelerateInterpolator()); //add this
    fadeIn.setDuration(1000);
    
    Animation fadeOut = new AlphaAnimation(1, 0);
    fadeOut.setInterpolator(new AccelerateInterpolator()); //and this
    fadeOut.setStartOffset(1000);
    fadeOut.setDuration(1000);
    

    或者如果你想使用 XML

    淡入

    <?xml version="1.0" encoding="utf-8"?> 
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="0.0" 
        android:toAlpha= 1.0" 
        android:duration="1000"    
        android:repeatCount="infinite" 
        android:repeatMode="reverse"
        />
    

    淡出

    <?xml version="1.0" encoding="utf-8"?> 
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="1.0" 
        android:toAlpha="0.0" 
        android:duration="1000"    
        android:repeatCount="infinite" 
        android:repeatMode="reverse"
        />
    

    暂时你可以使用

    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        // FADE OUT THE POP UP/TOAST HERE
      }
    }, /*SET THE TIME HERE*/);
    

    我希望这个答案对你来说足够清楚 如果您对我的回答仍有疑问,请随时在评论中提问 :)

    【讨论】:

    • 您好 NAYOSO,您只能显示 Lenght.SHORT/LONG 的 toast。我需要按持续时间显示它。谢谢
    • 您好 NAYOSO,非常感谢您的帮助。如您所见,这是我以第二种方式尝试过的。似乎没有其他解决方案。我将等待 1-2 天并接受这个答案。谢谢
    • @UdiOshi 不客气,是的,我认为这是目前唯一的方法,对于我当前具有可以与用户交互的自定义​​弹出窗口的项目,我也使用这种方法:)为了向后兼容哈哈
    • 如何添加Fragment并将其定位在中间,它将包含Layout,它可以工作吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2021-06-06
    相关资源
    最近更新 更多