欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中自定义PopupWindow实现弹出框并带有动画效果

程序员文章站 2024-03-04 21:06:00
使用popupwindow来实现弹出框,并且带有动画效果 首先自定义popupwindow public class lostpopupwindow exten...

使用popupwindow来实现弹出框,并且带有动画效果

首先自定义popupwindow

public class lostpopupwindow extends popupwindow {
public lost lost;
public void onlost(lost lost){
this.lost = lost;
}
private view conentview;
public view getconentview() {
return conentview;
}
public lostpopupwindow(final activity context) {
layoutinflater inflater = (layoutinflater) context
.getsystemservice(context.layout_inflater_service);
conentview = inflater.inflate(r.layout.lost_pop_menu, null);
int h = context.getwindowmanager().getdefaultdisplay().getheight();
int w = context.getwindowmanager().getdefaultdisplay().getwidth();
// 设置selectpicpopupwindow的view
this.setcontentview(conentview);
// 设置selectpicpopupwindow弹出窗体的宽
this.setwidth(w / 2 + 50);
// 设置selectpicpopupwindow弹出窗体的高
this.setheight(layoutparams.wrap_content);
// 设置selectpicpopupwindow弹出窗体可点击
this.setfocusable(true);
this.setoutsidetouchable(true);
// 刷新状态
this.update();
// 实例化一个colordrawable颜色为半透明
colordrawable dw = new colordrawable(0000000000);
// 点back键和其他地方使其消失,设置了这个才能触发ondismisslistener ,设置其他控件变化等操作
this.setbackgrounddrawable(dw);
// mpopupwindow.setanimationstyle(android.r.style.animation_dialog);
// 设置selectpicpopupwindow弹出窗体动画效果
this.setanimationstyle(r.style.animationpreview);
linearlayout send = (linearlayout) conentview
.findviewbyid(r.id.send);
linearlayout mysend = (linearlayout) conentview
.findviewbyid(r.id.my_send);
linearlayout all = (linearlayout) conentview.findviewbyid(r.id.all);
send.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view arg0) {
lostpopupwindow.this.dismiss();
lost.onlost(2);
}
});
mysend.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
lostpopupwindow.this.dismiss();
lost.onlost(1);
}
});
all.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view v) {
lostpopupwindow.this.dismiss();
lost.onlost(0);
}
});
}
/**
* 显示popupwindow
*
* @param parent
*/
public void showpopupwindow(view parent) {
if (!this.isshowing()) {
// 以下拉方式显示popupwindow
this.showasdropdown(parent, parent.getlayoutparams().width / 2, 18);
} else {
this.dismiss();
}
}
}

r.layout.lost_pop_menu文件

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignparentright="true"
android:background="@drawable/black_menu_pop_bg"
android:orientation="vertical"
android:paddingleft="@dimen/activity_horizontal_margin">
<linearlayout
android:id="@+id/send"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:scaletype="fitxy">
<imageview
android:id="@+id/img5"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/icon_lost_add" />
<textview
android:id="@+id/item_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginleft="10dp"
android:text="发布信息"
android:textcolor="#e5e5e6"
android:textsize="18sp" />
</linearlayout>
<view
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#616467" />
<linearlayout
android:id="@+id/my_send"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<imageview
android:id="@+id/img6"
android:layout_width="20dp"
android:layout_height="20dp"
android:scaletype="fitxy"
android:src="@drawable/icon_lost_my" />
<textview
android:id="@+id/item_content1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginleft="10dp"
android:text="我发布的信息"
android:textcolor="#e5e5e6"
android:textsize="18sp" />
</linearlayout>
<view
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#616467" />
<linearlayout
android:id="@+id/all"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<imageview
android:id="@+id/img7"
android:layout_width="20dp"
android:layout_height="20dp"
android:scaletype="fitxy"
android:src="@drawable/icon_all" />
<textview
android:id="@+id/item_content2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginleft="10dp"
android:text="全部信息"
android:textcolor="#e5e5e6"
android:textsize="18sp" />
</linearlayout>
</linearlayout>
动画r.style.animationpreview
<style name="animationpreview">
<item name="android:windowenteranimation">@anim/fade_in</item>
<item name="android:windowexitanimation">@anim/fade_out</item>
</style>
@anim/fade_in
<?xml version="1.0" encoding="utf-8"?>
<!-- 左上角扩大-->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromxscale="0.001"
android:toxscale="1.0"
android:fromyscale="0.001"
android:toyscale="1.0"
android:pivotx="100%"
android:pivoty="10%"
android:duration="200" />
@anim/fade_out
<!-- 左上角缩小 -->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:fromxscale="1.0"
android:toxscale="0.001"
android:fromyscale="1.0"
android:toyscale="0.001"
android:pivotx="100%"
android:pivoty="10%"
android:duration="200" />

接下来就是使用了

lostpopupwindow popwindow = new lostpopupwindow(zixundetailactivity.this);
((imageview)(popwindow.getconentview().findviewbyid(r.id.img5))).setimageresource(r.drawable.ckplico);
((imageview)(popwindow.getconentview().findviewbyid(r.id.img6))).setimageresource(r.drawable.fbplico);
((imageview)(popwindow.getconentview().findviewbyid(r.id.img7))).setimageresource(r.drawable.zfplico);
((textview)(popwindow.getconentview().findviewbyid(r.id.item_content))).settext("查看评论");
((textview)(popwindow.getconentview().findviewbyid(r.id.item_content1))).settext("发表评论");
((textview)(popwindow.getconentview().findviewbyid(r.id.item_content2))).settext("转发文章");
popwindow.showpopupwindow(linmain);
popwindow.onlost(new lost() {
@override
public void onlost(int index) {
switch (index){
case 0: //转发文章
break;
case 1: //发表评论
linefooter.setvisibility(view.visible);
break;
case 2://查看评论
bundle bundle=new bundle();
bundle.putstring("id",mid);
startactivity(pinglunactivity.class, "热门评论", bundle);
break;
}
}
});

效果图

Android中自定义PopupWindow实现弹出框并带有动画效果

以上所述是小编给大家介绍的android中自定义popupwindow实现弹出框并带有动画效果,希望对大家有所帮助