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

一个Layout从中心放大和缩小的例子,直接上代码:
1.ScaleDialog.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的辅助类

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动画结束的监听类

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常量的类

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文件

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

<?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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值