popupWindow添加viewFlipper實現菜單動畫效果

 

activity代碼

package com.liudan.activity; 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.ViewFlipper;
public class PopupWindowDemo extends Activity{
 
 private ViewFlipper mViewFlipper;
  private PopupWindow popup = null;
    private Handler mHandler = new Handler(){ 
         
        public void handleMessage(Message msg) { 
            switch (msg.what) { 
            case 1: 
                showPopupWindow(); 
                break; 
            } 
        }; 
    }; 
     
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        initPopMenu(); 
        //create the timer   
        Timer timer = new Timer(); 
        timer.schedule(new initPopupWindow(), 100); 
       
    } 
     
    private class initPopupWindow extends TimerTask{ 
        @Override 
        public void run() { 
             
            Message message = new Message(); 
            message.what = 1; 
            mHandler.sendMessage(message); 
             
        }        
    } 
    
     
    public void showPopupWindow() { 
//        Context mContext = PopupWindowDemo.this; 
//        LayoutInflater mLayoutInflater = (LayoutInflater) mContext 
//                .getSystemService(LAYOUT_INFLATER_SERVICE); 
//        View music_popunwindwow = mLayoutInflater.inflate( 
//                R.layout.main, null); 
//        PopupWindow mPopupWindow = new PopupWindow(music_popunwindwow, 
//                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
       // mPopupWindow.showAtLocation(findViewById(R.id.button), Gravity.CENTER, 0, 0);
        popup.showAtLocation(findViewById(R.id.listview_catalog), Gravity.BOTTOM, -82, 0);
        mViewFlipper.startFlipping();
    } 
    private void initPopMenu() {

        mViewFlipper = new ViewFlipper(this);
        mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.menu_in));
        mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.menu_out));
        mViewFlipper.setFlipInterval(60000);

        String[] adapterItems = {"menu_image", "menu_content"};
        ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> map1 = new HashMap<String, Object>();
        HashMap<String, Object> map2 = new HashMap<String, Object>();
        HashMap<String, Object> map3 = new HashMap<String, Object>();
        HashMap<String, Object> map4 = new HashMap<String, Object>();
        HashMap<String, Object> map5 = new HashMap<String, Object>();
        HashMap<String, Object> map6 = new HashMap<String, Object>();
        HashMap<String, Object> map7 = new HashMap<String, Object>();
        HashMap<String, Object> map8 = new HashMap<String, Object>();
        HashMap<String, Object> map9 = new HashMap<String, Object>();

        map1.put(adapterItems[0], "111111111");
        map1.put(adapterItems[1], "aaaaaaaaaa");

        map2.put(adapterItems[0], "222222222222222");
        map2.put(adapterItems[1], "bbbbbbbbbbbbbbb");

        map3.put(adapterItems[0], "33333333333333");
        map3.put(adapterItems[1], "ccccccccccccccccc");

        map4.put(adapterItems[0], "44444444444444444444");
        map4.put(adapterItems[1], "dddddddddddddddddddddd");

        map5.put(adapterItems[0], "dddddddddddddddddddddd");
        map5.put(adapterItems[1], "dddddddddddddddddddddd");

        map6.put(adapterItems[0], "dddddddddddddddddddddd");
        map6.put(adapterItems[1], "dddddddddddddddddddddd");

        map7.put(adapterItems[0], "dddddddddddddddddddddd");
        map7.put(adapterItems[1], "dddddddddddddddddddddd");

        map8.put(adapterItems[0], "dddddddddddddddddddddd");
        map8.put(adapterItems[1], "dddddddddddddddddddddd");

        map9.put(adapterItems[0], "dddddddddddddddddddddd");
        map9.put(adapterItems[1], "dddddddddddddddddddddd");
        listItem.add(map1);
        listItem.add(map2);
        listItem.add(map3);
        listItem.add(map4);
        listItem.add(map5);
        listItem.add(map6);
        listItem.add(map7);
        listItem.add(map8);
        listItem.add(map9);
        SimpleAdapter adapter = new SimpleAdapter(this, listItem, R.layout.setting_menu,
                adapterItems, new int[]{
                        R.id.menu_image, R.id.menu_content});

        ListView listView = new ListView(this);
        listView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        listView.setAdapter(adapter);
        listView.setFocusable(false);
        listView.setCacheColorHint(Color.TRANSPARENT);
        listView.setScrollingCacheEnabled(false);
//        listView.setOnItemClickListener(new MenuOnItemClickListener());

        mViewFlipper.addView(listView);
        popup = new PopupWindow(mViewFlipper, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
      //  popup.setBackgroundDrawable(getResources().getDrawable(R.drawable.menulistbg));
        popup.setWidth(150);
        popup.setFocusable(true);
        popup.update();

    }


 

xml代碼

1.  main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
  
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
     <ListView android:id="@+id/listview_catalog"
              android:layout_gravity="right"
              android:layout_width="wrap_content" android:layout_height="wrap_content"
               >
    </ListView>
</LinearLayout>

2.setting_menu.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="40px"
                android:background="#000fff"
               >
    <ImageView
            android:id="@+id/menu_image"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:paddingLeft="5dip"
            />
    <TextView
            android:id="@+id/menu_content"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:paddingLeft="10dip"
            android:layout_toRightOf="@id/menu_image"
            android:textColor="#000000"
            android:layout_marginTop="5dip"
            android:textStyle="bold"
            />
 </RelativeLayout>

3.menu_in.xml

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

 

4.menu_out.xml

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

 

 

發佈了55 篇原創文章 · 獲贊 28 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章