android 自定义下拉菜单

简介:     本实例的自定义下拉菜单主要是继承PopupWindow类来实现的弹出窗体,各种布局效果可以根据自己定义设计。弹出的动画效果主要用到了translate、alpha、scale,具体实现步骤如下:          先上效果图如下:左边下拉菜单、中间下拉菜单、右边下拉菜单                   1.主界面布局 activity_main.xml

    本实例的自定义下拉菜单主要是继承PopupWindow类来实现的弹出窗体,各种布局效果可以根据自己定义设计。弹出的动画效果主要用到了translate、alpha、scale,具体实现步骤如下:

         先上效果图如下:左边下拉菜单、中间下拉菜单、右边下拉菜单

                 

1.主界面布局 activity_main.xml:

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#ffffff" >  
  6.   
  7.     <include  
  8.         android:id="@+id/main_top"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         layout="@layout/urm_top" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/rule_line_tv"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="0.5dp"  
  17.         android:layout_below="@id/main_top"  
  18.         android:background="@color/reserve_line" />  
  19.   
  20.     <LinearLayout  
  21.         android:id="@+id/main_ll"  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:layout_below="@id/rule_line_tv"  
  25.         android:gravity="center_vertical"  
  26.         android:orientation="horizontal"  
  27.         android:padding="10dp" >  
  28.   
  29.         <TextView  
  30.             android:id="@+id/left_tv"  
  31.             android:layout_width="0dp"  
  32.             android:layout_height="wrap_content"  
  33.             android:layout_weight="1"  
  34.             android:ellipsize="end"  
  35.             android:gravity="center_horizontal"  
  36.             android:maxLength="4"  
  37.             android:singleLine="true"  
  38.             android:text="我负责的线索" />  
  39.   
  40.         <TextView  
  41.             android:id="@+id/middle_tv"  
  42.             android:layout_width="0dp"  
  43.             android:layout_height="wrap_content"  
  44.             android:layout_weight="1"  
  45.             android:ellipsize="end"  
  46.             android:gravity="center_horizontal"  
  47.             android:maxLength="4"  
  48.             android:singleLine="true"  
  49.             android:text="团队" />  
  50.   
  51.         <TextView  
  52.             android:id="@+id/right_tv"  
  53.             android:layout_width="0dp"  
  54.             android:layout_height="wrap_content"  
  55.             android:layout_weight="1"  
  56.             android:ellipsize="end"  
  57.             android:gravity="center_horizontal"  
  58.             android:maxLength="4"  
  59.             android:singleLine="true"  
  60.             android:text="自定义" />  
  61.     </LinearLayout>  
  62.   
  63.     <TextView  
  64.         android:id="@+id/rule_line01_tv"  
  65.         android:layout_width="match_parent"  
  66.         android:layout_height="0.5dp"  
  67.         android:layout_below="@id/main_ll"  
  68.         android:background="@color/reserve_line" />  
  69.   
  70.     <TextView  
  71.         android:id="@+id/main_tv"  
  72.         android:layout_width="wrap_content"  
  73.         android:layout_height="wrap_content"  
  74.         android:layout_centerInParent="true"  
  75.         android:text="主界面" />  
  76.   
  77. </RelativeLayout>  

2.主界面测试类 MainActivity.java

