2012-03-10 2 views
4

setAnimationStyle()を使用してポップアップウィンドウのアニメーションスタイルを試してみたいと思いますが、ドキュメントを理解するのに苦労しています。setAnimationStyle()の明示的なアニメーション、私の選択は何ですか?

"ポップアップが表示され消えるときに使用するアニメーションスタイル。デフォルトのアニメーションは-1、アニメーションなしの場合は0、明示的なアニメーションの場合はリソース識別子です。"

これは例を示していないか、利用可能なリソースの選択肢を教えてください。私は私の目的のための最高のアニメーションが右からスライドしていると思う...それはオプションとして存在するのだろうか?リストから選択することができますか、どういうわけか自分自身を作成する必要がありますか?

EDIT:

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 /アニメーション/の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)と入れ替えてみました。アニメーションはありません。なぜ私は考えていない。

答えて

14

これが私の最後に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); 

コーディングのおかげ&ハッピー!

+3

ニース! 'mce_bogus =" 1 "'ものは何ですか? – Timmmm

+0

なぜここで固定値ですか? 'android:toYDelta =" 854 "'あなたは説明したり、いくつかの代替を提供することができますか?画面が小さくなるとどうなるでしょうか? –

0

このオプションは、R.styleのマニュアルに記載されています。しかし、多くはありません。それ以上のものが必要な場合は、自分で作成する必要があります。基本ガイドhereを参照してください。

カスタマイズされたアニメーションの例は、simple-quickactions google code siteにあります。

あなたのコメントによると、私はあなたのスタイルのIDをR.style.Animation_testと指定するべきだと思います。

+0

元の質問に編集を参照してください。 – Mick

+0

すばらしい返答をいただきありがとうございます。しかし、「あなたのスタイルのIDをR.style.Animation_testとして指定する方法」についてはわかりません。確かにあなたはアンドロイドを置くつもりはありません:id = "@ + id/R.style.Animation_test"は私のXMLにありますか? – Mick

+0

私は 'pw.setAnimationStyle(R.style.Animation_test)'を意味しますが、私は間違っている可能性があります – MByD