AnimationTest

来源:互联网 发布:容祖儿密友知乎 编辑:程序博客网 时间:2024/05/16 04:39

package com.ihandy.cn;

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

public class AnimationTest extends Activity {

 private ImageView image;
 private Button btn01,btn02,btn03,btn04,btn05;

 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.animation_test);
  image=(ImageView)findViewById(R.id.anim_test_img);
  image.setImageResource(R.drawable.img_06);
  btn01=(Button)findViewById(R.id.anim_test_btn01);
  btn02=(Button)findViewById(R.id.anim_test_btn02);
  btn03=(Button)findViewById(R.id.anim_test_btn03);
  btn04=(Button)findViewById(R.id.anim_test_btn04);
  btn05=(Button)findViewById(R.id.anim_test_btn05);
  
  btn01.setOnClickListener(new OnClickListener()
  {
   public void onClick(View v)
   {
    // TODO Auto-generated method stub
    //通过资源文件定义动画
//    Animation anim=AnimationUtils.loadAnimation(AnimationTest.this,
//                                                R.anim.alpha_anim);
//    image.startAnimation(anim);
    
    //使用Java代码定义动画
    Animation anim2=new AlphaAnimation(0.1f,1.0f);
    anim2.setDuration(5000);
    image.startAnimation(anim2);
   }
  });
  
  btn02.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v)
   {
    //通过资源文件定义动画
//    Animation anim=AnimationUtils.loadAnimation(AnimationTest.this,
//                                                R.anim.scale_anim);
//    image.startAnimation(anim);
    
    //使用Java代码定义动画
    Animation anim2=new ScaleAnimation(0.0f,1.0f,0.0f,1.0f);
    anim2.setDuration(5000);
    anim2.setRepeatCount(2);
    image.startAnimation(anim2);
   }
  });
  
  btn03.setOnClickListener(new OnClickListener(){
   @Override
   public void onClick(View v)
   {
    //通过资源文件定义动画
//    Animation anim=AnimationUtils.loadAnimation(AnimationTest.this,
//                                              R.anim.translate_anim);
//    image.startAnimation(anim);
    
    //使用Java代码定义动画
    Animation anim2=new TranslateAnimation(0,400,0,0);
    anim2.setDuration(5000);
    anim2.setInterpolator(new BounceInterpolator());//设置弹跳效果
    image.startAnimation(anim2);
   }
  });
  
  btn04.setOnClickListener(new OnClickListener()
  {
   public void onClick(View v)
   {
    // TODO Auto-generated method stub
    //通过资源文件定义动画
    Animation anim=AnimationUtils.loadAnimation(AnimationTest.this,
                                          R.anim.rotate_anim);
    image.startAnimation(anim);
    
    //使用Java代码定义动画
//    Animation anim2=new TranslateAnimation(0,400,0,0);
//    anim2.setDuration(5000);
//    anim2.setInterpolator(new BounceInterpolator());
//    image.startAnimation(anim2);
   }
  });
  
  //设置组合动画,将前四种动画效果随机组合演示
  btn05.setOnClickListener(new OnClickListener()
  {
   public void onClick(View v)
   {
    //通过资源文件定义动画
    Animation anim=AnimationUtils.loadAnimation(AnimationTest.this,
                                          R.anim.group_anim);
    image.startAnimation(anim);
   }
  });
 }

 @Override
 public boolean onTouchEvent(MotionEvent event) {
  // TODO Auto-generated method stub
  
  switch(event.getAction()){
  case MotionEvent.ACTION_DOWN:
  case MotionEvent.ACTION_UP:
   startActivity(new Intent(this,FrameAnimationTest.class));
  }
  return false;
 }
 

}

 

1>在res目录下要创建anim文件夹  在这个文件夹下要创建如下xml :alpha_anim.xml,group_anim.xml,rotate_anim.xml,scale_anim.xml,translate_anim.xml。

2>四个xml文件对应的代码如下

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <alpha
    android:fromAlpha="0.1"
  android:toAlpha="1.0"
  android:duration="5000"
  />
</set>

 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 
 <translate
  android:fromXDelta="0"
  android:fromYDelta="0"
  android:toXDelta="400"
  android:toYDelta="0"
  android:duration="6000"
 />
  <rotate
 android:fromDegrees="0"
 android:toDegrees="+359"
 android:pivotX="50%"
 android:pivotY="50%"
 android:duration="2000"
 android:repeatCount="2"
 />
 
</set>


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 
 <translate
  android:fromXDelta="0"
  android:fromYDelta="0"
  android:toXDelta="400"
  android:toYDelta="0"
  android:duration="6000"
 />
  <rotate
 android:fromDegrees="0"
 android:toDegrees="+359"
 android:pivotX="50%"
 android:pivotY="50%"
 android:duration="2000"
 android:repeatCount="2"
 />
 
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 
 <translate
  android:fromXDelta="0"
  android:fromYDelta="0"
  android:toXDelta="400"
  android:toYDelta="0"
  android:duration="6000"
 />
  <rotate
 android:fromDegrees="0"
 android:toDegrees="+359"
 android:pivotX="50%"
 android:pivotY="50%"
 android:duration="2000"
 android:repeatCount="2"
 />
 
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 <rotate
 android:fromDegrees="0"
 android:toDegrees="+360"
 android:pivotX="50%"
 android:pivotY="50%"
 android:duration="2000"
 android:repeatCount="2"
 />
</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 <scale
  android:fromXScale="0.0"
  android:fromYScale="0.0"
  android:toXScale="1.0"
  android:toYScale="1.0"
  android:pivotX="50%"
  android:pivotY="50%"
  android:repeatCount="2"
  android:duration="2000"  
 />
</set>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 <translate
  android:interpolator="@android:anim/accelerate_interpolator"
  android:fromXDelta="0"
  android:fromYDelta="0"
  android:toXDelta="400"
  android:toYDelta="0"
  android:repeatCount="2"
  android:duration="5000"
 />
 <!-- 第一行表示加速效果:速度渐变,要减速效果只需将accelerate改为decelerate -->
 <!-- 上面2到5行的意思是定义移动的起始和结束点的x坐标和y坐标 -->
</set>

3>自己在准备一张图片放在res的drawable文件夹下,名字和上面定义的致就行。


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <alpha
    android:fromAlpha="0.1"
  android:toAlpha="1.0"
  android:duration="5000"
  />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
  <alpha
    android:fromAlpha="0.1"
  android:toAlpha="1.0"
  android:duration="5000"
  />
</set>
4>这个activity用到的布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ImageView
   android:layout_gravity="center_vertical"
    android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:id="@+id/anim_test_img"
  />
  <Button
   android:layout_marginTop="50.0dp"
   android:layout_marginLeft="100.0dp"
    android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Alpha Animation"
   android:id="@+id/anim_test_btn01"
  />
  <Button
   android:layout_marginTop="50.0dp"
   android:layout_marginLeft="100.0dp"
    android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Scale Animation"
   android:id="@+id/anim_test_btn02"
  />
  <Button
   android:layout_marginTop="50.0dp"
   android:layout_marginLeft="100.0dp"
    android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="TranslateAnimation"
   android:id="@+id/anim_test_btn03"
  />
  <Button
   android:layout_marginTop="50.0dp"
   android:layout_marginLeft="100.0dp"
    android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Rotate Animation"
   android:id="@+id/anim_test_btn04"
  />
   <Button
    android:layout_marginTop="50.0dp"
    android:layout_marginLeft="100.0dp"
    android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Group \n Animation_1"
   android:id="@+id/anim_test_btn05"
  />
</LinearLayout>

 

原创粉丝点击