main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pop" />
</LinearLayout>
自定义View
pop_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/button_popupe"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:text="pop,pop" >
</Button>
</LinearLayout>
anim文件下自定义动画
fipper_in.xml
<?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>
fipper_out.xml
<?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="100%p" />
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
ActivityMain
package com.android;
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.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.ViewFlipper;
public class ActivityMain extends Activity {
//定义PopupWindow
private PopupWindow popup;
//定义PopupWindow动画
private ViewFlipper mViewFlipper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 创建动画
mViewFlipper = new ViewFlipper(this);
mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.flipper_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.flipper_out));
//获取自定义的pop_view,因为pop_view.xml没有设置setContentView方法,所以不能用findViewById方法获取对象
LayoutInflater mLayoutInflater = LayoutInflater.from(getApplicationContext());
View pop_view = mLayoutInflater.inflate(R.layout.pop_view, null);
mViewFlipper.addView(pop_view);
mViewFlipper.setFlipInterval(3000);
// 创建Popup
popup = new PopupWindow(mViewFlipper, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
popup.setBackgroundDrawable(getResources().getDrawable(
R.drawable.pop_bg));
popup.setFocusable(true);
popup.update();
//开启pop的按钮
Button btn = (Button)this.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if (popup != null) {
if (popup.isShowing()) {
popup.dismiss();
} else {
//相对与某个控件的位置
popup.showAtLocation(v,Gravity.BOTTOM, 0, 0);
//播放动画
mViewFlipper.startFlipping();
}
}
}
});
}
}
点击pop按钮即可出现下面pop.pop弹窗。