Android 的四中动画效果

jopen 10年前

初步讲解android的淡进淡出、旋转、移动、透明度的问题

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:paddingBottom="@dimen/activity_vertical_margin"      android:paddingLeft="@dimen/activity_horizontal_margin"      android:paddingRight="@dimen/activity_horizontal_margin"      android:paddingTop="@dimen/activity_vertical_margin"      tools:context=".MainActivity" >      <Button          android:id="@+id/button1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignParentBottom="true"          android:layout_centerHorizontal="true"          android:text="Button" />      <Button          android:id="@+id/button2"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_above="@+id/button1"          android:layout_alignLeft="@+id/button1"          android:layout_marginBottom="20dp"          android:text="Button" />      <Button          android:id="@+id/button3"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_above="@+id/button2"          android:layout_alignLeft="@+id/button2"          android:layout_marginBottom="16dp"          android:text="Button" />      <Button          android:id="@+id/button4"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_above="@+id/button3"          android:layout_alignRight="@+id/button3"          android:layout_marginBottom="38dp"          android:text="Button" />      <ImageView          android:id="@+id/imageView1"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:layout_alignLeft="@+id/button4"          android:layout_alignParentTop="true"          android:layout_marginTop="26dp"          android:src="@drawable/ic_launcher" />  </RelativeLayout>

这是布局文件

package com.example.move;  import android.os.Bundle;  import android.app.Activity;  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.RotateAnimation;  import android.view.animation.ScaleAnimation;  import android.view.animation.TranslateAnimation;  import android.widget.Button;  import android.widget.ImageView;  public class MainActivity extends Activity {   private Button alphaBut;//淡静淡出   private ImageView imageView;   private Button rotateBut;//旋转   private Button scaleBut;//缩小也可以放大   private Button tranBut;//移动      @Override   protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    alphaBut=(Button) findViewById(R.id.button1);    rotateBut=(Button) findViewById(R.id.button2);    scaleBut=(Button) findViewById(R.id.button3);    tranBut=(Button) findViewById(R.id.button4);    imageView=(ImageView) findViewById(R.id.imageView1);    alphaBut.setOnClickListener(new AlphaButoon());    rotateBut.setOnClickListener(new RotateButoon());    scaleBut.setOnClickListener(new ScaleButoon());    tranBut.setOnClickListener(new TranButoon());   }   class TranButoon implements OnClickListener{    @Override    public void onClick(View arg0) {     AnimationSet animationSet=new AnimationSet(true);     Animation animation=new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);     animation.setDuration(2000);     animationSet.addAnimation(animation);     imageView.startAnimation(animationSet);     tranBut.startAnimation(animationSet);    }       }   class ScaleButoon implements OnClickListener{        @Override    public void onClick(View arg0) {     AnimationSet animationSet=new AnimationSet(true);     Animation animation=new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);     animation.setDuration(2000);     animationSet.addAnimation(animation);     imageView.startAnimation(animationSet);     scaleBut.startAnimation(animationSet);    }       }   class AlphaButoon implements OnClickListener{        @Override    public void onClick(View arg0) {     AnimationSet animationSet=new AnimationSet(true);     Animation animation=new AlphaAnimation(0, 1);     animation.setDuration(2000);     animationSet.addAnimation(animation);     imageView.startAnimation(animationSet);     alphaBut.startAnimation(animationSet);    }       }      class RotateButoon implements OnClickListener{    @Override    public void onClick(View arg0) {     AnimationSet animationSet=new AnimationSet(true);     //0-360从那个角度转到那个角度,X轴的参考值,0,5f百分之50,Y轴的参考值,0,5f百分之50     Animation rotate=new RotateAnimation(0, 360,Animation.RELATIVE_TO_PARENT,0.5f,Animation.RELATIVE_TO_PARENT,0.5f);     Animation alpha=new AlphaAnimation(0, 1);     rotate.setDuration(2000);     animationSet.addAnimation(rotate);     alpha.setDuration(100);     animationSet.addAnimation(alpha);     imageView.startAnimation(animationSet);     rotateBut.startAnimation(animationSet);    }   }  }

主函数