20150604_Andriod 窗体PopupWindow动画


参考地址: http://www.open-open.com/lib/view/open1378720752084.html

               http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0702/1627.html


/////////////////////////////////////////////////////////////////////
package com.example.test1;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

////////////////////////////
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
//import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
//import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;

import android.widget.Toast;
////////////////////////////

 

public class PopupWindow2 extends ActionBarActivity {
   
    ////////////////////////////
     private Button button;
     private Button button1;
     private Button button2;
     private Button button3;
     private Button button4;
     private View popupWindowView;
     private PopupWindow popupWindow;
     private LayoutInflater inflater;
    ////////////////////////////
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_popup_window2);
  
        ////////////////////////////
  button=(Button) findViewById(R.id.button_m);    
  button.setOnClickListener(new ButtonOnClickListener());
        ////////////////////////////
 }
 
 
 
 ////////////////////////////
  private class ButtonOnClickListener implements OnClickListener{
         public void onClick(View v) {
             inflater=(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
             popupWindowView=inflater.inflate(R.layout.activity_popup_window_sub, null);
          
             popupWindow=new PopupWindow(popupWindowView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,true);
             //必须要有这句否则弹出popupWindow后监听不到Back键
             popupWindow.setBackgroundDrawable(new BitmapDrawable());
             popupWindow.showAtLocation(findViewById(R.id.button_m), Gravity.NO_GRAVITY, 0, 0);
             //让popupWindow获得焦点
             popupWindow.setFocusable(true);
             //设置动画
             popupWindow.setAnimationStyle(R.style.popupWindowAnimation);
             popupWindow.update();
                          
             //popupWindow中按钮的处理
             button1=(Button) popupWindowView.findViewById(R.id.button1);
             button2=(Button) popupWindowView.findViewById(R.id.button2);
             button3=(Button) popupWindowView.findViewById(R.id.button3);
             button4=(Button) popupWindowView.findViewById(R.id.button4);
             button1.setOnClickListener(new ButtonsOnClickListener());
             button2.setOnClickListener(new ButtonsOnClickListener());
             button3.setOnClickListener(new ButtonsOnClickListener());
             button4.setOnClickListener(new ButtonsOnClickListener());
              
         }
          
     }
      
     private class ButtonsOnClickListener implements OnClickListener {
         public void onClick(View v) {
             switch (v.getId()) {
             case R.id.button1:
                 //System.out.println("点击了按钮1");
              Toast.makeText(getApplicationContext(), "提示:点击了按钮1,点击窗口外部关闭窗口!",
                 Toast.LENGTH_SHORT).show();
                 break;
             case R.id.button2:
                  System.out.println("点击了按钮2");
                 break;
             case R.id.button3:
                  System.out.println("点击了按钮3");
                 break;
             case R.id.button4:
                  //System.out.println("点击了按钮4");
              popupWindow.dismiss(); // 关闭窗口
                 break;
  
             default:
                 break;
             }
  
         }
  
     }
      
     //监听Back事件
     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode==KeyEvent.KEYCODE_BACK) {
             if (popupWindow!=null&&popupWindow.isShowing()) {
                 popupWindow.dismiss();      
             } else {
                 //MainActivity.this.finish();
              PopupWindow2.this.finish();
             }
         }
         return super.onKeyDown(keyCode, event);
     }

 ////////////////////////////
 

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.popup_window2, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // Handle action bar item clicks here. The action bar will
  // automatically handle clicks on the Home/Up button, so long
  // as you specify a parent activity in AndroidManifest.xml.
  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }
}


/////////////////////////////////////////////////////////////////////

style.xml如下:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
   <style name="AppTheme" parent="android:Theme.Light" />
   <style name="popupWindowAnimation" mce_bogus="1" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/enter</item>
        <item name="android:windowExitAnimation">@anim/exit</item>
    </style>
</resources>

enter.xml动画如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- android:fromYDelta:动画开始的点离当前View X坐标上的差值 -->
<!-- 利用100%p表示该动画在当前View的最下方 -->
<set 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="1500"
        android:fromYDelta="100%p"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toYDelta="0" />
 
</set>

exit.xml动画如下:
<?xml version="1.0" encoding="utf-8"?>
<!-- Alpha=1.0表示不透明,Alpha=0.0表示透明 -->
<set 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator" >
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
     />
</set>


/////////////////////////////////////////////////////////////////////

PopupWindow的动画

很多时候我们把PopupWindow用作自定义的菜单,需要一个从底部向上弹出的效果,这就需要为PopupWindow添加动画。

设置动画的方法:

   1.public void setAnimationStyle(int animationStyle)

   2.在res/value/styles.xml添加一个sytle

   3.在工程res下新建anim文件夹,在anim文件夹先新建两个xml文件 :enter.xml, exit.xml


/////////////////////////////////////////////////////////////////////

 

你可能感兴趣的:(PopupWindow)