1. 程式人生 > >Android popupwindow在指定控制元件正下方滑動彈出效果

Android popupwindow在指定控制元件正下方滑動彈出效果

用popupwindow實現在某空間正下方滑動彈出,並用SharedPreferences記錄選擇當前項的位置。


activity的佈局檔案:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@drawable/bg_work">
    
	<TextView 
	    android:id="@+id/tv_pop"
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:text="名稱排序"
	    android:textSize="16sp"
	    android:drawableRight="@drawable/back"
	    android:padding="10dp"/>
	<View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000"/>
</LinearLayout>

popupwindow的佈局檔案:
<?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" 
    android:background="#345435">

    <RelativeLayout
        android:id="@+id/rl_01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv_01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="名稱排序"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/iv_01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:padding="10dp"
            android:src="@drawable/back"
            android:visibility="gone" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000" />

    <RelativeLayout
        android:id="@+id/rl_02"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv_02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="從高到低排序"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/iv_02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:padding="10dp"
            android:src="@drawable/back"
            android:visibility="gone" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000" />

    <RelativeLayout
        android:id="@+id/rl_03"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/tv_03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="從低到高排序"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/iv_03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:padding="10dp"
            android:src="@drawable/back"
            android:visibility="gone" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#000000" />

</LinearLayout>

動畫樣式需要在styles.xml中定義:
<style name="style_pop_animation">
        <item name="android:windowEnterAnimation">@anim/anim_pop_in</item>
        <item name="android:windowExitAnimation">@anim/anim_pop_out</item>
    </style>

動畫須在anim資料夾中定義:

彈出動畫anim_pop_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="300"
        android:fromXScale="1.0"
        android:fromYScale="0"
        android:pivotX="100%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="1.0"/>

</set>

隱藏動畫anim_pop_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:duration="300"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="100%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="0.0"/>

</set>

activity程式碼
public class PopUpWindowActivity extends Activity implements OnClickListener {
	private TextView tv_pop;
	private PopupWindow popupWindow;
	private SharedPreferences sp;
	private ImageView iv_01, iv_02, iv_03;
	private TextView tv_01, tv_02, tv_03;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_popupwindow);
		sp = getSharedPreferences("state", MODE_PRIVATE);
		// 初始化時清除儲存的狀態資料
		Editor edit = sp.edit();
		edit.clear();
		edit.commit();

		initView();
	}

	private void initView() {
		tv_pop = (TextView) findViewById(R.id.tv_pop);
		tv_pop.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_pop:
			showPopWindow(getView());
			break;

		default:
			break;
		}
	}

	private void showPopWindow(View view) {
		try {
			
			popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			popupWindow.setFocusable(true);
			popupWindow.setOutsideTouchable(true);
			ColorDrawable cd = new ColorDrawable(0x00ffffff);// 背景顏色全透明
			popupWindow.setBackgroundDrawable(cd);
			int[] location = new int[2];
			tv_pop.getLocationOnScreen(location);
			popupWindow.setAnimationStyle(R.style.style_pop_animation);// 動畫效果必須放在showAsDropDown()方法上邊,否則無效
			backgroundAlpha(0.5f);// 設定背景半透明
			popupWindow.showAsDropDown(tv_pop);
			//popupWindow.showAtLocation(tv_pop, Gravity.NO_GRAVITY, location[0]+tv_pop.getWidth(),location[1]);
			popupWindow.setOnDismissListener(new OnDismissListener() {

				@Override
				public void onDismiss() {
					popupWindow = null;// 當點選螢幕時,使popupWindow消失
					backgroundAlpha(1.0f);// 當點選螢幕時,使半透明效果取消
				}
			});

			if (sp.getInt("flag", 1) == 1) {
				iv_01.setVisibility(View.VISIBLE);
				tv_01.setTextColor(Color.RED);
			} else if (sp.getInt("flag", 2) == 2) {
				iv_02.setVisibility(View.VISIBLE);
				tv_02.setTextColor(Color.RED);
			} else if (sp.getInt("flag", 3) == 3) {
				iv_03.setVisibility(View.VISIBLE);
				tv_03.setTextColor(Color.RED);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 設定popupWindow背景半透明
	public void backgroundAlpha(float bgAlpha) {
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.alpha = bgAlpha;// 0.0-1.0
		getWindow().setAttributes(lp);
	}

	/*
	 * 得到popupwindow的View
	 */
	private View getView() {
		View view = LayoutInflater.from(PopUpWindowActivity.this).inflate(
				R.layout.layout_pop_item, null);
		iv_01 = (ImageView) view.findViewById(R.id.iv_01);
		iv_02 = (ImageView) view.findViewById(R.id.iv_02);
		iv_03 = (ImageView) view.findViewById(R.id.iv_03);
		tv_01 = (TextView) view.findViewById(R.id.tv_01);
		tv_02 = (TextView) view.findViewById(R.id.tv_02);
		tv_03 = (TextView) view.findViewById(R.id.tv_03);
		RelativeLayout rl_01 = (RelativeLayout) view.findViewById(R.id.rl_01);
		RelativeLayout rl_02 = (RelativeLayout) view.findViewById(R.id.rl_02);
		RelativeLayout rl_03 = (RelativeLayout) view.findViewById(R.id.rl_03);

		rl_01.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				tv_pop.setText("名稱排序");
				saveCurrentState(1);
			}
		});
		rl_02.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				popupWindow.dismiss();
				tv_pop.setText("從高到低排序");
				saveCurrentState(2);

			}
		});
		rl_03.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				iv_03.setVisibility(View.VISIBLE);
				popupWindow.dismiss();
				tv_pop.setText("從低到高排序");
				saveCurrentState(3);
			}
		});
		return view;
	}

	/*
	 * 儲存當前狀態 1-->名稱排序 2-->從高到低 3-->從低到高
	 */
	private void saveCurrentState(int i) {
		Editor editor = sp.edit();
		editor.putInt("flag", i);
		editor.commit();
	}
}

基本就能實現了。