[java]  view plain  copy
  1. package com.popuptest;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.os.Bundle;  
  5. import android.util.DisplayMetrics;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.Window;  
  9. import android.widget.AdapterView;  
  10. import android.widget.Button;  
  11. import android.widget.ImageButton;  
  12. import android.widget.ImageView;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.TextView;  
  15. import android.widget.AdapterView.OnItemClickListener;  
  16. import android.widget.RelativeLayout.LayoutParams;  
  17. import android.app.Activity;  
  18.   
  19. public class MainActivity extends Activity implements OnClickListener {  
  20.   
  21.     public static int screenW, screenH;  
  22.   
  23.     private ImageButton backBtn, createBtn;  
  24.     private Button confirmBtn;  
  25.     private TextView topTv;  
  26.     private LinearLayout topll;  
  27.     private ImageView topIv;  
  28.     private TextView topLineTv;  
  29.   
  30.     private TopMiddlePopup middlePopup;  
  31.   
  32.     @Override  
  33.     protected void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  36.         setContentView(R.layout.activity_main);  
  37.         getScreenPixels();  
  38.         initWidget();  
  39.     }  
  40.   
  41.     /** 
  42.      * 初始化控件 
  43.      */  
  44.     private void initWidget() {  
  45.         backBtn = (ImageButton) findViewById(R.id.urm_back_btn);  
  46.         createBtn = (ImageButton) findViewById(R.id.urm_create_btn);  
  47.         confirmBtn = (Button) findViewById(R.id.urm_confirm_btn);  
  48.   
  49.         topll = (LinearLayout) findViewById(R.id.urm_top_ll);  
  50.         topIv = (ImageView) findViewById(R.id.urm_top_iv);  
  51.   
  52.         topLineTv = (TextView) findViewById(R.id.rule_line_tv);  
  53.   
  54.         topTv = (TextView) findViewById(R.id.urm_top_tv);  
  55.         topTv.setText("企业客户");  
  56.   
  57.         backBtn.setOnClickListener(this);  
  58.         createBtn.setOnClickListener(this);  
  59.         confirmBtn.setOnClickListener(this);  
  60.         topll.setOnClickListener(this);  
  61.   
  62.     }  
  63.   
  64.     /** 
  65.      * 设置弹窗 
  66.      *  
  67.      * @param type 
  68.      */  
  69.     private void setPopup(int type) {  
  70.         middlePopup = new TopMiddlePopup(MainActivity.this, screenW, screenH,  
  71.                 onItemClickListener, getItemsName(), type);  
  72.     }  
  73.   
  74.     /** 
  75.      * 设置弹窗内容 
  76.      *  
  77.      * @return 
  78.      */  
  79.     private ArrayList<String> getItemsName() {  
  80.         ArrayList<String> items = new ArrayList<String>();  
  81.         items.add("企业客户");  
  82.         items.add("集团客户");  
  83.         items.add("公海客户");  
  84.         return items;  
  85.     }  
  86.   
  87.     @Override  
  88.     public void onClick(View v) {  
  89.         switch (v.getId()) {  
  90.         case R.id.urm_back_btn:  
  91.             setPopup(1);  
  92.             middlePopup.show(topLineTv);  
  93.             break;  
  94.         case R.id.urm_create_btn:  
  95.             setPopup(2);  
  96.             middlePopup.show(topLineTv);  
  97.             break;  
  98.         case R.id.urm_confirm_btn:  
  99.   
  100.             break;  
  101.         case R.id.urm_top_ll:  
  102.             setPopup(0);  
  103.             middlePopup.show(topLineTv);  
  104.             break;  
  105.         }  
  106.     }  
  107.   
  108.     /** 
  109.      * 弹窗点击事件 
  110.      */  
  111.     private OnItemClickListener onItemClickListener = new OnItemClickListener() {  
  112.   
  113.         @Override  
  114.         public void onItemClick(AdapterView<?> parent, View view, int position,  
  115.                 long id) {  
  116.             System.out.println("--onItemClickListener--:");  
  117.             middlePopup.dismiss();  
  118.         }  
  119.     };  
  120.   
  121.     /** 
  122.      * 获取屏幕的宽和高 
  123.      */  
  124.     public void getScreenPixels() {  
  125.         DisplayMetrics metrics = new DisplayMetrics();  
  126.         getWindowManager().getDefaultDisplay().getMetrics(metrics);  
  127.         screenW = metrics.widthPixels;  
  128.         screenH = metrics.heightPixels;  
  129.     }  
  130.   
  131. }  

3.自定义弹窗类 TopMiddlePopup.java

