Android中自訂PopupWindow實現彈出框並帶有動畫效果_Android

來源:互聯網
上載者:User

使用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的Viewthis.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() {@Overridepublic void onClick(View arg0) {LostPopupWindow.this.dismiss();lost.onLost(2);}});mySend.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {LostPopupWindow.this.dismiss();lost.onLost(1);}});all.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {LostPopupWindow.this.dismiss();lost.onLost(0);}});}/*** 顯示popupWindow** @param parent*/public void showPopupWindow(View parent) {if (!this.isShowing()) {// 以下拉方式顯示popupwindowthis.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"><LinearLayoutandroid:id="@+id/send"android:layout_width="match_parent"android:layout_height="40dp"android:gravity="center_vertical"android:orientation="horizontal"android:scaleType="fitXY"><ImageViewandroid:id="@+id/img5"android:layout_width="20dp"android:layout_height="20dp"android:src="@drawable/icon_lost_add" /><TextViewandroid: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><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:background="#616467" /><LinearLayoutandroid:id="@+id/my_send"android:layout_width="match_parent"android:layout_height="40dp"android:gravity="center_vertical"android:orientation="horizontal"><ImageViewandroid:id="@+id/img6"android:layout_width="20dp"android:layout_height="20dp"android:scaleType="fitXY"android:src="@drawable/icon_lost_my" /><TextViewandroid: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><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:background="#616467" /><LinearLayoutandroid:id="@+id/all"android:layout_width="match_parent"android:layout_height="40dp"android:gravity="center_vertical"android:orientation="horizontal"><ImageViewandroid:id="@+id/img7"android:layout_width="20dp"android:layout_height="20dp"android:scaleType="fitXY"android:src="@drawable/icon_all" /><TextViewandroid: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() {@Overridepublic 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實現彈出框並帶有動畫效果,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.