1. 程式人生 > >Layout的放大和縮小效果例子(ScaleAnimation)

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程式碼  
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical" android:layout_width="fill_parent"  
    android:layout_height="fill_parent" android:id="@+id/layout_parent">  
    <Button android:text="Scale" android:id="@+id/scale_btn"  
        android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>  
</RelativeLayout>  
6.second.xml檔案 
Java程式碼  
<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent" android:layout_height="fill_parent"  
    android:id="@+id/jobContent">  
    <ImageView android:layout_width="fill_parent"  
        android:layout_height="fill_parent" android:id="@+id/jobShadow" />  
    <Button android:layout_width="fill_parent" android:text="Dismiss"  
        android:layout_marginTop="20dp" android:layout_height="wrap_content"  
        android:src="@drawable/jobbg" android:layout_alignParentBottom="true"  
        android:id="@+id/dismiss_btn" />  
</RelativeLayout>  

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';