[java]  view plain  copy
  1. package com.popuptest;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.content.Context;  
  5. import android.graphics.drawable.ColorDrawable;  
  6. import android.view.LayoutInflater;  
  7. import android.view.MotionEvent;  
  8. import android.view.View;  
  9. import android.view.View.OnTouchListener;  
  10. import android.view.ViewGroup.LayoutParams;  
  11. import android.widget.LinearLayout;  
  12. import android.widget.ListView;  
  13. import android.widget.PopupWindow;  
  14. import android.widget.AdapterView.OnItemClickListener;  
  15.   
  16. public class TopMiddlePopup extends PopupWindow {  
  17.   
  18.     private Context myContext;  
  19.     private ListView myLv;  
  20.     private OnItemClickListener myOnItemClickListener;  
  21.     private ArrayList<String> myItems;  
  22.     private int myWidth;  
  23.     private int myHeight;  
  24.     private int myType;  
  25.   
  26.     // 判断是否需要添加或更新列表子类项  
  27.     private boolean myIsDirty = true;  
  28.   
  29.     private LayoutInflater inflater = null;  
  30.     private View myMenuView;  
  31.   
  32.     private LinearLayout popupLL;  
  33.   
  34.     private PopupAdapter adapter;  
  35.   
  36.     public TopMiddlePopup(Context context) {  
  37.         // TODO Auto-generated constructor stub  
  38.     }  
  39.   
  40.     public TopMiddlePopup(Context context, int width, int height,  
  41.             OnItemClickListener onItemClickListener, ArrayList<String> items,  
  42.             int type) {  
  43.   
  44.         inflater = (LayoutInflater) context  
  45.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  46.         myMenuView = inflater.inflate(R.layout.top_popup, null);  
  47.   
  48.         this.myContext = context;  
  49.         this.myItems = items;  
  50.         this.myOnItemClickListener = onItemClickListener;  
  51.         this.myType = type;  
  52.   
  53.         this.myWidth = width;  
  54.         this.myHeight = height;  
  55.   
  56.         System.out.println("--myWidth--:" + myWidth + "--myHeight--:"  
  57.                 + myHeight);  
  58.         initWidget();  
  59.         setPopup();  
  60.     }  
  61.   
  62.     /** 
  63.      * 初始化控件 
  64.      */  
  65.     private void initWidget() {  
  66.         myLv = (ListView) myMenuView.findViewById(R.id.popup_lv);  
  67.         popupLL = (LinearLayout) myMenuView.findViewById(R.id.popup_layout);  
  68.         myLv.setOnItemClickListener(myOnItemClickListener);  
  69.   
  70.         if (myType == 1) {  
  71.             android.widget.RelativeLayout.LayoutParams lpPopup = (android.widget.RelativeLayout.LayoutParams) popupLL  
  72.                     .getLayoutParams();  
  73.             lpPopup.width = (int) (myWidth * 1.0 / 4);  
  74.             lpPopup.setMargins(00, (int) (myWidth * 3.0 / 4), 0);  
  75.             popupLL.setLayoutParams(lpPopup);  
  76.         } else if (myType == 2) {  
  77.             android.widget.RelativeLayout.LayoutParams lpPopup = (android.widget.RelativeLayout.LayoutParams) popupLL  
  78.                     .getLayoutParams();  
  79.             lpPopup.width = (int) (myWidth * 1.0 / 4);  
  80.             lpPopup.setMargins((int) (myWidth * 3.0 / 4), 000);  
  81.             popupLL.setLayoutParams(lpPopup);  
  82.         }  
  83.     }  
  84.   
  85.     /** 
  86.      * 设置popup的样式 
  87.      */  
  88.     private void setPopup() {  
  89.         // 设置AccessoryPopup的view  
  90.         this.setContentView(myMenuView);  
  91.         // 设置AccessoryPopup弹出窗体的宽度  
  92.         this.setWidth(LayoutParams.MATCH_PARENT);  
  93.         // 设置AccessoryPopup弹出窗体的高度  
  94.         this.setHeight(LayoutParams.MATCH_PARENT);  
  95.         // 设置AccessoryPopup弹出窗体可点击  
  96.         this.setFocusable(true);  
  97.         // 设置AccessoryPopup弹出窗体的动画效果  
  98.         if (myType == 1) {  
  99.             this.setAnimationStyle(R.style.AnimTopLeft);  
  100.         } else if (myType == 2) {  
  101.             this.setAnimationStyle(R.style.AnimTopRight);  
  102.         } else {  
  103.             //this.setAnimationStyle(R.style.AnimTop);  
  104.             this.setAnimationStyle(R.style.AnimTopMiddle);  
  105.         }  
  106.         // 实例化一个ColorDrawable颜色为半透明  
  107.         ColorDrawable dw = new ColorDrawable(0x33000000);  
  108.         // 设置SelectPicPopupWindow弹出窗体的背景  
  109.         this.setBackgroundDrawable(dw);  
  110.   
  111.         myMenuView.setOnTouchListener(new OnTouchListener() {  
  112.   
  113.             @Override  
  114.             public boolean onTouch(View v, MotionEvent event) {  
  115.   
  116.                 int height = popupLL.getBottom();  
  117.                 int left = popupLL.getLeft();  
  118.                 int right = popupLL.getRight();  
  119.                 System.out.println("--popupLL.getBottom()--:"  
  120.                         + popupLL.getBottom());  
  121.                 int y = (int) event.getY();  
  122.                 int x = (int) event.getX();  
  123.                 if (event.getAction() == MotionEvent.ACTION_UP) {  
  124.                     if (y > height || x < left || x > right) {  
  125.                         System.out.println("---点击位置在列表下方--");  
  126.                         dismiss();  
  127.                     }  
  128.                 }  
  129.                 return true;  
  130.             }  
  131.         });  
  132.     }  
  133.   
  134.     /** 
  135.      * 显示弹窗界面 
  136.      *  
  137.      * @param view 
  138.      */  
  139.     public void show(View view) {  
  140.         if (myIsDirty) {  
  141.             myIsDirty = false;  
  142.             adapter = new PopupAdapter(myContext, myItems, myType);  
  143.             myLv.setAdapter(adapter);  
  144.         }  
  145.   
  146.         showAsDropDown(view, 00);  
  147.     }  
  148.   
  149. }  

