参考地址: 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. 通用窗口类 Inventory Pro 2.1.2 Demo1(上)

    插件功能 按照Demo1的实现,使用插件来实现一个装备窗口是很easy的,虽然效果还很原始但是也点到为止了,本篇涉及的功能用加粗标出,具体的功能如下: 1.实现了两个窗口,通过点击键盘I来,打开或者关 ...

  2. Swift游戏实战-跑酷熊猫 14 熊猫打滚

    这节内容我们来实现熊猫打滚.思路是这样的,当熊猫起跳时记录他的Y坐标,落到平台上的时候再记录它的Y坐标.两个坐标之间的差要是大于一定数值就判断它从高处落下要进行打滚缓冲.至此跑酷熊猫已经像一个游戏的样 ...

  3. PostgreSQL Replication之第十二章 与Postgres-XC一起工作(7)

    12.7 处理故障转移和删除节点 在本节中,我们将看看故障切换如何处理.我们还将看看如何使用安全可靠的方法添加节点到Postgres-XC设置以及如何从Postgres-XC设置删除节点. 12.7. ...

  4. Codeforces Round #325 (Div. 1) D. Lizard Era: Beginning

    折半搜索,先搜索一半的数字,记录第一个人的值,第二个人.第三个人和第一个人的差值,开个map哈希存一下,然后另一半搜完直接根据差值查找前一半的答案. 代码 #include<cstdio> ...

  5. php session session_set_save_handler 接管所有的session管理工作

    一个已知管用的方法是,使用session_set_save_handler,接管所有的session管理工作,一般是把session信息存储到数 据库,这样可以通过SQL语句来删除所有过期的sessi ...

  6. paper 7:支持向量机系列四:Outliers —— 介绍支持向量机使用松弛变量处理 outliers 方法。

    在最开始讨论支持向量机的时候,我们就假定,数据是线性可分的,亦即我们可以找到一个可行的超平面将数据完全分开.后来为了处理非线性数据,使用 Kernel 方法对原来的线性 SVM 进行了推广,使得非线性 ...

  7. 由linux下的多进程编程引发的关于进程间隔离的思考

    源代码放到了三个文件中: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include & ...

  8. [fedora21]给fedora21安装fcitx输入法

    如果已经安装了ibus,需要卸载ibus:   sudo yum remove ibus; 安装fcitx: sudo yum install fcitx fcitx-pinyin fcitx-con ...

  9. Date() 及其 如何验证用户输入的日期是合法的

    1.var someDate = new Date(Date.parse("May 25, 2004"));   <=>  var someDate = new Dat ...

  10. vsftpd基本配置(原)

    前提:iptables关闭或放行,selinux关闭或放行. 目标:创建虚拟ftpuser到指定的www.ftpuser.com目录,且只能在本目录下. 创建帐号 useradd -d /var/ww ...