一个Layout从中心放大和缩小的例子,直接上代码: 
1.ScaleDialog.java文件 
Java代码  
  1. package cn.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.Drawable;  
  5. import android.os.Bundle;  
  6. import android.widget.Button;  
  7. import android.widget.ImageView;  
  8. import android.widget.LinearLayout;  
  9. import android.widget.RelativeLayout;  
  10. import android.view.LayoutInflater;  
  11. import android.view.View;  
  12. import android.view.View.OnClickListener;  
  13.   
  14. public class ScaleDialog extends Activity implements OnClickListener {  
  15.   
  16.     RelativeLayout layout_parent;  
  17.       
  18.     Button scale_btn;  
  19.   
  20.     /** Called when the activity is first created. */  
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.first);  
  25.   
  26.         scale_btn = (Button) findViewById(R.id.scale_btn);  
  27.         scale_btn.setOnClickListener(this);  
  28.   
  29.         layout_parent = (RelativeLayout) findViewById(R.id.layout_parent);  
  30.     }  
  31.   
  32.     @Override  
  33.     public void onClick(View v) {  
  34.         // TODO Auto-generated method stub  
  35.         switch (v.getId()) {  
  36.         case R.id.scale_btn:  
  37.             displayPage();  
  38.             v.setEnabled(false);  
  39.             break;  
  40.         case R.id.dismiss_btn:  
  41.             dismissPage();  
  42.             break;  
  43.         }  
  44.   
  45.     }  
  46.   
  47.     View layout;  
  48.     ImageView jobShadow;  
  49.   
  50.     public void displayPage() {  
  51.         LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
  52.         layout = inflater.inflate(R.layout.second, null);  
  53.         layout.setId(Constant.KEY_LAYOUT_ID);  
  54.         jobShadow = (ImageView) layout.findViewById(R.id.jobShadow);  
  55.   
  56.         Drawable ico = getResources().getDrawable(R.drawable.dbg);  
  57.         jobShadow.setBackgroundDrawable(ico);  
  58.         ico.mutate().setAlpha(200);  
  59.   
  60.         LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  
  61.                 LinearLayout.LayoutParams.FILL_PARENT,  
  62.                 LinearLayout.LayoutParams.FILL_PARENT);  
  63.         layout_parent.addView(layout, layoutParams);  
  64.   
  65.         findDialogView();  
  66.   
  67.         // 显示layout进行缩放动画效果  
  68.         ScaleAnimationHelper scaleHelper = new ScaleAnimationHelper(this,  
  69.                 Constant.KEY_FIRSTPAGE);  
  70.         scaleHelper.ScaleOutAnimation(layout);  
  71.     }  
  72.   
  73.     public void removeLayout() {  
  74.   
  75.         layout_parent.removeView(layout_parent  
  76.                 .findViewById(Constant.KEY_LAYOUT_ID));  
  77.     }  
  78.   
  79.     Button dismiss_btn;  
  80.   
  81.     public void findDialogView() {  
  82.         dismiss_btn = (Button) findViewById(R.id.dismiss_btn);  
  83.         dismiss_btn.setOnClickListener(this);  
  84.     }  
  85.   
  86.     public void dismissPage() {  
  87.         ScaleAnimationHelper scaleHelper = new ScaleAnimationHelper(this,  
  88.                 Constant.KEY_SECONDPAGE);  
  89.         scaleHelper.ScaleInAnimation(layout);  
  90.         scale_btn.setEnabled(true);  
  91.     }  
  92. }  

