首页 > 代码库 > 自定义PopupWindow动画效果

自定义PopupWindow动画效果

 

Java代码  收藏代码
  1. public class RollActivity extends Activity {  
  2.     private View view;  
  3.     private Button btn;  
  4.     private PopupWindow mPopupWindow;  
  5.     private View[] btns;  
  6.     /** Called when the activity is first created. */  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main);  
  11. //      LinearLayout layout=(LinearLayout) view.findViewById(R.id.layout_main);  
  12. //      //设置背景图片旋转180  
  13. //      Bitmap mBitmap=setRotate(R.drawable.bg_kuang);  
  14. //      BitmapDrawable drawable=new BitmapDrawable(mBitmap);  
  15. //      layout.setBackgroundDrawable(drawable);  
  16.           
  17.         btn=(Button) this.findViewById(R.id.btn);  
  18.         btn.setOnClickListener(new OnClickListener(){  
  19.   
  20.             @Override  
  21.             public void onClick(View v) {  
  22.                 // TODO Auto-generated method stub  
  23.                 showPopupWindow(btn);  
  24.             }  
  25.               
  26.         });  
  27.           
  28.         initPopupWindow(R.layout.popwindow);  
  29.   
  30.     }  
  31.       
  32.     private void initPopupWindow(int resId){  
  33.         LayoutInflater mLayoutInflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);  
  34.         view = mLayoutInflater.inflate(resId, null);  
  35.               
  36.         mPopupWindow = new PopupWindow(view, 400,LayoutParams.WRAP_CONTENT);  
  37. //      mPopupWindow.setBackgroundDrawable(new BitmapDrawable());//必须设置background才能消失  
  38.         mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_frame));  
  39.         mPopupWindow.setOutsideTouchable(true);  
  40.           
  41.         //自定义动画  
  42. //      mPopupWindow.setAnimationStyle(R.style.PopupAnimation);  
  43.         //使用系统动画  
  44.         mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);  
  45.         mPopupWindow.update();  
  46.         mPopupWindow.setTouchable(true);  
  47.         mPopupWindow.setFocusable(true);  
  48.           
  49.         btns=new View[3];  
  50.         btns[0]=view.findViewById(R.id.btn_0);  
  51.         btns[1]=view.findViewById(R.id.btn_1);  
  52.         btns[2]=view.findViewById(R.id.btn_2);  
  53.         btns[0].setOnClickListener(new OnClickListener() {  
  54.               
  55.             @Override  
  56.             public void onClick(View v) {  
  57.                 // TODO Auto-generated method stub  
  58.                 //doSomething  
  59.             }  
  60.         });  
  61.         btns[1].setOnClickListener(new OnClickListener() {  
  62.               
  63.             @Override  
  64.             public void onClick(View v) {  
  65.                 // TODO Auto-generated method stub  
  66.                 //doSomething  
  67.             }  
  68.         });  
  69.         btns[2].setOnClickListener(new OnClickListener() {  
  70.               
  71.             @Override  
  72.             public void onClick(View v) {  
  73.                 // TODO Auto-generated method stub  
  74.                 //doSomething  
  75.             }  
  76.         });  
  77.     }  
  78.     private void showPopupWindow(View view) {  
  79.         if(!mPopupWindow.isShowing()){  
  80. //          mPopupWindow.showAsDropDown(view,0,0);  
  81.             mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);  
  82.         }  
  83.     }  
  84.     public Bitmap setRotate(int resId) {  
  85.         Matrix mFgMatrix = new Matrix();  
  86.         Bitmap mFgBitmap = BitmapFactory.decodeResource(getResources(), resId);  
  87.         mFgMatrix.setRotate(180f);  
  88.         return mFgBitmap=Bitmap.createBitmap(mFgBitmap, 0, 0,   
  89.                 mFgBitmap.getWidth(), mFgBitmap.getHeight(), mFgMatrix, true);  
  90.     }  
  91. }  


