gpt4 book ai didi

android - 在屏幕中央使用动画实现自定义 View 的最佳方式?

转载 作者:行者123 更新时间:2023-11-29 01:54:33 25 4
gpt4 key购买 nike

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

MyCustomToastClass.makeText(context,View,anyDurationInMS);

我想设置“ toast ”重力,布局,持续时间(不仅仅是长度。短/长)

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

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

    params.windowAnimations

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

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

它没有实现我的动画。

  • 使用 rootView 元素进行类化以将 Toast 的布局添加到其中:通过这种方式,只有当 Root View 是 FrameLayout 类型时,我才能成功实现所有内容(对于其他布局,我无法将“Toast”重力设置为中心)。

如果有人实现了这个功能,我将不胜感激,或者如果我在我的方法中遗漏了一些东西,我将不胜感激。

谢谢

最佳答案

抱歉误解了您的问题

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

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

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

关于android - 在屏幕中央使用动画实现自定义 View 的最佳方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15969936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com