废话不多说,上代码
首先我先写了个Pop要显示弹出来的窗口的布局,见下(布局相当简单):
文件名:show_pop.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom" >
<RelativeLayout
android:id="@+id/out"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#aabbcc" >
<Button
android:id="@+id/dismiss_pop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#ffffff"
android:text="dismiss_pop" />
</RelativeLayout>
</RelativeLayout>
另外,主页面布局我就写了下个按钮用来点击弹出pop的按钮,也给大家看下:
文件名:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="showPop" />
</RelativeLayout>
接下来,我在res文件夹中创建一个anim的文件夹
就是这样,里面两个文本里的代码如下:
1.文件名:pop_exit.xml中
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromYDelta="0"
android:toYDelta="100%" />
</set>
2.文件名:pop_in.xml中
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromYDelta="100%"
android:toYDelta="0" />
<!-- <rotate
android:fromDegrees="180"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="0"
android:duration="1000"/> -->
</set>
再接下来就是需要在style里面设置一下pop要进入界面和退出界面时个的一个过程了,只需要在styles里面加上以下一段:
文件名:styles
<style name="popUpwindow" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/pop_in</item>
<item name="android:windowExitAnimation">@anim/pop_exit</item>
</style>
OK,差不多了,下面就是写一个popupwindow的一个类,用来调用显示pop了
文件名:GetPopupwindow
package com.gudd.popupwindowanimation;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class GetPopupwindow extends PopupWindow {
View view;
public GetPopupwindow(Context context,OnClickListener onClickListener){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.show_pop, null);
Button dismiss_pop = (Button) view.findViewById(R.id.dismiss_pop);
dismiss_pop.setOnClickListener(onClickListener);
this.setContentView(view);
this.setWidth(LayoutParams.MATCH_PARENT);
this.setHeight(LayoutParams.WRAP_CONTENT);
this.setFocusable(true);
this.setAnimationStyle(R.style.popUpwindow);
ColorDrawable cd = new ColorDrawable(0xb0000000);
this.setBackgroundDrawable(cd);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
}
这个呢其实就相当于一个公共的类了,哪里都可以用了。
我们这里要让它显示出来,我在MainActivity.class界面中调用了这个类,实现点击进入退出pop了
package com.gudd.popupwindowanimation;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button click;
GetPopupwindow popupwindow;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
click = (Button) findViewById(R.id.click);
click.setOnClickListener(onClickListener);
}
private OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.click:
popupwindow = new GetPopupwindow(context, onClickListener);
popupwindow.showAtLocation(click, Gravity.BOTTOM,0, 0);
break;
case R.id.dismiss_pop:
popupwindow.dismiss();
break;
default:
break;
}
}
};
}
好了,以上就是我所有的代码了,
如果你需要有更多的如淡入淡出、折扇拉伸等效果可以在上面pop_in.xml和pop_exit.xml两个xml文件中设置相应的动画属性,祝君成功!
欢迎给我提意见哦...
我写的demo的下载地址:点击打开链接