Layout的放大和缩小效果例子(ScaleAnimation)

个Layout从中心放大和缩小的例子,直接上代码: 
1.ScaleDialog.java文件 
Java代码  
package cn.com;  
  
import android.app.Activity;  
import android.graphics.drawable.Drawable;  
import android.os.Bundle;  
import android.widget.Button;  
import android.widget.ImageView;  
import android.widget.LinearLayout;  
import android.widget.RelativeLayout;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.View.OnClickListener;  
  
public class ScaleDialog extends Activity implements OnClickListener {  
  
    RelativeLayout layout_parent;  
      
    Button scale_btn;  
  
    /** Called when the activity is first created. */  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.first);  
  
        scale_btn = (Button) findViewById(R.id.scale_btn);  
        scale_btn.setOnClickListener(this);  
  
        layout_parent = (RelativeLayout) findViewById(R.id.layout_parent);  
    }  
  
    @Override  
    public void onClick(View v) {  
        // TODO Auto-generated method stub  
        switch (v.getId()) {  
        case R.id.scale_btn:  
            displayPage();  
            v.setEnabled(false);  
            break;  
        case R.id.dismiss_btn:  
            dismissPage();  
            break;  
        }  
  
    }  
  
    View layout;  
    ImageView jobShadow;  
  
    public void displayPage() {  
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
        layout = inflater.inflate(R.layout.second, null);  
        layout.setId(Constant.KEY_LAYOUT_ID);  
        jobShadow = (ImageView) layout.findViewById(R.id.jobShadow);  
  
        Drawable ico = getResources().getDrawable(R.drawable.dbg);  
        jobShadow.setBackgroundDrawable(ico);  
        ico.mutate().setAlpha(200);  
  
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(  
                LinearLayout.LayoutParams.FILL_PARENT,  
                LinearLayout.LayoutParams.FILL_PARENT);  
        layout_parent.addView(layout, layoutParams);  
  
        findDialogView();  
  
        // 显示layout进行缩放动画效果  
        ScaleAnimationHelper scaleHelper = new ScaleAnimationHelper(this,  
                Constant.KEY_FIRSTPAGE);  
        scaleHelper.ScaleOutAnimation(layout);  
    }  
  
    public void removeLayout() {  
  
        layout_parent.removeView(layout_parent  
                .findViewById(Constant.KEY_LAYOUT_ID));  
    }  
  
    Button dismiss_btn;  
  
    public void findDialogView() {  
        dismiss_btn = (Button) findViewById(R.id.dismiss_btn);  
        dismiss_btn.setOnClickListener(this);  
    }  
  
    public void dismissPage() {  
        ScaleAnimationHelper scaleHelper = new ScaleAnimationHelper(this,  
                Constant.KEY_SECONDPAGE);  
        scaleHelper.ScaleInAnimation(layout);  
        scale_btn.setEnabled(true);  
    }  
}  
2. ScaleAnimationHelper.java的辅助类 
Java代码  
package cn.com;  
  
import android.content.Context;  
import android.view.View;  
import android.view.animation.AccelerateInterpolator;  
import android.view.animation.Animation;  
import android.view.animation.AnimationSet;  
import android.view.animation.ScaleAnimation;  
  
public class ScaleAnimationHelper {  
    Context con;  
    int order;  
  
    public ScaleAnimationHelper(Context con, int order) {  
        this.con = con;  
        this.order = order;  
    }  
  
    DisplayNextView listener;  
    ScaleAnimation myAnimation_Scale;  
        //放大的类,不需要设置监听器  
    public void ScaleOutAnimation(View view) {  
        myAnimation_Scale = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1f,  
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,  
                0.5f);  
        myAnimation_Scale.setInterpolator(new AccelerateInterpolator());  
        AnimationSet aa = new AnimationSet(true);  
        aa.addAnimation(myAnimation_Scale);  
        aa.setDuration(500);  
  
        view.startAnimation(aa);  
    }  
  
    public void ScaleInAnimation(View view) {  
        listener = new DisplayNextView((ScaleDialog) con, order);  
        myAnimation_Scale = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f,  
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,  
                0.5f);  
        myAnimation_Scale.setInterpolator(new AccelerateInterpolator());  
               //缩小Layout的类,在动画结束需要从父View移除它  
        myAnimation_Scale.setAnimationListener(listener);  
        AnimationSet aa = new AnimationSet(true);  
        aa.addAnimation(myAnimation_Scale);  
        aa.setDuration(500);  
  
        view.startAnimation(aa);  
    }  
}  
3. DisplayNextView.java动画结束的监听类 
Java代码  
package cn.com;  
  
import android.app.Activity;  
import android.view.animation.Animation;  
  
public class DisplayNextView implements Animation.AnimationListener {  
  
    Object obj;  
  
    // 动画监听器的构造函数  
    Activity ac;  
    int order;  
  
    public DisplayNextView(Activity ac, int order) {  
        this.ac = ac;  
        this.order = order;  
    }  
  
    public void onAnimationStart(Animation animation) {  
    }  
  
    public void onAnimationEnd(Animation animation) {  
        doSomethingOnEnd(order);  
    }  
  
    public void onAnimationRepeat(Animation animation) {  
    }  
  
    private final class SwapViews implements Runnable {  
        public void run() {  
            switch (order) {  
            case Constant.KEY_SECONDPAGE:  
                ((ScaleDialog) ac).removeLayout();  
                break;  
            }  
        }  
    }  
  
    public void doSomethingOnEnd(int _order) {  
        switch (_order) {  
          
        case Constant.KEY_SECONDPAGE:  
            ((ScaleDialog) ac).layout_parent.post(new SwapViews());  
            break;  
        }  
    }  
}  
4. Constant.java标识ID常量的类 
Java代码  
package cn.com;  
  
public class Constant {  
  
    public final static int KEY_FIRSTPAGE = 1;  
      
    public final static int KEY_SECONDPAGE = 2;  
      
    public final static int KEY_LAYOUT_ID = 3;  
}  
5.first.xml文件 
Java代码  
  
    android:orientation="vertical" android:layout_width="fill_parent"  
    android:layout_height="fill_parent" android:id="@+id/layout_parent">  
            android:layout_width="fill_parent" android:layout_height="wrap_content">  
  
6.second.xml文件 
Java代码  
  
    android:layout_width="fill_parent" android:layout_height="fill_parent"  
    android:id="@+id/jobContent">  
            android:layout_height="fill_parent" android:id="@+id/jobShadow" />  
            android:layout_marginTop="20dp" android:layout_height="wrap_content"  
        android:src="@drawable/jobbg" android:layout_alignParentBottom="true"  
        android:id="@+id/dismiss_btn" />  
  

if ($ != jQuery) { $ = jQuery.noConflict(); } var isLogined = false; var cb_blogId = 79334; var cb_entryId = 2017259; var cb_blogApp = "foura"; var cb_blogUserGuid = "f06ec66b-5d6c-df11-ba8f-001cf0cd104b"; var cb_entryCreatedDate = '2011/4/15 15:47:00';

你可能感兴趣的:(Android基础)