PopupWindow的布局popwindow.xml 
注意3个LinearLayout里必须设置clickable和background,这样当点击上去的时候才会有点击效果。 
android:clickable="true" 
android:background="@drawable/state_btn_pressed" 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <LinearLayout   
  3.         xmlns:android="http://schemas.android.com/apk/res/android"  
  4.         android:layout_width="fill_parent"  
  5.         android:layout_height="wrap_content"   
  6.         android:orientation="horizontal"  
  7.         android:id="@+id/layout_main"  
  8.         >  
  9.         <LinearLayout android:layout_width="fill_parent"  
  10.             android:layout_height="wrap_content"  
  11.             android:orientation="vertical"  
  12.             android:gravity="center_horizontal"  
  13.             android:clickable="true"  
  14.             android:background="@drawable/state_btn_pressed"  
  15.             android:layout_weight="1"  
  16.             android:id="@+id/btn_0"  
  17.             >  
  18.             <ImageView android:layout_width="wrap_content"  
  19.                 android:layout_height="wrap_content"   
  20.                 android:scaleType="fitCenter"  
  21.                 android:src=http://www.mamicode.com/"@drawable/ic_call"  
  22.                 >  
  23.             </ImageView>  
  24.             <TextView android:layout_width="wrap_content"  
  25.                 android:layout_height="wrap_content"   
  26.                 android:textColor="#000000"  
  27.                 android:textSize="18px"  
  28.                 android:text="电话">  
  29.             </TextView>  
  30.         </LinearLayout>  
  31.         <LinearLayout android:layout_width="fill_parent"  
  32.             android:layout_height="wrap_content"  
  33.             android:orientation="vertical"  
  34.             android:gravity="center_horizontal"  
  35.             android:clickable="true"  
  36.             android:background="@drawable/state_btn_pressed"  
  37.             android:layout_weight="1"  
  38.             android:id="@+id/btn_1"  
  39.             >  
  40.             <ImageView android:layout_width="wrap_content"  
  41.                 android:layout_height="wrap_content"   
  42.                 android:scaleType="fitCenter"  
  43.                 android:src=http://www.mamicode.com/"@drawable/ic_home"  
  44.                 >  
  45.             </ImageView>  
  46.             <TextView android:layout_width="wrap_content"  
  47.                 android:layout_height="wrap_content"   
  48.                 android:textColor="#000"  
  49.                 android:textSize="18px"  
  50.                 android:text="空间">  
  51.             </TextView>  
  52.         </LinearLayout>  
  53.           
  54.         <LinearLayout android:layout_width="fill_parent"  
  55.             android:layout_height="wrap_content"  
  56.             android:orientation="vertical"  
  57.             android:gravity="center_horizontal"  
  58.             android:clickable="true"  
  59.             android:background="@drawable/state_btn_pressed"  
  60.             android:layout_weight="1"  
  61.             android:id="@+id/btn_2"  
  62.             >  
  63.             <ImageView android:layout_width="wrap_content"  
  64.                 android:layout_height="wrap_content"   
  65.                 android:scaleType="fitCenter"  
  66.                 android:src=http://www.mamicode.com/"@drawable/ic_sms"  
  67.                 >  
  68.             </ImageView>  
  69.             <TextView android:layout_width="wrap_content"  
  70.                 android:layout_height="wrap_content"   
  71.                 android:textColor="#000"  
  72.                 android:textSize="18px"  
  73.                 android:text="短信"  
  74.                 >  
  75.             </TextView>  
  76.         </LinearLayout>  
  77. </LinearLayout>  

state_btn_pressed.xml,点击的效果: 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_pressed="true"  
  4.         android:drawable="@drawable/bg_btn_pressed"  
  5.         android:padding="0dp"/>  
  6. </selector>  


Android 模仿迅雷的 PopupWindow 出现/消失动画 
出现: 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <scale android:fromXScale="0.6" android:toXScale="1.1"  
  4.         android:fromYScale="0.6" android:toYScale="1.1" android:pivotX="50%"  
  5.         android:pivotY="50%" android:duration="200" />  
  6.     <scale android:fromXScale="1.0" android:toXScale="0.91"  
  7.         android:fromYScale="1.0" android:toYScale="0.91" android:pivotX="50%"  
  8.         android:pivotY="50%" android:duration="400" android:delay="200" />  
  9.     <alpha android:interpolator="@android:anim/linear_interpolator"  
  10.         android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="400" />  
  11. </set>  

消失: 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <scale android:fromXScale="1.0" android:toXScale="1.25"  
  4.         android:fromYScale="1.0" android:toYScale="1.25" android:pivotX="50%"  
  5.         android:pivotY="50%" android:duration="200" />  
  6.     <scale android:fromXScale="1.0" android:toXScale="0.48"  
  7.         android:fromYScale="1.0" android:toYScale="0.48" android:pivotX="50%"  
  8.         android:pivotY="50%" android:duration="400" android:delay="200" />  
  9.     <alpha android:interpolator="@android:anim/linear_interpolator"  
  10.         android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="400" />  
  11. </set>  

最后用下面的 XML 封装: 
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="PopupAnimation" parent="android:Animation"  
  4.         mce_bogus="1">  
  5.         <item name="android:windowEnterAnimation">@anim/anim_dialog_show</item>  
  6.         <item name="android:windowExitAnimation">@anim/anim_dialog_hide</item>  
  7.     </style>  
  8. </resources>  
  • DraweRoll.rar (111 KB)
  • 下载次数: 806
  • 查看图片附件

自定义PopupWindow动画效果