Popupwindow的定制
public class
MyPopupwindow
extends
PopupWindow{
private Context mContext ;
private View view ;
private Button btn_cancel , btn_sure ;
public MyPopupwindow(Context mContext, View.OnClickListener itemsOnClick) {
this . view = LayoutInflater.from(mContext).inflate(R.layout. popupwindow_layout , null );
btn_cancel = (Button) view .findViewById(R.id. btn_cancel );
btn_sure = (Button) view .findViewById(R.id. btn_sure );
// 取消按钮
btn_cancel .setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
// 销毁弹出框
dismiss();
}
});
btn_sure .setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
// 销毁弹出框
dismiss();
}
});
// 设置按钮监听
// 设置外部可点击
this .setOutsideTouchable( true );
// mMenuView 添加 OnTouchListener 监听判断获取触屏位置如果在选择框外面则销毁弹出框
this . view .setOnTouchListener( new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = view .findViewById(R.id. pop_layout ).getTop();
int y = ( int ) event.getY();
if (event.getAction() == MotionEvent. ACTION_UP ) {
if (y < height) {
dismiss();
}
}
return true ;
}
});
/* 设置弹出窗口特征 */
// 设置视图
this .setContentView( this . view );
// 设置弹出窗体的宽和高
this .setHeight(RelativeLayout.LayoutParams. WRAP_CONTENT );
this .setWidth(RelativeLayout.LayoutParams. MATCH_PARENT );
// 设置弹出窗体可点击
this .setFocusable( true );
// 实例化一个 ColorDrawable 颜色为半透明
ColorDrawable dw = new ColorDrawable( 0x00000000 );
// 设置弹出窗体的背景
this .setBackgroundDrawable(dw);
// 设置弹出窗体显示时的动画,从底部向上弹出
this .setAnimationStyle(R.style. take_photo_anim );
}
}
private Context mContext ;
private View view ;
private Button btn_cancel , btn_sure ;
public MyPopupwindow(Context mContext, View.OnClickListener itemsOnClick) {
this . view = LayoutInflater.from(mContext).inflate(R.layout. popupwindow_layout , null );
btn_cancel = (Button) view .findViewById(R.id. btn_cancel );
btn_sure = (Button) view .findViewById(R.id. btn_sure );
// 取消按钮
btn_cancel .setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
// 销毁弹出框
dismiss();
}
});
btn_sure .setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
// 销毁弹出框
dismiss();
}
});
// 设置按钮监听
// 设置外部可点击
this .setOutsideTouchable( true );
// mMenuView 添加 OnTouchListener 监听判断获取触屏位置如果在选择框外面则销毁弹出框
this . view .setOnTouchListener( new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = view .findViewById(R.id. pop_layout ).getTop();
int y = ( int ) event.getY();
if (event.getAction() == MotionEvent. ACTION_UP ) {
if (y < height) {
dismiss();
}
}
return true ;
}
});
/* 设置弹出窗口特征 */
// 设置视图
this .setContentView( this . view );
// 设置弹出窗体的宽和高
this .setHeight(RelativeLayout.LayoutParams. WRAP_CONTENT );
this .setWidth(RelativeLayout.LayoutParams. MATCH_PARENT );
// 设置弹出窗体可点击
this .setFocusable( true );
// 实例化一个 ColorDrawable 颜色为半透明
ColorDrawable dw = new ColorDrawable( 0x00000000 );
// 设置弹出窗体的背景
this .setBackgroundDrawable(dw);
// 设置弹出窗体显示时的动画,从底部向上弹出
this .setAnimationStyle(R.style. take_photo_anim );
}
}
进入动画
<?xml version=
"1.0"
encoding=
"utf-8"
?>
< set xmlns:android= "http://schemas.android.com/apk/res/android" >
< translate
android:duration= "200"
android:fromYDelta= "100%p"
android:toYDelta= "0" />
< alpha
android:duration= "200"
android:fromAlpha= "0.0"
android:toAlpha= "1.0" />
</ set >
< set xmlns:android= "http://schemas.android.com/apk/res/android" >
< translate
android:duration= "200"
android:fromYDelta= "100%p"
android:toYDelta= "0" />
< alpha
android:duration= "200"
android:fromAlpha= "0.0"
android:toAlpha= "1.0" />
</ set >
退出动画
<?xml version=
"1.0"
encoding=
"utf-8"
?>
< set xmlns:android= "http://schemas.android.com/apk/res/android" >
< translate
android:duration= "200"
android:fromYDelta= "0"
android:toYDelta= "50%p" />
< alpha
android:duration= "200"
android:fromAlpha= "1.0"
android:toAlpha= "0.0" />
</ set >
< set xmlns:android= "http://schemas.android.com/apk/res/android" >
< translate
android:duration= "200"
android:fromYDelta= "0"
android:toYDelta= "50%p" />
< alpha
android:duration= "200"
android:fromAlpha= "1.0"
android:toAlpha= "0.0" />
</ set >
Style
文件
xml
<
style
name=
"take_photo_anim"
parent=
"android:Animation"
>
< item name= "android:windowEnterAnimation" >@anim/pop_enter_anim</ item >
< item name= "android:windowExitAnimation" >@anim/pop_exit_anim</ item >
< item name= "android:windowEnterAnimation" >@anim/pop_enter_anim</ item >
< item name= "android:windowExitAnimation" >@anim/pop_exit_anim</ item >
</style>