2. ScaleAnimationHelper.java的辅助类 
Java代码  
  1. package cn.com;  
  2.   
  3. import android.content.Context;  
  4. import android.view.View;  
  5. import android.view.animation.AccelerateInterpolator;  
  6. import android.view.animation.Animation;  
  7. import android.view.animation.AnimationSet;  
  8. import android.view.animation.ScaleAnimation;  
  9.   
  10. public class ScaleAnimationHelper {  
  11.     Context con;  
  12.     int order;  
  13.   
  14.     public ScaleAnimationHelper(Context con, int order) {  
  15.         this.con = con;  
  16.         this.order = order;  
  17.     }  
  18.   
  19.     DisplayNextView listener;  
  20.     ScaleAnimation myAnimation_Scale;  
  21.         //放大的类,不需要设置监听器  
  22.     public void ScaleOutAnimation(View view) {  
  23.         myAnimation_Scale = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1f,  
  24.                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,  
  25.                 0.5f);  
  26.         myAnimation_Scale.setInterpolator(new AccelerateInterpolator());  
  27.         AnimationSet aa = new AnimationSet(true);  
  28.         aa.addAnimation(myAnimation_Scale);  
  29.         aa.setDuration(500);  
  30.   
  31.         view.startAnimation(aa);  
  32.     }  
  33.   
  34.     public void ScaleInAnimation(View view) {  
  35.         listener = new DisplayNextView((ScaleDialog) con, order);  
  36.         myAnimation_Scale = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,  
  37.                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,  
  38.                 0.5f);  
  39.         myAnimation_Scale.setInterpolator(new AccelerateInterpolator());  
  40.                //缩小Layout的类,在动画结束需要从父View移除它  
  41.         myAnimation_Scale.setAnimationListener(listener);  
  42.         AnimationSet aa = new AnimationSet(true);  
  43.         aa.addAnimation(myAnimation_Scale);  
  44.         aa.setDuration(500);  
  45.   
  46.         view.startAnimation(aa);  
  47.     }  
  48. }  

3. DisplayNextView.java动画结束的监听类 
Java代码  
  1. package cn.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.view.animation.Animation;  
  5.   
  6. public class DisplayNextView implements Animation.AnimationListener {  
  7.   
  8.     Object obj;  
  9.   
  10.     // 动画监听器的构造函数  
  11.     Activity ac;  
  12.     int order;  
  13.   
  14.     public DisplayNextView(Activity ac, int order) {  
  15.         this.ac = ac;  
  16.         this.order = order;  
  17.     }  
  18.   
  19.     public void onAnimationStart(Animation animation) {  
  20.     }  
  21.   
  22.     public void onAnimationEnd(Animation animation) {  
  23.         doSomethingOnEnd(order);  
  24.     }  
  25.   
  26.     public void onAnimationRepeat(Animation animation) {  
  27.     }  
  28.   
  29.     private final class SwapViews implements Runnable {  
  30.         public void run() {  
  31.             switch (order) {  
  32.             case Constant.KEY_SECONDPAGE:  
  33.                 ((ScaleDialog) ac).removeLayout();  
  34.                 break;  
  35.             }  
  36.         }  
  37.     }  
  38.   
  39.     public void doSomethingOnEnd(int _order) {  
  40.         switch (_order) {  
  41.           
  42.         case Constant.KEY_SECONDPAGE:  
  43.             ((ScaleDialog) ac).layout_parent.post(new SwapViews());  
  44.             break;  
  45.         }  
  46.     }  
  47. }  


4. Constant.java标识ID常量的类 
Java代码  
  1. package cn.com;  
  2.   
  3. public class Constant {  
  4.   
  5.     public final static int KEY_FIRSTPAGE = 1;  
  6.       
  7.     public final static int KEY_SECONDPAGE = 2;  
  8.       
  9.     public final static int KEY_LAYOUT_ID = 3;  
  10. }  


5.first.xml文件 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" android:id="@+id/layout_parent">  
  5.     <Button android:text="Scale" android:id="@+id/scale_btn"  
  6.         android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>  
  7. </RelativeLayout>  


6.second.xml文件 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  4.     android:id="@+id/jobContent">  
  5.     <ImageView android:layout_width="fill_parent"  
  6.         android:layout_height="fill_parent" android:id="@+id/jobShadow" />  
  7.     <Button android:layout_width="fill_parent" android:text="Dismiss"  
  8.         android:layout_marginTop="20dp" android:layout_height="wrap_content"  
  9.         android:src="@drawable/jobbg" android:layout_alignParentBottom="true"  
  10.         android:id="@+id/dismiss_btn" />  
  11. </RelativeLayout>  

posted on 2011-04-15 15:47  kitea  阅读(3848)  评论(1)    收藏  举报