setAnimationStyle()的显式动画,我的选择是什么?

时间:2012-03-10 17:56:15

标签: java android eclipse

我想使用setAnimationStyle()为弹出窗口尝试不同的动画样式,但我很难理解文档。

developer.android.com,说:“当弹出窗口显示和消失时使用的动画样式。默认动画设置为-1,无动画设置为0,显式动画设置为资源标识符。”

它没有提供任何示例,也没有告诉我可用的资源选择。我怀疑为我的目的最好的动画将从右边滑入...这是作为一个选项存在吗?我可以从列表中选择这些东西,还是以某种方式创建自己的东西?

编辑:我制作弹出窗口的当前代码是这样的(简化):

public void completed_dialog()
{
  runOnUiThread(new Runnable()
  {
    public void run()
    {

      View layout = inflater.inflate(R.layout.endofgame, null, false);

      Button b1 = (Button) layout.findViewById(R.id.pu_menu);
      Button b2 = (Button) layout.findViewById(R.id.pu_repeat);
      Button b3 = (Button) layout.findViewById(R.id.pu_next);

      b1.setBackgroundResource(R.drawable.custom_menu_but);
      b2.setBackgroundResource(R.drawable.custom_repeat_but);
      b3.setBackgroundResource(R.drawable.custom_next_but);

      b1.setOnClickListener(menu_button_click_listener);
      b2.setOnClickListener(repeat_button_click_listener);
      b3.setOnClickListener(next_button_click_listener);

      final PopupWindow pw = new PopupWindow(layout, canvas_width, 
                                                       canvas_width /2,  true);

      pw.setAnimationStyle(android.R.style.Animation_Dialog);
      pw.showAtLocation(game_frame_layout, Gravity.CENTER, 0, 0);
    }
  }
}

我现在将以下内容放在文件res / anim / test.xml中:

<?xml version="1.0" encoding="utf-8"?>

    <set xmlns:android="http://schemas.android.com/apk/res/android">
            <scale
                    android:fromXScale="0.3" android:toXScale="1.0"
                    android:fromYScale="0.3" android:toYScale="1.0"
                    android:pivotX="0%" android:pivotY="0%"
                    android:duration="@android:integer/config_shortAnimTime"
            />
            <alpha
                    android:interpolator="@android:anim/decelerate_interpolator"
                    android:fromAlpha="0.0" android:toAlpha="1.0"
                    android:duration="@android:integer/config_shortAnimTime"
            />
    </set>

原始代码用pw.setAnimationStyle(android.R.style.Animation_Dialog);工作正常,就像标准动画中出现的对话框一样 - 但是如果我用pw.setAnimationStyle(R.anim.test)交换该行;没有动画。我不明白为什么。

2 个答案:

答案 0 :(得分:15)

这是我setAnimationStyle()的最佳实例:

<强> RES /动画/ in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator">
    <translate 
        android:fromYDelta="854" 
        android:toYDelta="0"
        android:duration="1000"/>
</set>

<强> RES /动画/ out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate android:interpolator="@android:anim/decelerate_interpolator"
        android:fromYDelta="0"
        android:toYDelta="854" 
        android:duration="10000"
    />
</set>

<强>布局/ main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout"
    >
    <Button
        android:id="@+id/btn_show"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="show"
        />
</RelativeLayout >

<强>布局/ popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:background="#cccccc" 
    > 
    <Button 
        android:id="@+id/btn_dismiss"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="dismiss"/>
</LinearLayout>

<强>值/ styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="PopupAnimation" parent="android:Animation" mce_bogus="1">      
         <item name="android:windowEnterAnimation">@anim/in</item>  
        <item name="android:windowExitAnimation">@anim/out</item>  
    </style>  
</resources>

<强>值/ strings.xml中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">Popupwindow</string>
</resources>

MainActivity.java

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    boolean flag =false;
    PopupWindow popupWindow;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
    public void init() {
        Button btn_show = (Button) findViewById(R.id.btn_show);
        LayoutInflater inflater = LayoutInflater.from(this);
        View layout = inflater.inflate(R.layout.popup, null);
        popupWindow =new PopupWindow(layout, LayoutParams.FILL_PARENT,
                LayoutParams.FILL_PARENT);
        Button btn_dismiss = (Button) layout.findViewById(R.id.btn_dismiss);
        btn_dismiss.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                openMenu();
            }
        });
        btn_show.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                openMenu();
            }
        });
    }

    public void btn_showOnClicked() {
        openMenu();
    }

    public void btn_dismissOnClicked() {
        openMenu();
    }

    public void openMenu() {
        if (!flag) {
            popupWindow.setAnimationStyle(R.style.PopupAnimation);
            popupWindow.showAtLocation(findViewById(R.id.btn_show), Gravity.NO_GRAVITY, 0, 0);
            popupWindow.setFocusable(true);
            popupWindow.update();
            flag =true;
        } else {
            popupWindow.dismiss();
            popupWindow.setFocusable(false);
            flag =false;
        }
    }
}

并在您的代码中将您的动画文件添加到样式为:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="test" parent="android:Animation" mce_bogus="1">      
         <item name="android:windowEnterAnimation">@anim/test</item>  
    </style>  
</resources>

并在代码中:

pw.setAnimationStyle(android.R.style.test);

谢谢&amp;快乐编码!!!

答案 1 :(得分:0)

您可以在R.style的文档中看到这些选项。但是并不是很多。如果你想要更多,你需要自己创建它。请参阅基本指南here

您可以在simple-quickactions google code site找到自定义动画的更多示例。

根据您的评论,我认为您应该将样式的ID指定为R.style.Animation_test