標籤:android2d android開發 popwindow popanimation
1、什麼是popWindow?
popWindow就是對話方塊的一種方式!此文講解的android中對話方塊的一種使用方式,它叫popWindow。
2、popWindow的特性
Android的對話方塊有兩種:PopupWindow和AlertDialog。它們的不同點在於:
AlertDialog的位置固定,而PopupWindow的位置可以隨意。
AlertDialog是非阻塞線程的,而PopupWindow是阻塞線程的。
PopupWindow的位置按照有無位移分,可以分為位移和無位移兩種;按照參照物的不同,可以分為相對於某個控制項(Anchor錨)和相對於父控制項。具體如下
showAsDropDown(View anchor):相對某個控制項的位置(正左下方),無位移
showAsDropDown(View anchor, int xoff, int yoff):相對某個控制項的位置,有位移
showAtLocation(View parent, int gravity, int x, int y):相對於父控制項的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設定位移或無位移
3、popWindow的設計
:
實現這個效果的,總共有兩個三個xml布局,一個是工具列布局,一個是popWindow布局,一個是父介面布局。
工具列布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="@dimen/chat_llyt_title_height" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="@color/black" android:orientation="horizontal" > <ImageView android:id="@+id/app_iv_back" android:layout_width="@dimen/chat_llyt_title_height" android:layout_height="fill_parent" android:background="@drawable/actionbar_bg_left" android:layout_gravity="center" android:padding="7dip" android:scaleType="centerInside" /> <TextView android:id="@+id/app_tv_barname" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="4.0" android:background="@drawable/actionbar_bg_middle" android:gravity="center" android:text="@string/app_name" android:textColor="@color/white" android:textSize="@dimen/wc_title_textsize" /> <ImageView android:id="@+id/app_iv_search" android:layout_width="@dimen/chat_llyt_title_height" android:layout_height="fill_parent" android:background="@drawable/actionbar_bg_right" android:padding="7dip" android:layout_gravity="center" android:scaleType="centerInside" android:src="@drawable/yuyou_bg_pop" /></LinearLayout>
popWindow布局:
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rlyt_menu" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="9" /> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:orientation="vertical" > <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.05" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2.3" android:background="@drawable/yuyou_pop_menu" android:orientation="vertical" > <View android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.1" /> <LinearLayout android:id="@+id/rlyt_code" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical" > <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:contentDescription="@drawable/face_code_bg" android:paddingLeft="10dp" android:paddingRight="10dp" android:src="@drawable/face_code_bg" android:scaleType="centerInside" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal|top" android:text="掃一掃" android:textColor="@color/white" android:textSize="@dimen/yuyou_pop_text_size" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.05" android:visibility="gone"/> <LinearLayout android:id="@+id/rlyt_his" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical" > <ImageView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:contentDescription="@drawable/face_his_bg" android:paddingLeft="10dp" android:paddingRight="10dp" android:src="@drawable/face_his_bg" android:scaleType="centerInside" /> <TextView android:id="@+id/tv_ting_tong" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal|top" android:text="記錄" android:textColor="@color/white" android:textSize="@dimen/yuyou_pop_text_size" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.05" android:visibility="gone"/> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="6" /> </LinearLayout> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0.05" /></LinearLayout>
父介面布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="@dimen/chat_llyt_title_height" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="@color/black" android:orientation="horizontal" > <ImageView android:id="@+id/app_iv_back" android:layout_width="@dimen/chat_llyt_title_height" android:layout_height="fill_parent" android:background="@drawable/actionbar_bg_left" android:layout_gravity="center" android:padding="7dip" android:scaleType="centerInside" /> <TextView android:id="@+id/app_tv_barname" android:layout_width="0.0dip" android:layout_height="fill_parent" android:layout_weight="4.0" android:background="@drawable/actionbar_bg_middle" android:gravity="center" android:text="@string/app_name" android:textColor="@color/white" android:textSize="@dimen/wc_title_textsize" /> <ImageView android:id="@+id/app_iv_search" android:layout_width="@dimen/chat_llyt_title_height" android:layout_height="fill_parent" android:background="@drawable/actionbar_bg_right" android:padding="7dip" android:layout_gravity="center" android:scaleType="centerInside" android:src="@drawable/yuyou_bg_pop" /></LinearLayout>
對話方塊類,繼承OnClickListener,對點擊事件的相應:
package com.example.popwindow;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.widget.PopupWindow;public class MySimplePopMenu implements OnClickListener {private final PopupWindow popupWindow;private Activity mParent;public MySimplePopMenu(Activity mParent, int hight) {this.mParent = mParent;View menu = initMenuView(mParent);popupWindow = new PopupWindow(menu);popupWindow.setWidth(LayoutParams.MATCH_PARENT);popupWindow.setHeight(hight);popupWindow.setAnimationStyle(R.style.PopAnimation);}/** * 設定具體Menu按鈕的監聽 * * @param mParent * @return */private View initMenuView(Activity mParent) {View menuView = mParent.getLayoutInflater().inflate(R.layout.my_simple_popupwindow_menu, null);menuView.findViewById(R.id.rlyt_menu).setOnClickListener(this);menuView.findViewById(R.id.rlyt_code).setOnClickListener(this);menuView.findViewById(R.id.rlyt_his).setOnClickListener(this);return menuView;}@Overridepublic void onClick(View view) {switch (view.getId()) {// 點擊空白處case R.id.rlyt_menu:if (popupWindow != null && popupWindow.isShowing()) {popupWindow.dismiss();}break;// 掃一掃case R.id.rlyt_code:break;// 記錄case R.id.rlyt_his:break;}}/** * 返回View * * @return */public PopupWindow getMenu() {return popupWindow;}}
這段代碼中的popupWindow.setAnimationStyle(R.style.PopAnimation);這行代碼需要特別注意,作用於對話方塊的彈出效果,如何使用可關注的我的博文:http://blog.csdn.net/stoppig/article/details/21481057。使用原理是差不多的。
主activity代碼:
package com.example.popwindow;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.view.Display;import android.view.Gravity;import android.view.LayoutInflater;import android.view.Menu;import android.view.View;import android.view.WindowManager;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.PopupWindow;import android.widget.RelativeLayout;public class MainActivity extends Activity {private MySimplePopMenu popMenu;// 彈出選擇框private PopupWindow mPopupWindow; // 彈出框private ImageView app_iv_search;private boolean mIsFirst = true; // 是否第一次初始化彈出框private Context mContext;private RelativeLayout mLayout;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViews();initListener();this.mContext = this;}private void findViews() {app_iv_search = (ImageView) findViewById(R.id.app_iv_search);mLayout = (RelativeLayout) findViewById(R.id.rv_main);}private void initListener() {app_iv_search.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif (mIsFirst) {mIsFirst = false;int height = mLayout.getHeight() - v.getHeight();popMenu = new MySimplePopMenu(MainActivity.this, height);mPopupWindow = popMenu.getMenu();}if (mPopupWindow == null) {return;}if (mPopupWindow.isShowing()) {mPopupWindow.dismiss();return;}mPopupWindow.showAtLocation(mLayout, Gravity.BOTTOM, 0, 0);}});}/** * 擷取螢幕高度 * * @param context * @return */private int getScreenHeight(Context context) {WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);Display display = manager.getDefaultDisplay();return display.getHeight();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
ps:這段代碼mIsFirst這個屬性是特別注意的,這個屬性控制著對話方塊彈出的次數,如果沒有進行控制,每點一次就會彈出一次。
主要代碼就這樣差不多了。資源地址請點擊:下載資源。