Scale的放大与缩小

来源:互联网 发布:知乎 椅子推荐 编辑:程序博客网 时间:2024/05/21 09:38

一、activity代码

package com.liudan.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationActivity extends Activity {
 
    private Button scale,rotate,alpha,translate;
    private ImageView imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        scale = (Button)findViewById(R.id.scale);
        rotate = (Button)findViewById(R.id.rotate);
        alpha = (Button)findViewById(R.id.alpha);
        translate = (Button)findViewById(R.id.translate);
        imageView = (ImageView)findViewById(R.id.imageView);
        
        scale.setOnClickListener(new ScaleListener());
        rotate.setOnClickListener(new RotateListener());
        alpha.setOnClickListener(new AlphaListener());
        translate.setOnClickListener(new TranslateListener());
        
    }
    private class ScaleListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   Animation animation = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.scale);
   animation.setFillAfter(true);
   imageView.startAnimation(animation);
   
   
  }
     
    }
    private class RotateListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   Animation animation = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.scale2);

   imageView.startAnimation(animation);
  }
     
    }
    private class AlphaListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   Animation animation = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.alpha);
   imageView.startAnimation(animation);
  }
     
    }
    private class TranslateListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   Animation animation = AnimationUtils.loadAnimation(AnimationActivity.this, R.anim.translate);

   imageView.startAnimation(animation);
   
  }
     
    }
}

 

二、xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <Button android:id="@+id/scale" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="测试scale动画效果" />
 <Button android:id="@+id/rotate" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="测试scale放大之后再缩小的动画效果" />
 <Button android:id="@+id/alpha" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="测试alpha动画效果" />
 <Button android:id="@+id/translate" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="测试translate动画效果" />
 <ImageView android:id="@+id/imageView"  android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:src="@drawable/icon" />

</LinearLayout>

 

三、 动画的xml代码

在res目录下新建anim文件夹,在anim下新建 alpha.xml  scale.xml scale2.xml rotate.xml tanslate.xml

1.  scale.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
<scale
 android:fromXScale="1.0"
 android:toXScale="2.0"
 android:fromYScale="1.0"
 android:toYScale="2.0"
 android:pivotX="50%"
 android:pivotY="50%"
 android:duration="1000"
 
 
 />

</set>

2.  scale2.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
<scale
 android:fromXScale="2.0"
 android:toXScale="1.0"
 android:fromYScale="2.0"
 android:toYScale="1.0"
 android:pivotX="50%"
 android:pivotY="50%"
 android:duration="1000"
 
 
 />

</set>

3.  alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
<alpha
 android:fromAlpha="1.0"
 android:toAlpha="0.0"
 android:duration="500"
 />

</set>

4. rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
<rotate
 android:fromDegrees="0"
 android:toDegrees="+350"
 android:pivotX="50%"
 android:pivotY="50%"
 android:duration="500"/>

</set>

5. tanslate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/accelerate_interpolator">
<translate
 android:fromXDelta="50%"
 android:toXDelta="100%"
 android:fromYDelta="0%"
 android:toYDelta="100%"
 android:duration="500"/>

</set>

0 0
原创粉丝点击