如何使动画在弹出窗口的android窗口、动画、在弹出、android

2023-09-06 01:47:18 作者:parhoia妄想症

我在我的应用程序,它的出现哈瓦一个弹出窗口时,有些按钮点击 我希望在动画设置淡入淡出窗口, 我把RES /动画文件夹中的XML文件,并设置了弹出式窗口动画的风格,但动画不能正常工作? 这里是我的codeS:

myanim.xml ...

 < XML版本=1.0编码=UTF-8&GT?;
<设置的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <阿尔法机器人:fromAlpha =0.0
        机器人:toAlpha =1.0
        机器人:插=@机器人:动画/ accelerate_interpolator
        机器人:时间=4000
        安卓的repeatCount =1/>
< /集>
 

===============================================

创建弹出式窗口

 私人PopupWindow showOptions(上下文MCON){
    尝试{
        LayoutInflater充气=(LayoutInflater)mcon.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        查看布局= inflater.inflate(R.layout.options_layout,NULL);
        layout.setAnimation(AnimationUtils.loadAnimation(这一点,R.anim.myanim));
        PopupWindow optionspu =新PopupWindow(布局,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

        optionspu.setFocusable(真正的);
        optionspu.showAtLocation(布局,Gravity.TOP,0,0);
        optionspu.update(0,0,LayoutParams.WRAP_CONTENT,(int)的(开平/ 5));
        optionspu.setAnimationStyle(R.anim.myanim);
        返回optionspu;
    }
    赶上(例外五){e.printStackTrace();
    返回null;}
}
 

=============================================== == 的onClick方法......(optionsPopup的类型PopupWindow的全局变量)

  @覆盖
公共无效的onClick(视图v){
               开关(v.getId()){
        案例R.id.options:
                optionsPopup = showOptions(本);
            打破;
}
 
chao ge的推荐内容

解决方案

我认为这个问题是你只提供一组动画风格。但实际上,一个 PopupWindow 需要两个动画。其中将使用它时,窗口显示和另外一个隐藏的窗口。

这是你应该怎么做,

1)创建两个不同的组动画。

说, popup_show.xml 和 popup_hide.xml 并把它添加到您的动画文件夹,你必须创建在资源文件夹。

2)现在里面的值文件夹中创建一个名为 styles.xml XML和添加这些动画像这样,

 <样式名称=动画>
    <项目名称=机器人:windowEnterAnimation> @动画/ popup_show< /项目>
    <项目名称=机器人:windowExitAnimation> @动画/ popup_hide< /项目>
< /风格>
 

3)现在设置这种风格到你的 PopupWindow 动画,

  popup.setAnimationStyle(R.style.Animation);
 

现在它会自动检测窗口进入和退出,并提供了所需的动画。

I hava a popup window in my application, its appears when some button clicked I want to set fade in animation to this window, I put the xml file in "res/anim" folder and set the animation style for the popup window but the animation does not work? here is my codes:

myanim.xml...

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha android:fromAlpha="0.0"
        android:toAlpha="1.0" 
        android:interpolator="@android:anim/accelerate_interpolator" 
        android:duration="4000"
        android:repeatCount="1"/>
</set>

===============================================

Create the popup window

private PopupWindow showOptions(Context mcon){
    try{ 
        LayoutInflater inflater = (LayoutInflater) mcon.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.options_layout,null);
        layout.setAnimation(AnimationUtils.loadAnimation(this, R.anim.myanim));
        PopupWindow optionspu = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        optionspu.setFocusable(true);
        optionspu.showAtLocation(layout, Gravity.TOP, 0, 0);
        optionspu.update(0, 0, LayoutParams.WRAP_CONTENT, (int)(hei/5));
        optionspu.setAnimationStyle(R.anim.myanim);
        return optionspu;
    }
    catch (Exception e){e.printStackTrace();
    return null;}
}

================================================= onClick method... (optionsPopup is global variable of type PopupWindow)

 @Override
public void onClick(View v) {
               switch (v.getId()) { 
        case R.id.options:
                optionsPopup=showOptions(this);
            break;  
}

解决方案

I think the problem is you have provided only one set of Animation style. But actually a PopupWindow requires two animations. One will be used by it when window is shown and other one to hide the Window.

This is how you should do it,

1)Create Two Different set of animations.

say, popup_show.xml and popup_hide.xml and add it to your anim folder which you have to create inside res folder.

2)Now inside values folder create a xml called styles.xml and add these animations to it like this,

<style name="Animation">
    <item name="android:windowEnterAnimation">@anim/popup_show</item>
    <item name="android:windowExitAnimation">@anim/popup_hide</item>
</style>

3)Now set this style to your PopupWindow animation,

 popup.setAnimationStyle(R.style.Animation);

Now it automatically detects Window Enter and Exit and provides with the required animation.