参考地址: 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

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

20150604_Andriod 窗体PopupWindow动画的更多相关文章

  1. 20150604_Andriod 窗体PopupWindow

    package com.example.test1; import android.support.v7.app.ActionBarActivity;import android.os.Bundle; ...

  2. popUpWindow 动画无法超出窗体的解决方案

    popupWindow 做动画时,当要求有一个放大动画时,动画无法超出窗体,给人的感觉是只有内容在放大,窗体不动. 这是由于窗口大小固定的原因,解决方案是加大popUpwindow的 大小. 一个比较 ...

  3. 自定义PopupWindow动画效果

    public class RollActivity extends Activity { private View view; private Button btn; private PopupWin ...

  4. Android 自定义PopupWindow动画效果

    public class RollActivity extends Activity { private View view; private Button btn; private PopupWin ...

  5. Android初级教程以动画的形式弹出窗体

    这一篇集合动画知识和弹出窗体知识,综合起来以动画的形式弹出窗体. 动画的知识前几篇已经做过详细的介绍,可翻阅前面写的有关动画博文.先简单介绍一下弹出窗体效果的方法: 首先,需要窗体的实例:PopupW ...

  6. 【C#】窗体动画效果

    通过调用API可以实现C#窗体的动画效果,主要调用user32.dll的行数AnimateWindow 1.函数申明 [System.Runtime.InteropServices.DllImport ...

  7. PopupWindow设置动画效果

    创建popupwindow的方法 Button menu; private void showPopupWindow() { //设置contentView float density = Densi ...

  8. 【补间动画示例】Tweened Animation

    代码中定义动画示例 public class MainActivity extends ListActivity </integer> 常用的Activity转场动画中的补间动画 publ ...

  9. android:PopupWindow的使用场景和注意事项

    1.PopupWindow的特点 借用Google官方的说法: "A popup window that can be used to display an arbitrary view. ...

随机推荐

  1. Swift游戏实战-跑酷熊猫(一) 简介 (含源代码)

    优酷观看地址:http://v.youku.com/v_show/id_XNzM2Nzc2MTIw.html 通过这个小游戏,我们能够接触到物理系统(SKPhysicsBody,physicsWorl ...

  2. 当As3遇见Swift(三)

    类 As3 Swift中似乎没有包,包路径的概念.因而显得简洁的多. package { public class ShuaiGe { } } Swift类 class ShuaiGe{ } 类的构造 ...

  3. postgres-toolkit (A Victorinox for PostgreSQL DBA )

    postgres-toolkit A collection of scripts and utilities to manage PostgreSQL servers. Allows DBA to p ...

  4. SQL top查询

    select *from emp;

  5. eclipse 改变字体大小

  6. 两条直线(蓝桥杯)二分枚举+RMQ

    算法提高 两条直线   时间限制:1.0s   内存限制:256.0MB        问题描述 给定平面上n个点. 求两条直线,这两条直线互相垂直,而且它们与x轴的夹角为45度,并且n个点中离这两条 ...

  7. yii框架中保存第三方登录信息

    (第三方登录) 创建应用,域名,详情请看:http://www.cnblogs.com/xujn/p/5287157.html 效果图:

  8. (转)supertable像excel那样固定table的表头和第一列

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"/> <title> ...

  9. [javascript] ajaxfileupload.js 跨域上传文件

    原文地址: http://www.ueffort.com/jqueryajaxfileupload-js-duo-wen-jian-shang-chuan-chuan-zhi-kua-yu/ 跨域 这 ...

  10. [待解决] sudo unable to resolve host

    怪哉怪哉, 大debian突然就出现了这个问题 , 问题的现象是只要使用 sudo 执行命令就会出现 sudo unable to resolve host </etc/hostname中的内容 ...