关于PopupWindow的使用例子

本文介绍如何利用PopupWindow在Android中创建淡入淡出的动画效果,包括初始化界面、设置动画样式、配置PopupWindow显示与隐藏的过程,并提供完整代码示例。

先上图

用的豌豆荚截图,本来是个动画的,每个过程都有几张,大概就是要实现这个效果,初始状态只有一个Button,当点击show的时候,另外一个页面从底部慢慢升起来,直到覆盖到上一个页面,注意这里不是启用另一个Activity,是用的PopupWindow,当点击dismiss的时候,又慢慢消失。。。这种效果看上去不错,PopupWindow上面可以添加想要添加的控件,比如ListView(注意,如果添加ListView的话,当它弹出来之后,按back键不起作用,它获取不了监听,其余的非ListView控件可以,这里添加了个Button ),下面贴出代码

这是主类MainActivity.java

package com.test.popupwindow;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;

publicclass MainActivity extends Activity {
/** Called when the activity is first created. */

boolean flag =false;
PopupWindow popupWindow;

@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

init();
}

publicvoid 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
publicvoid onClick(View v) {
// TODO Auto-generated method stub
openMenu();
}
});
btn_show.setOnClickListener(
new OnClickListener() {

@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
openMenu();
}
});
}

publicvoid btn_showOnClicked() {
openMenu();
}

publicvoid btn_dismissOnClicked() {
openMenu();
}

publicvoid 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;
}
}
}

  布局文件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>

  工程结构

注意看在res文件夹下面新建了一个anim文件夹,里面要实现的动画页面,比如从哪个坐标移动到哪个坐标之类的,当然还可以定义其它的,这里只实现了Y坐标的移动

在anim文件夹下面建两个文件,一个是in.xml,另外一个是out.xml,意思一看就明白

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>

  它表示Y的坐标从854(因为我的手机分辨率是850x480,摩托MB525)移动到0,时间为1秒

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>

  这个不解释了。。。


有人给我反应说缺少style文件,我看了一下,确实是,当时发贴的时候漏掉了,现在补上

在values文件夹下面建一个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>

现在应该OK了

 

转载于:https://www.cnblogs.com/and_he/archive/2011/08/12/2136107.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值