4.自定义弹窗布局 top_popup.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <LinearLayout  
  7.         android:id="@+id/popup_layout"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentTop="true"  
  11.         android:background="#ffffff"  
  12.         android:orientation="vertical" >  
  13.   
  14.         <ListView  
  15.             android:id="@+id/popup_lv"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="match_parent"  
  18.             android:divider="@color/content_line"  
  19.             android:dividerHeight="0.5dp" >  
  20.         </ListView>  
  21.   
  22.         <TextView  
  23.             android:layout_width="match_parent"  
  24.             android:layout_height="0.5dp"  
  25.             android:background="@color/reserve_line" />  
  26.     </LinearLayout>  
  27.   
  28. </RelativeLayout>  

5.弹窗类表适配器类 PopupAdapter

[java]  view plain  copy
  1. package com.popuptest;  
  2.   
  3. import java.util.ArrayList;  
  4. import android.content.Context;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.BaseAdapter;  
  10. import android.widget.RelativeLayout.LayoutParams;  
  11. import android.widget.TextView;  
  12.   
  13. public class PopupAdapter extends BaseAdapter {  
  14.     private Context myContext;  
  15.     private LayoutInflater inflater;  
  16.     private ArrayList<String> myItems;  
  17.     private int myType;  
  18.   
  19.     public PopupAdapter(Context context, ArrayList<String> items, int type) {  
  20.         this.myContext = context;  
  21.         this.myItems = items;  
  22.         this.myType = type;  
  23.   
  24.         inflater = LayoutInflater.from(myContext);  
  25.   
  26.     }  
  27.   
  28.     @Override  
  29.     public int getCount() {  
  30.         return myItems.size();  
  31.     }  
  32.   
  33.     @Override  
  34.     public String getItem(int position) {  
  35.         return myItems.get(position);  
  36.     }  
  37.   
  38.     @Override  
  39.     public long getItemId(int position) {  
  40.         return 0;  
  41.     }  
  42.   
  43.     @Override  
  44.     public View getView(int position, View convertView, ViewGroup parent) {  
  45.         PopupHolder holder = null;  
  46.         if (convertView == null) {  
  47.             holder = new PopupHolder();  
  48.             convertView = inflater.inflate(R.layout.top_popup_item, null);  
  49.             holder.itemNameTv = (TextView) convertView  
  50.                     .findViewById(R.id.popup_tv);  
  51.             if (myType == 0) {  
  52.                 holder.itemNameTv.setGravity(Gravity.CENTER);  
  53.             } else if (myType == 1) {  
  54.                 holder.itemNameTv.setGravity(Gravity.LEFT);  
  55.             } else if (myType == 2) {  
  56.                 holder.itemNameTv.setGravity(Gravity.RIGHT);  
  57.             }  
  58.             convertView.setTag(holder);  
  59.         } else {  
  60.             holder = (PopupHolder) convertView.getTag();  
  61.         }  
  62.         String itemName = getItem(position);  
  63.         holder.itemNameTv.setText(itemName);  
  64.         return convertView;  
  65.     }  
  66.   
  67.     private class PopupHolder {  
  68.         TextView itemNameTv;  
  69.     }  
  70.   
  71. }  

6.子item布局 top_popup_item.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#ffffff"  
  6.     android:padding="10dp" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/popup_tv"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"   
  12.         style="@style/urm_tv"/>  
  13.   
  14. </RelativeLayout>  

