Android PopupWindow全屏詳細(xì)介紹及實(shí)例代碼
Android PopupWindow全屏
很多應(yīng)用中經(jīng)??梢钥吹綇棾鲞@種PopupWindow的效果,做了一個(gè)小demo分享一下。demo的思路是通過遍歷文件,找到圖片以及圖片文件夾放置在PopupWindow上面。點(diǎn)擊按鈕可以彈出這個(gè)PopupWindow,這里為PopupWindow設(shè)置了動(dòng)畫。
PopupWindow全屏代碼提要
受限需要自定義Popupwindow,這里不看Popupwindow里面要展示的內(nèi)容,主要是設(shè)置Popupwindow的高度。
public class PopupwindowList extends PopupWindow { private int mWidth; private int mHeight; private View mContentView; private List<FileBean> mFileBeans; private ListView mListView; public PopupwindowList(Context context,List<FileBean> mFileBeans) { super(context); this.mFileBeans=mFileBeans; //計(jì)算寬度和高度 calWidthAndHeight(context); setWidth(mWidth); setHeight(mHeight); mContentView= LayoutInflater.from(context).inflate(R.layout.popupwidow,null); //設(shè)置布局與相關(guān)屬性 setContentView(mContentView); setFocusable(true); setTouchable(true); setTouchable(true); setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //點(diǎn)擊PopupWindow以外區(qū)域時(shí)PopupWindow消失 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { dismiss(); } return false; } }); } /** * 設(shè)置PopupWindow的大小 * @param context */ private void calWidthAndHeight(Context context) { WindowManager wm= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics metrics= new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(metrics); mWidth=metrics.widthPixels; //設(shè)置高度為全屏高度的70% mHeight= (int) (metrics.heightPixels*0.7); } }
點(diǎn)擊按鈕彈出PopupWindow
mButtonShowPopup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //點(diǎn)擊時(shí)彈出PopupWindow,屏幕變暗 popupwindowList.setAnimationStyle(R.style.ListphotoSelect); popupwindowList.showAsDropDown(mButtonShowPopup, 0, 0); lightoff(); } });
private void lightoff() { WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha=0.3f; getWindow().setAttributes(lp); }
一、FileBean類保存信息
FileBean如上圖PopupWindow所示,需要保存文件的路徑,文件夾的名稱,文件夾中文件的數(shù)量,文件夾中第一張圖片的路徑?;救繛閟et、get方法
/** * this class is used to record file information like the name of the file 、the path of the file…… * */ public class FileBean { private String mFileName; private String mFilePath; private String mFistImgPath; private int mPhotoCount; public String getmFileName() { return mFileName; } public void setmFileName(String mFileName) { this.mFileName = mFileName; } public String getmFilePath() { return mFilePath; } public void setmFilePath(String mFilePath) { this.mFilePath = mFilePath; int index=this.mFilePath.lastIndexOf(File.separator); mFileName=this.mFilePath.substring(index); } public String getmFistImgPath() { return mFistImgPath; } public void setmFistImgPath(String mFistImgPath) { this.mFistImgPath = mFistImgPath; } public int getmPhotoCount() { return mPhotoCount; } public void setmPhotoCount(int mPhotoCount) { this.mPhotoCount = mPhotoCount; } }
二、PopupWidow界面設(shè)置
自定義PopupWindow,
public class PopupwindowList extends PopupWindow { private int mWidth; private int mHeight; private View mContentView; private List<FileBean> mFileBeans; private ListView mListView; //觀察者模式 public interface OnSeletedListener{ void onselected(FileBean bean); } private OnSeletedListener listener; public void setOnSelecterListener(OnSeletedListener listener){ this.listener=listener; } public PopupwindowList(Context context,List<FileBean> mFileBeans) { super(context); this.mFileBeans=mFileBeans; calWidthAndHeight(context); setWidth(mWidth); setHeight(mHeight); mContentView= LayoutInflater.from(context).inflate(R.layout.popupwidow,null); setContentView(mContentView); setFocusable(true); setTouchable(true); setTouchable(true); setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { //點(diǎn)擊PopupWindow以外區(qū)域時(shí)PopupWindow消失 if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { dismiss(); } return false; } }); initListView(context); initEvent(); } private void initEvent() { } //初始化PopupWindow的listview private void initListView(Context context) { MyListViewAdapter adapter=new MyListViewAdapter(context,0,mFileBeans); mListView= (ListView) mContentView.findViewById(R.id.listview); mListView.setAdapter(adapter); } /** * 設(shè)置PopupWindow的大小 * @param context */ private void calWidthAndHeight(Context context) { WindowManager wm= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics metrics= new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(metrics); mWidth=metrics.widthPixels; mHeight= (int) (metrics.heightPixels*0.7); } }
三、點(diǎn)擊按鈕彈出PopupWindow
public class PopupWindowTest extends AppCompatActivity { private Button mButtonShowPopup; private Set<String> mFilePath; private List<FileBean> mFileBeans; PopupwindowList popupwindowList; private Handler mHandler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); initPopupWindow(); } }; private void initPopupWindow() { popupwindowList=new PopupwindowList(this,mFileBeans); popupwindowList.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { lighton(); } }); } //PopupWindow消失時(shí),使屏幕恢復(fù)正常 private void lighton() { WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha=1.0f; getWindow().setAttributes(lp); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.popupwindowtestlayout); mButtonShowPopup= (Button) findViewById(R.id.button_showpopup); mFileBeans=new ArrayList<>(); initData(); initEvent(); } private void initEvent() { mButtonShowPopup.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //點(diǎn)擊時(shí)彈出PopupWindow,屏幕變暗 popupwindowList.setAnimationStyle(R.style.ListphotoSelect); popupwindowList.showAsDropDown(mButtonShowPopup, 0, 0); lightoff(); } }); } private void lightoff() { WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha=0.3f; getWindow().setAttributes(lp); } //開啟線程初始化數(shù)據(jù),遍歷文件找到所有圖片文件,及其文件夾與路徑進(jìn)行保存。 private void initData() { if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ ToastUtils.showToast(this,"當(dāng)前sdcard不用使用"); } new Thread(){ @Override public void run() { super.run(); Uri imgsUri= MediaStore.Images.Media.EXTERNAL_CONTENT_URI; ContentResolver contentResolver=getContentResolver(); Cursor cursor=contentResolver.query(imgsUri,null,MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?",new String[]{"image/jpeg","image/png"},MediaStore.Images.Media.DATA); mFilePath=new HashSet<>(); while (cursor.moveToNext()){ String path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); File parentfile=new File(path).getParentFile(); if(parentfile==null) continue; String filePath=parentfile.getAbsolutePath(); FileBean fileBean=null; if(mFilePath.contains(filePath)){ continue; }else { mFilePath.add(filePath); fileBean=new FileBean(); fileBean.setmFilePath(filePath); fileBean.setmFistImgPath(path); } if(parentfile.list()==null) continue; int count=parentfile.list(new FilenameFilter() { @Override public boolean accept(File dir, String filename) { if(filename.endsWith(".jpg")||filename.endsWith(".png")||filename.endsWith(".jpeg")){ return true; } return false; } }).length; fileBean.setmPhotoCount(count); mFileBeans.add(fileBean); } cursor.close(); //將mFilePath置空,發(fā)送消息,初始化PopupWindow。 mFilePath=null; mHandler.sendEmptyMessage(0x110); } }.start(); } }
四、PopupWindow動(dòng)畫設(shè)置。
(1)編寫彈出與消失動(dòng)畫
①彈出動(dòng)畫
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="100%" android:toYDelta="0" android:duration="1000"></translate> </set>
②消失動(dòng)畫
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="0" android:fromYDelta="0" android:toYDelta="100%" android:duration="1000"></translate> </set>
(2)設(shè)置style與調(diào)用style
①設(shè)置style
在style中進(jìn)行添加
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light"> <!-- Customize your theme here. --> </style> <style name="ListphotoSelect"> <item name="android:windowEnterAnimation">@anim/animshow</item> <item name="android:windowExitAnimation">@anim/animdismiss</item> </style> </resources>
②調(diào)用style
為PopupWindow設(shè)置動(dòng)畫style
popupwindowList.setAnimationStyle(R.style.ListphotoSelect);
備注:布局很簡單不再展示。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
用Android?studio實(shí)現(xiàn)簡易計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了用Android?studio實(shí)現(xiàn)簡易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android ViewGroup事件分發(fā)和處理源碼分析
這篇文章主要為大家介紹了Android ViewGroup事件分發(fā)和處理源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02android開機(jī)自啟動(dòng)APP及使用adb命令測試方法
今天小編就為大家分享一篇android開機(jī)自啟動(dòng)APP及使用adb命令測試方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08Android中Gallery和ImageSwitcher的使用實(shí)例
今天小編就為大家分享一篇關(guān)于Android中Gallery和ImageSwitcher的使用實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03Android Studio設(shè)置繪制布局時(shí)的視圖
這篇文章介紹了Android Studio設(shè)置繪制布局時(shí)視圖的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11Flutter Android應(yīng)用啟動(dòng)白屏的解決方案
任何一個(gè)app基本都會(huì)設(shè)計(jì)一個(gè)啟動(dòng)頁,今天我們就來看看怎么在flutter項(xiàng)目中設(shè)置啟動(dòng)頁,這篇文章主要給大家介紹了關(guān)于Flutter Android應(yīng)用啟動(dòng)白屏解決的相關(guān)資料,需要的朋友可以參考下2021-11-11Android實(shí)現(xiàn)瘋狂連連看游戲之狀態(tài)數(shù)據(jù)模型(三)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)瘋狂連連看游戲之狀態(tài)數(shù)據(jù)模型,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03