Android中自定義PopupWindow實(shí)現(xiàn)彈出框并帶有動(dòng)畫效果
使用PopupWindow來實(shí)現(xiàn)彈出框,并且?guī)в袆?dòng)畫效果
首先自定義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(); // 設(shè)置SelectPicPopupWindow的View this.setContentView(conentView); // 設(shè)置SelectPicPopupWindow彈出窗體的寬 this.setWidth(w / 2 + 50); // 設(shè)置SelectPicPopupWindow彈出窗體的高 this.setHeight(LayoutParams.WRAP_CONTENT); // 設(shè)置SelectPicPopupWindow彈出窗體可點(diǎn)擊 this.setFocusable(true); this.setOutsideTouchable(true); // 刷新狀態(tài) this.update(); // 實(shí)例化一個(gè)ColorDrawable顏色為半透明 ColorDrawable dw = new ColorDrawable(0000000000); // 點(diǎn)back鍵和其他地方使其消失,設(shè)置了這個(gè)才能觸發(fā)OnDismisslistener ,設(shè)置其他控件變化等操作 this.setBackgroundDrawable(dw); // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog); // 設(shè)置SelectPicPopupWindow彈出窗體動(dòng)畫效果 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="發(fā)布信息" 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="我發(fā)布的信息" 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> 動(dòng)畫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"?> <!-- 左上角擴(kuò)大--> <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("查看評(píng)論"); ((TextView)(popWindow.getConentView().findViewById(R.id.item_content1))).setText("發(fā)表評(píng)論"); ((TextView)(popWindow.getConentView().findViewById(R.id.item_content2))).setText("轉(zhuǎn)發(fā)文章"); popWindow.showPopupWindow(linMain); popWindow.onLost(new Lost() { @Override public void onLost(int index) { switch (index){ case 0: //轉(zhuǎn)發(fā)文章 break; case 1: //發(fā)表評(píng)論 lineFooter.setVisibility(View.VISIBLE); break; case 2://查看評(píng)論 Bundle bundle=new Bundle(); bundle.putString("id",mID); startActivity(PingLunActivity.class, "熱門評(píng)論", bundle); break; } } });
效果圖
以上所述是小編給大家介紹的Android中自定義PopupWindow實(shí)現(xiàn)彈出框并帶有動(dòng)畫效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Android進(jìn)階CoordinatorLayout協(xié)調(diào)者布局實(shí)現(xiàn)吸頂效果
這篇文章主要為大家介紹了Android進(jìn)階CoordinatorLayout協(xié)調(diào)者布局實(shí)現(xiàn)吸頂效果,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01基于Android XML解析與保存的實(shí)現(xiàn)
本篇文章小編為大家介紹,基于Android XML解析與保存的實(shí)現(xiàn)。需要的朋友參考下2013-04-04解決Android webview設(shè)置cookie和cookie丟失的問題
這篇文章主要介紹了解決Android webview設(shè)置cookie和cookie丟失的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03實(shí)例詳解Android Webview攔截ajax請(qǐng)求
本篇內(nèi)容主要給大家講解了Android Webview攔截ajax請(qǐng)求的詳細(xì)講解,需要的朋友一起來學(xué)習(xí)一下。2017-11-11Android仿京東首頁(yè)秒殺倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了Android仿京東首頁(yè)秒殺倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android編程實(shí)現(xiàn)獲取多媒體庫(kù)視頻、音頻、圖片的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取多媒體庫(kù)視頻、音頻、圖片的方法,涉及Android針對(duì)多媒體視頻、音頻及相關(guān)專輯圖片、縮略圖等獲取操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01