7.主界面顶部布局 urm_top.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#eeeeee" >  
  6.   
  7.     <ImageButton  
  8.         android:id="@+id/urm_back_btn"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:background="@null"  
  13.         android:contentDescription="@string/app_name"  
  14.         android:src="@drawable/back" />  
  15.   
  16.     <LinearLayout  
  17.         android:id="@+id/urm_top_ll"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_centerInParent="true"  
  21.         android:gravity="center_vertical"  
  22.         android:orientation="horizontal" >  
  23.   
  24.         <TextView  
  25.             android:id="@+id/urm_top_tv"  
  26.             style="@style/main_tv_style"  
  27.             android:layout_width="wrap_content"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="企业客户" />  
  30.   
  31.         <ImageView  
  32.             android:id="@+id/urm_top_iv"  
  33.             android:layout_width="wrap_content"  
  34.             android:layout_height="wrap_content"  
  35.             android:layout_marginLeft="5dp"  
  36.             android:background="@null"  
  37.             android:contentDescription="@string/app_name"  
  38.             android:src="@drawable/switch02" />  
  39.     </LinearLayout>  
  40.   
  41.     <RelativeLayout  
  42.         android:id="@+id/urm_top_right_rl"  
  43.         android:layout_width="wrap_content"  
  44.         android:layout_height="wrap_content"  
  45.         android:layout_alignParentRight="true"  
  46.         android:layout_centerVertical="true" >  
  47.   
  48.         <ImageButton  
  49.             android:id="@+id/urm_create_btn"  
  50.             android:layout_width="wrap_content"  
  51.             android:layout_height="wrap_content"  
  52.             android:background="@null"  
  53.             android:contentDescription="@string/app_name"  
  54.             android:src="@drawable/btn_add_2x" />  
  55.   
  56.         <Button  
  57.             android:id="@+id/urm_confirm_btn"  
  58.             android:layout_width="wrap_content"  
  59.             android:layout_height="wrap_content"  
  60.             android:background="@null"  
  61.             android:gravity="center_vertical"  
  62.             android:padding="10dp"  
  63.             android:text="确定"  
  64.             android:textColor="@color/blue2"  
  65.             android:textSize="18sp"  
  66.             android:visibility="gone" />  
  67.     </RelativeLayout>  
  68.   
  69.     <ImageButton  
  70.         android:id="@+id/urm_search_btn"  
  71.         android:layout_width="wrap_content"  
  72.         android:layout_height="wrap_content"  
  73.         android:layout_centerVertical="true"  
  74.         android:layout_toLeftOf="@id/urm_top_right_rl"  
  75.         android:background="@null"  
  76.         android:contentDescription="@string/app_name"  
  77.         android:src="@drawable/search"  
  78.         android:visibility="gone" />  
  79.   
  80. </RelativeLayout>  

8.styles.xml文件

[html]  view plain  copy
  1. <resources>  
  2.   
  3.     <!--  
  4.         Base application theme, dependent on API level. This theme is replaced  
  5.         by AppBaseTheme from res/values-vXX/styles.xml on newer devices.  
  6.     -->  
  7.     <style name="AppBaseTheme" parent="android:Theme.Light">  
  8.         <!--  
  9.             Theme customizations available in newer API levels can go in  
  10.             res/values-vXX/styles.xml, while customizations related to  
  11.             backward-compatibility can go here.  
  12.         -->  
  13.     </style>  
  14.   
  15.     <!-- Application theme. -->  
  16.     <style name="AppTheme" parent="AppBaseTheme">  
  17.         <!-- All customizations that are NOT specific to a particular API-level can go here. -->  
  18.     </style>  
  19.       
  20.     <style name="AnimTop" parent="@android:style/Animation">  
  21.         <item name="android:windowEnterAnimation">@anim/push_top_in</item>  
  22.         <item name="android:windowExitAnimation">@anim/push_top_out</item>  
  23.     </style>  
  24.       
  25.      <style name="AnimTopRight" parent="@android:style/Animation">  
  26.         <item name="android:windowEnterAnimation">@anim/top_right_in</item>  
  27.         <item name="android:windowExitAnimation">@anim/top_right_out</item>  
  28.     </style>  
  29.       
  30.      <style name="AnimTopLeft" parent="@android:style/Animation">  
  31.         <item name="android:windowEnterAnimation">@anim/top_left_in</item>  
  32.         <item name="android:windowExitAnimation">@anim/top_left_out</item>  
  33.     </style>  
  34.       
  35.      <style name="AnimTopMiddle" parent="@android:style/Animation">  
  36.         <item name="android:windowEnterAnimation">@anim/top_middle_in</item>  
  37.         <item name="android:windowExitAnimation">@anim/top_middle_out</item>  
  38.     </style>  
  39.       
  40.     <style name="main_tv_style">  
  41.         <item name="android:textSize">20sp</item>  
  42.         <item name="android:textColor">#000000</item>  
  43.     </style>  
  44.   
  45.     <style name="urm_tv">  
  46.         <item name="android:textSize">18sp</item>  
  47.     </style>  
  48. </resources>  

