您的位置:首页 > 运维架构

底部弹出popWindow的动画

2015-06-30 20:13 267 查看
当我点击按钮的时候,主界面的下方会弹出popWindow.而且popwindow具有一定的动画效果.

主要的思路是:

设置其窗口的布局文件,出现的位置,弹出时的动画效果,还有控件的事件处理.

1 首先创建popWindow的布局文件:

[code]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_margin="5dp"
  android:orientation="vertical" >

  <!-- 这里的linearLayout加android:background=""这个属性要谨慎,如果加了后,popwindow是不能半透明了的 -->

  <Button
    android:id="@+id/first"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="5dp"
    android:background="@color/red"
    android:text="第一个按钮" />

  <Button
    android:id="@+id/second"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@color/red"
    android:text="第二个按钮" />

  <Button
    android:id="@+id/third"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="@color/red"
    android:text="第三个按钮" />

</LinearLayout>


2 创建popwindow的动画的xml文件:

1 隐藏时的动画效果:

[code]<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

   <translate
    android:duration="1000"
    android:fromYDelta="0"
    android:toYDelta="50%p" />

  <alpha
    android:duration="1000"
    android:fromAlpha="1.0"
    android:toAlpha="0.0" />
</set>


2 出现时的动画效果:

[code]<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
   <translate
    android:duration="1000"
    android:fromYDelta="100%p"
    android:toYDelta="0" />

  <alpha
    android:duration="1000"
    android:fromAlpha="0.0"
    android:toAlpha="1.0" />
</set>


3 创建样式,在style.xml文件中添加代码:

[code]   <!--  这个是加入的代码 -->
  <style name="mypopwindow_anim_style">
    <item name="android:windowEnterAnimation">@anim/popshow_anim</item>
 <!-- 指定显示的动画xml -->

    <item name="android:windowExitAnimation">@anim/pophidden_anim</item>
 <!-- 指定消失的动画xml -->
  </style>


3 在MainActivity中,处理按钮的点击事件:

[code]public void show(View view){
        Toast.makeText(this, "hhh",Toast.LENGTH_LONG).show();
        LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v=inflater.inflate(R.layout.popwindow,null);

        PopupWindow window=new PopupWindow(v, WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
        window.setFocusable(true);

        //设置背景
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        window.setBackgroundDrawable(dw);

        //设置动画
        window.setAnimationStyle(R.style.mypopwindow_anim_style);
        //设置展现的位置
        window.showAtLocation(this.findViewById(R.id.start), Gravity.BOTTOM,0,0);

        v.findViewById(R.id.first).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
              Toast.makeText(MainActivity.this,"第一个按钮被点击",Toast.LENGTH_LONG).show();        
            }
        });

        window.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                  Toast.makeText(MainActivity.this,"消失",Toast.LENGTH_LONG).show();      

            }
        });
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: