简体   繁体   English

在屏幕中央实现带有动画的自定义视图的最佳方法?

[英]Best way to implement custom view with animation at the center of the screen?

My GOAL is to implement custom toast in my application as follows: 我的目标是在我的应用程序中实现自定义吐司,如下所示:

MyCustomToastClass.makeText(context,View,anyDurationInMS);

I want to set the "Toast" gravity, Layout, duration(not just Length.SHORT/LONG) 我想设置“吐司”重力,布局,持续时间(而不仅仅是Length.SHORT / LONG)

Things i've tried so far: 到目前为止我尝试过的事情:

  • Class with windowManager object problem was it must implement android system animation in: 带有windowManager对象的类问题是必须在以下位置实现android系统动画:

    params.windowAnimations params.windowAnimations

and when I tried to implement my custom animation as followed: 当我尝试实现自定义动画时,如下所示:

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

it didn't implement my animation. 它没有实现我的动画。

  • Class with the rootView element to add the Toast's layout to it: In this way I succeeded to implement everything only when the root view was FrameLayout type (With other layout I couldn't set the "Toast" gravity to center). 带有rootView元素的类,以向其添加Toast的布局:这样,仅当根视图为FrameLayout类型时,我才成功实现了所有功能(对于其他布局,我无法将“ Toast”重力设置为居中)。

I would appreciate if someone has implemented this feature, Or to guide me if i'm missing something in one of my ways. 如果有人实现了此功能,我将不胜感激;或者,如果我以一种方式缺少某些东西,可以为我提供指导。

thanks 谢谢

Sorry for misunderstand your question 抱歉误会您的问题

if you want to create an pop up like toast and the duration is up to you maybe you can try to create a custom view that will contain the content you want to be toasted after that you can place your main layout in the frame layout then everytime the user trigger your custom toast you can add the custom view to your frame layout so it'll be positioned in front of your main layout and for fade in fade out animation you can use this 如果您想创建一个像吐司这样的弹出窗口,并且持续时间由您决定,那么您可以尝试创建一个自定义视图,该视图将包含您要烘烤的内容,然后可以将主布局放置在框架布局中,用户触发您的自定义祝酒,您可以将自定义视图添加到框架布局中,以便将其放置在主布局的前面,并且对于淡入淡出动画,可以使用此视图

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);

or if you want to use XML 或者如果您想使用XML

FadeIn 淡入

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

FadeOut 淡出

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

and for the time you can use 而且你可以使用的时间

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

I hope this answer is clear enough for you and if you still have some question about my answer don't hesitate to ask in the comment :) 我希望这个答案对您来说足够清楚,如果您对我的答案还有疑问,请随时在评论中提问:)

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

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