本文实例为大家分享了PopWindow实现带动画向下弹出效果的具体代码,供大家参考,具体内容如下
首先建一个popwin的实体类
package dmpte.mytest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
public class PopWin extends PopupWindow {
private Context mContext;
private View view;
public PopWin(final Context mContext, View. Listener items , int flag) {
this.mContext = mContext;
this.view = LayoutInflater.from(mContext).inflate(R.layout.view_popwin, null);
// 设置外部可点击
this.setOutsideTouchable(true);
/* 设置弹出窗口特征 */
// 设置视图
this.setContentView(this.view);
// 设置弹出窗体的宽和高
this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);//高
this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);//宽
// 设置弹出窗体可点击
this.setFocusable(true);
// 设置弹出窗体显示时的动画,从底部向上弹出
this.setAnimationStyle(R.style.take_photo_anim);
// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
this.view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int height = view.findViewById(R.id.pop_layout).getHeight();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//Y表示手指点击的位置,屏幕顶端为0,往下一次递增。height是popwin的高度。y > height就表示手指点在popwin的外面,然后关闭popwin
if (y > height) {
dismiss();
}
}
return true;
}
});
}
}
然后是这个类的布局 view_popwin.
<? version=\"1.0\" encoding=\"utf-8\"?> <LinearLayout ns:android=\"http://schemas.android.com/apk/res/android\" android:id=\"@+id/pop_layout\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" android:background=\"@null\" android:orientation=\"vertical\"> <LinearLayout android:layout_width=\"match_parent\" android:layout_height=\"170dp\" android:background=\"#ffff\" android:orientation=\"vertical\"> <TextView android:id=\"@+id/tv_jingtai\" android:layout_width=\"match_parent\" android:layout_height=\"match_parent\" android:layout_gravity=\"center\" android:layout_marginTop=\"2dp\" android:gravity=\"center\" android:text=\"移动静态\" android:textColor=\"#f123\" /> </LinearLayout> </LinearLayout>
接下来是这个类里涉及的动画 popwin_anim,在res/values/styles下
<style name=\"popwin_anim\" parent=\"android:Animation\">
<item name=\"android:windowEnterAnimation\">@anim/pop_enter_anim</item>
<item name=\"android:windowExitAnimation\">@anim/pop_exit_anim</item>
</style>
然后是进场动画 pop_enter_anim和出场动画 pop_exit_anim,在res下建一个文件夹anim,分别新建上面两个
pop_enter_anim.
<? version=\"1.0\" encoding=\"utf-8\"?> <set ns:android=\"http://schemas.android.com/apk/res/android\" android:shareInterpolator=\"false\"> <!-- 平移动画 --> <translate android:duration=\"500\" android:fromYDelta=\"-100%p\" android:toYDelta=\"0\" /> </set>
pop_exit_anim.
<? version=\"1.0\" encoding=\"utf-8\"?> <set ns:android=\"http://schemas.android.com/apk/res/android\" android:shareInterpolator=\"false\"> <!-- 平移动画 --> <translate android:duration=\"1000\" android:fromYDelta=\"0\" android:toYDelta=\"-100%p\" /> </set>
最后是使用
//让背景变暗
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.7f;
getWindow().setAttributes(lp);
//弹出窗体
PopWin popWin_ = new PopWin(this, null, 0);
popWin_.showAsDropDown(findViewById(R.id.relativeLayout));
//监听popwin是否关闭,关闭的话让背景恢复
popWin_.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
继续阅读与本文标签相同的文章
下一篇 :
编程小技巧:多态原理
-
什么是高防?有什么用?
2026-05-19栏目: 教程
-
阿里云如何添加CDN解析过程 新手必看教程
2026-05-19栏目: 教程
-
好程序员web前端学习路线分享css3中的渐进增强和降级
2026-05-19栏目: 教程
-
什么是苹果iOS超级签名源码系统?
2026-05-19栏目: 教程
-
好程序员web前端学习路线分享模拟JavaScript中面向对象技术
2026-05-19栏目: 教程
