zoukankan      html  css  js  c++  java
  • Android的PopWindow动画实现

    转载博客:http://www.open-open.com/lib/view/open1423626956186.html

    1.实现步骤

    1.主布局activity_main.xml
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <Button
            android:id="@+id/open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="打开泡泡窗口" />
    
    </RelativeLayout>

    2.popupwindow.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <GridView
            android:id="@+id/gv"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="#00bfff"
            android:numColumns="4" >
        </GridView>
    
    </LinearLayout>

    3.MainActivity

    public class MainActivity extends Activity implements OnClickListener {
    
        private Button open;
        private View parent;
        private View popView;
        private PopupWindow popupWindow;
        private GridView gv;
    
        private String[] names = { "", "", "", "", "", "", "", "" };
        private int[] images = { R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher,
                R.drawable.ic_launcher, R.drawable.ic_launcher };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            parent = findViewById(R.id.main);
            open = (Button) findViewById(R.id.open);
            open.setOnClickListener(this);
            initPopupWindow();
        }
    
        /**
         * 初始化popupWindow
         */
        private void initPopupWindow() {
            popView = getLayoutInflater().inflate(R.layout.popupwindow, null);
            popupWindow = new PopupWindow(popView,
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            popupWindow.setFocusable(true);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setOutsideTouchable(true);
            gv = (GridView) popView.findViewById(R.id.gv);
            gv.setAdapter(MyAdapter());
            
    
        }
    
        /**
         * 为GridView填充数据
         * 
         * @return
         */
        private ListAdapter MyAdapter() {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < names.length; i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("names", names[i]);
                map.put("images", images[i]);
                list.add(map);
            }
            SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
                    R.layout.pop_item, new String[] { "names", "images" },
                    new int[] { R.id.tv, R.id.img });
            return simpleAdapter;
        }
    
        @Override
        public void onClick(View v) {
            //为popWindow添加动画效果
            popupWindow.setAnimationStyle(R.style.popWindow_animation);
            // 点击弹出泡泡窗口
            popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        }
    }

    4.为了实现动画进出效果,定义两个布局文件

    popupwindow_enter.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <translate
            android:duration="500"
            android:fromYDelta="100%p"
            android:toYDelta="0" />
    
        <alpha
            android:duration="500"
            android:fromAlpha="0.5"
            android:toAlpha="1.0" />
    
    </set>

    popupwindow_exit.xml

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <alpha
            android:duration="500"
            android:fromAlpha="1.0"
            android:toAlpha="0.5" />
    
        <translate
            android:fromYDelta="0"
            android:toYDelta="100%p"
            android:duration="500" />
    
    </set>

    5.style.xml

    <style name="popWindow_animation">
            <item name="android:windowEnterAnimation">@anim/popupwindow_enter</item>
            <item name="android:windowExitAnimation">@anim/popupwindow_exit</item>
    </style>

     PopuWindow和软键盘共存时的设置

    一、键盘不消失,popuwindow在下层布局大小不变

      

    popupWindow=new PopupWindow(popuview,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    // 需要设置一下此参数,点击外边可消失  
    popupWindow.setBackgroundDrawable(new BitmapDrawable());  
    //设置点击窗口外边窗口消失  
    popupWindow.setOutsideTouchable(true);  
    //设置弹出窗体需要软键盘,
    popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
    //再设置模式,和Activity的一样,覆盖。
    popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    二、键盘不消失,popuWindow在下层,布局上移

      

    popupWindow=new PopupWindow(popuview,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    // 需要设置一下此参数,点击外边可消失  
    popupWindow.setBackgroundDrawable(new BitmapDrawable());  
    //设置点击窗口外边窗口消失  
    popupWindow.setOutsideTouchable(true);  
     
    //设置弹出窗体需要软键盘,
    popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
    //再设置模式,和Activity的一样,覆盖,调整大小。
    popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

    三、键盘消失

      

    popupWindow=new PopupWindow(popuview,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    // 需要设置一下此参数,点击外边可消失  
    popupWindow.setBackgroundDrawable(new BitmapDrawable());  
    //设置点击窗口外边窗口消失  
    popupWindow.setOutsideTouchable(true);  
    popupWindow.setFocusable(true);
  • 相关阅读:
    install git on ubuntu
    deploy uwsgi with niginx on ubuntu
    ubuntu下部署solr
    solr relevent project
    20100722
    [Programming Visual C++]Ex05cCScrollView Revisited
    iter_test
    交友类节目
    install scrapy on windows
    20100703
  • 原文地址:https://www.cnblogs.com/AndroidJotting/p/5318693.html
Copyright © 2011-2022 走看看