Android的PopWindow動畫實現底部滑出菜單

niki 9年前發布 | 96K 次閱讀 Android Android開發 移動開發

 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>
5.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>
6.style.xml
 <style name="popWindow_animation">
        <item name="android:windowEnterAnimation">@anim/popupwindow_enter</item>
        <item name="android:windowExitAnimation">@anim/popupwindow_exit</item>
    </style>


來自:http://blog.csdn.net/u013771273/article/details/43731175


 本文由用戶 niki 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!