自訂PopupWindow+ListView+Anim,自訂popupwindow

來源:互聯網
上載者:User

自訂PopupWindow+ListView+Anim,自訂popupwindow


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/rl_root"    tools:context="com.example.popupwindowvslistview.MainActivity" >    <RelativeLayout        android:id="@+id/rl_common_default"        android:layout_width="fill_parent"        android:layout_height="48dp"        android:background="@drawable/top_bg" >        <ImageView            android:id="@+id/iv_common_more"            android:layout_width="34dp"            android:layout_height="34dp"            android:padding="5dp"            android:layout_alignParentRight="true"            android:layout_centerVertical="true"            android:layout_marginRight="15dp"            android:src="@drawable/top_more_n"            android:visibility="visible" />    </RelativeLayout></RelativeLayout>


MainActivity

package com.example.popupwindowvslistview;import com.example.popupwindowvslistview.CommonPopuWindow.AnimStyle;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.Window;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.Toast;public class MainActivity extends Activity {private RelativeLayout rl_common_default;private ImageView iv_common_more;private CommonPopuWindow mSmsMorePopup;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initView();initPopup();iv_common_more.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if (mSmsMorePopup.isShowing()) {mSmsMorePopup.dismiss();// 關閉}mSmsMorePopup.show(AnimStyle.RIGHTANIM);}});}private void initPopup() {mSmsMorePopup = new CommonPopuWindow(this, R.style.animation,new CommonPopuWindow.ItemClickCallBack() {@Overridepublic void callBack(int position) {switch (position) {case 0:Toast.makeText(MainActivity.this, "Toast測試彈出清空記錄",Toast.LENGTH_SHORT).show();mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM);break;case 1:Toast.makeText(MainActivity.this, "Toast測試彈出備份",Toast.LENGTH_SHORT).show();mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM);break;case 2:Toast.makeText(MainActivity.this, "Toast測試彈出匯入",Toast.LENGTH_SHORT).show();mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM);break;case 3:Toast.makeText(MainActivity.this, "Toast測試彈出儲值支付",Toast.LENGTH_SHORT).show();mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM);break;case 4:mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM);break;case 5:mSmsMorePopup.thisDismiss(AnimStyle.RIGHTANIM);break;}}}, null);mSmsMorePopup.initData(R.array.messagePopuWindowmore);}private void initView() {rl_common_default = (RelativeLayout) findViewById(R.id.rl_common_default);iv_common_more = (ImageView) findViewById(R.id.iv_common_more);}}
CommonPopuWindow

package com.example.popupwindowvslistview;import android.annotation.SuppressLint;import android.app.Activity;import android.content.Context;import android.content.res.Resources;import android.graphics.Rect;import android.graphics.drawable.BitmapDrawable;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.view.animation.Animation;import android.view.animation.Animation.AnimationListener;import android.view.animation.ScaleAnimation;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.PopupWindow;import android.widget.PopupWindow.OnDismissListener;import android.widget.TextView;import android.widget.LinearLayout.LayoutParams;public class CommonPopuWindow extends PopupWindow implements AnimationListener,OnDismissListener {private Activity mActivity;private View rootView;private ListView mListView;private Resources mResources;private DataAdapter mAdapter;public enum AnimStyle {LEFTANIM, RIGHTANIM}private ScaleAnimation leftShowAnim, rightShowAnim, leftExitAnim,rightExitAnim;private ItemClickCallBack mCallBack;private int animStyle;public CommonPopuWindow(Activity activity, int animStyle,ItemClickCallBack callBack, String lvWidthTag) {this.mActivity = activity;this.mResources = activity.getResources();this.mCallBack = callBack;this.animStyle = animStyle;init(lvWidthTag);}@SuppressLint("InflateParams")@SuppressWarnings("deprecation")private void init(String lvWidthTag) {this.rootView = LayoutInflater.from(mActivity).inflate(R.layout.popupwindow_layout, null);this.mListView = (ListView) rootView.findViewById(R.id.lv_popup_list);this.setContentView(rootView);this.setWidth(LayoutParams.WRAP_CONTENT);this.setHeight(LayoutParams.WRAP_CONTENT);this.setFocusable(true);this.setOnDismissListener(this);this.setBackgroundDrawable(new BitmapDrawable());this.setAnimationStyle(animStyle);this.setOutsideTouchable(true);this.mListView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {if (mCallBack != null) {mCallBack.callBack(position);}}});}/** * 擷取資料 */public void initData(int stringArrayId) {String[] arrays = mResources.getStringArray(stringArrayId);mAdapter = new DataAdapter(arrays);mListView.setAdapter(mAdapter);}/** * 顯示 */public void show(View anchor, int xoff, int yoff, AnimStyle style) {this.showAsDropDown(anchor, xoff, yoff);popupShowAlpha();showAnim(style);}public void show(AnimStyle style) {Rect frame = new Rect();mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);int mMorePopupMarginTop = frame.top + dp2Px(mActivity, 12);int mMorePopupMarginRight = dp2Px(mActivity, 16);popupShowAlpha();this.showAtLocation(mActivity.findViewById(R.id.rl_root), Gravity.RIGHT| Gravity.TOP, mMorePopupMarginRight, mMorePopupMarginTop);showAnim(style);}/** * 顯示動畫效果 */private void showAnim(AnimStyle style) {switch (style) {case LEFTANIM:if (leftShowAnim == null) {leftShowAnim = new ScaleAnimation(0f, 1f, 0f, 1f,Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f);leftShowAnim.setDuration(250);leftShowAnim.setFillAfter(true);}rootView.startAnimation(leftShowAnim);break;case RIGHTANIM:if (rightShowAnim == null) {rightShowAnim = new ScaleAnimation(0f, 1f, 0f, 1f,Animation.RELATIVE_TO_SELF, 1.0f,Animation.RELATIVE_TO_SELF, 0.0f);rightShowAnim.setDuration(250);rightShowAnim.setFillAfter(true);}rootView.startAnimation(rightShowAnim);break;}}/** * 離開動畫效果 */public void thisDismiss(AnimStyle style) {switch (style) {case LEFTANIM:if (leftExitAnim == null) {leftExitAnim = new ScaleAnimation(1f, 0f, 1f, 0f,Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f);leftExitAnim.setDuration(250);leftExitAnim.setFillAfter(true);leftExitAnim.setAnimationListener(this);}rootView.startAnimation(leftExitAnim);break;case RIGHTANIM:if (rightExitAnim == null) {rightExitAnim = new ScaleAnimation(1f, 0f, 1f, 0f,Animation.RELATIVE_TO_SELF, 1.0f,Animation.RELATIVE_TO_SELF, 0.0f);rightExitAnim.setDuration(250);rightExitAnim.setFillAfter(true);rightExitAnim.setAnimationListener(this);}rootView.startAnimation(rightExitAnim);break;}}@Overridepublic void onAnimationEnd(Animation animation) {this.dismiss();}private void popupShowAlpha() {Window window = ((Activity) mActivity).getWindow();WindowManager.LayoutParams params = window.getAttributes();params.alpha = 0.6f;window.setAttributes(params);}private void popupExitAlpha() {Window window = ((Activity) mActivity).getWindow();WindowManager.LayoutParams params = window.getAttributes();params.alpha = 1.0f;window.setAttributes(params);}private class DataAdapter extends BaseAdapter {private String[] arrays;class ViewHolder {TextView dataView;}public DataAdapter(String[] arrays) {this.arrays = arrays;}@Overridepublic int getCount() {if (arrays != null) {return arrays.length;}return 0;}@Overridepublic Object getItem(int position) {if (arrays != null) {return arrays[position];}return null;}@Overridepublic long getItemId(int position) {return position;}@SuppressLint("InflateParams")@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder viewHolder;if (convertView == null) {viewHolder = new ViewHolder();convertView = LayoutInflater.from(mActivity).inflate(R.layout.list_item_popupwindow, null);viewHolder.dataView = (TextView) convertView.findViewById(R.id.tv_list_item);convertView.setTag(viewHolder);} else {viewHolder = (ViewHolder) convertView.getTag();}viewHolder.dataView.setText(arrays[position]);return convertView;}}public interface ItemClickCallBack {void callBack(int position);}@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onDismiss() {popupExitAlpha();}private int dp2Px(Context context, float dp) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dp * scale + 0.5f);}}