9.各种动画效果

push_top_in.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 从屏幕上面进入 -->  
  3. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  4.   
  5.     <translate  
  6.         android:duration="500"  
  7.         android:fromYDelta="-100%p"  
  8.         android:toYDelta="0" />  
  9.   
  10.     <alpha  
  11.         android:duration="500"  
  12.         android:fromAlpha="0.0"  
  13.         android:toAlpha="1.0" />  
  14.   
  15. </set>  

push_top_out.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 从屏幕上面退出 -->  
  3. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  4.   
  5.     <translate  
  6.         android:duration="500"  
  7.         android:fromYDelta="0"  
  8.         android:toYDelta="-100%p" />  
  9.   
  10.     <alpha  
  11.         android:duration="500"  
  12.         android:fromAlpha="1.0"  
  13.         android:toAlpha="0.0" />  
  14.   
  15. </set>  

top_left_in.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="0.0"  
  8.         android:fromYScale="0.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="0%"  
  11.         android:pivotY="0%"  
  12.         android:toXScale="1.0"  
  13.         android:toYScale="1.0" />  
  14.   
  15. </set>  

top_left_out.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="1.0"  
  8.         android:fromYScale="1.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="0%"  
  11.         android:pivotY="0%"  
  12.         android:toXScale="0.0"  
  13.         android:toYScale="0.0" />  
  14.   
  15. </set>  

top_middle_in.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="0.0"  
  8.         android:fromYScale="0.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="50%"  
  11.         android:pivotY="0%"  
  12.         android:toXScale="1.0"  
  13.         android:toYScale="1.0" />  
  14.   
  15. </set>  

top_middle_out.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="1.0"  
  8.         android:fromYScale="1.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="50%"  
  11.         android:pivotY="0%"  
  12.         android:toXScale="0.0"  
  13.         android:toYScale="0.0" />  
  14.   
  15. </set>  

top_right_in.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="0.0"  
  8.         android:fromYScale="0.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="100%"  
  11.         android:pivotY="0%"  
  12.         android:toXScale="1.0"  
  13.         android:toYScale="1.0" />  
  14.   
  15. </set>  

top_right_out.xml

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fillAfter="false"  
  7.         android:fromXScale="1.0"  
  8.         android:fromYScale="1.0"  
  9.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  10.         android:pivotX="100%"  
  11.         android:pivotY="0%"  
  12.         android:toXScale="0.0"  
  13.         android:toYScale="0.0" />  
  14.   
  15. </set>  

运行项目即可搞定!
目录
相关文章
|
24天前
|
XML Java Android开发
Android实现自定义进度条(源码+解析)
Android实现自定义进度条(源码+解析)
51 1
|
4月前
|
XML Android开发 数据安全/隐私保护
Android 自定义开源库 EasyView
Android 自定义开源库 EasyView
|
4月前
|
XML Java Android开发
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
Android Studio App开发中改造已有的控件实战(包括自定义支付宝月份选择器、给翻页栏添加新属性、不滚动的列表视图 附源码)
42 1
|
7月前
|
API Android开发
Android 自定义最大宽度,高度, 宽高比例 Layout
Android 自定义最大宽度,高度, 宽高比例 Layout
|
4月前
|
XML 搜索推荐 Java
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
Android App开发之自定义图形中位图与图形互转、剪裁图形内部区域、给图形添加部件的讲解及实战(附源码 简单易懂)
33 0
|
4月前
|
XML 前端开发 Java
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
Android Studio App自定义控件中自定义视图的绘制讲解及实战(附源码 包括自定义绘制各种图形)
37 1
|
3天前
|
移动开发 Java Unix
Android系统 自动加载自定义JAR文件
Android系统 自动加载自定义JAR文件
21 1
|
3天前
|
Shell Android开发 开发者
Android系统 自定义动态修改init.custom.rc
Android系统 自定义动态修改init.custom.rc
23 0
|
3天前
|
存储 安全 Android开发
Android系统 自定义系统和应用权限
Android系统 自定义系统和应用权限
18 0
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条