android 中layout 的大小缩放

1.首相要创建一个activity 代码如下:

package com.wljie.layout.z;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class ZlayoutTestActivity extends Activity {
	
	 public RelativeLayout layout_parent;  
     
	 public Button scale_btn;
	 
	 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        scale_btn = (Button) findViewById(R.id.scale_btn);  
        scale_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 switch (v.getId()) {  
			        case R.id.scale_btn:  
			            displayPage();  
			            v.setEnabled(false);  
			            break;  
			        case R.id.dismiss_btn:  
			            dismissPage();  
			            break;  
			        }  
			}
		}); 
  
        layout_parent = (RelativeLayout) findViewById(R.id.layout_parent);  
    }
    
    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.icon);  
        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(new OnClickListener() {
			
				public void onClick(View v) {
					 switch (v.getId()) {  
				        case R.id.scale_btn:  
				            displayPage();  
				            v.setEnabled(false);  
				            break;  
				        case R.id.dismiss_btn:  
				            dismissPage();  
				            break;  
				        }  
				}
				
		})  ;
    }  
  
    public void dismissPage() {  
        ScaleAnimationHelper scaleHelper = new ScaleAnimationHelper(this,  
                Constant.KEY_SECONDPAGE);  
        scaleHelper.ScaleInAnimation(layout);  
        scale_btn.setEnabled(true);  
    }  
}

2.然后创建一个创建类并实现AnimationListener接口 这个接口大家不会陌生吧。是动画效果的接口。

package com.wljie.layout.z;

import android.app.Activity;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

public class DisplayNextView implements 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:  
                ((ZlayoutTestActivity) ac).removeLayout();  
                break;  
            }  
        }  
    }  
  
    public void doSomethingOnEnd(int _order) {  
        switch (_order) {  
          
        case Constant.KEY_SECONDPAGE:  
            ((ZlayoutTestActivity) ac).layout_parent.post(new SwapViews());  
            break;  
        }  
    }  
}  

3. 定义常量的类:

package com.wljie.layout.z;

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

4.将动画单独做一个类package com.wljie.layout.z;

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((ZlayoutTestActivity) 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);  
    }  
}

最后还有两个xml :

main.xml

<?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>  


second.xml

<?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/icon" android:layout_alignParentBottom="true"  
        android:id="@+id/dismiss_btn" />  
</RelativeLayout>  


本文转自:http://edison-cool911.iteye.com/blog/704812

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值