popupwindow_layout.xml

<?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="fill_parent" >    <ListView        android:id="@+id/lv_popup_list"        android:layout_width="110dp"        android:layout_height="wrap_content"        android:background="@drawable/layer_popup"        android:divider="@drawable/helper_line"        android:focusableInTouchMode="true"        android:footerDividersEnabled="false"        android:listSelector="@drawable/popupwindow_list_item_text_selector"        android:paddingBottom="5dp"        android:paddingTop="5dp"        android:scrollbars="none" /></LinearLayout>

list_item_popupwindow.xml

<?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="fill_parent"    android:orientation="vertical" >        <TextView         android:id="@+id/tv_list_item"        android:layout_width="fill_parent"        android:layout_height="45dp"        android:textSize="16dp"        android:textColor="#111111"        android:text="aaa"        android:gravity="center"        android:background="@drawable/popupwindow_list_item_text_selector"                /></LinearLayout>

layer_popup.xml

<?xml version="1.0" encoding="utf-8"?>  <layer-list xmlns:android="http://schemas.android.com/apk/res/android">      <!-- Bottom 2dp Shadow -->      <item>          <shape  android:shape="rectangle">              <solid android:color="#d9d9d9" />              <corners android:radius="5dp" />                   </shape>      </item>            <!-- White Top color -->      <item android:bottom="5px">          <shape  android:shape="rectangle">               <solid android:color="#d9d9d9" />               <corners android:radius="5dp" />          </shape>             </item>  </layer-list>  <!--  -->

popupwindow_list_item_text_selector.xml

<?xml version="1.0" encoding="utf-8"?><selector  xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"  android:drawable="@drawable/more_popu_normal"/>    <item android:drawable="@drawable/more_popu_pressed" /></selector>

out.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >     <!--    位移效果    fromXDelta="0%p" 指的x軸是相對於父控制項從父控制項的x=0出開始位移    fromYDelta="0%p" 指的y軸是相對於父控制項從父控制項的y=0出開始位移    toXDelta="100%p" 指的x軸是相對於父控制項到達父控制項的x=100的位置即x軸在螢幕的終點    toYDelta="100%p" 指的y軸是相對於父控制項到達父控制項的y=100的位置即x軸在螢幕的終點     -->     <scale        android:duration="250"      android:pivotX="100%"      android:pivotY="0%"        android:fromXScale="1.0"      android:toXScale="0.0"      android:fromYScale="1.0"      android:toYScale="0.0" />   </set>     

另外還有arrays.xml

<?xml version="1.0" encoding="utf-8"?><resources>        <string-array name="messagePopuWindowmore">        <item >@string/message_clear</item>        <item >@string/back_up</item>        <item >@string/lead_into</item>        <item >@string/pay</item>        <item >@string/set</item>        <item >@string/login_out</item>    </string-array></resources>

color.xml

<?xml version="1.0" encoding="utf-8"?><resources>     <drawable name="more_popu_pressed">#d9d9d9</drawable>    <drawable name="more_popu_normal">#ffffff</drawable></resources>

styles.xml

        <style name="animation">        <item name="android:windowExitAnimation">@anim/out</item></style>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.