AnimationAlpha.java
package com.example.animationAlpha; 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.RotateAnimation; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.ImageView; public class animationAlpha extends Activity { /** Called when the activity is first created. */ private ImageView imageView = null; private Button translateButtonId=null; private Button scaleButtonId=null; private Button alphaButtonId=null; private Button rotateButtonId=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView)findViewById(R.id.imageViewId); translateButtonId = (Button)findViewById(R.id.translateButtonId); translateButtonId.setOnClickListener(new translateOnClickListener()); scaleButtonId = (Button)findViewById(R.id.scaleButtonId); scaleButtonId.setOnClickListener(new scaleOnClickListener()); alphaButtonId = (Button)findViewById(R.id.alphaButtonId); alphaButtonId.setOnClickListener(new alphaOnClickListener()); rotateButtonId = (Button)findViewById(R.id.rotateButtonId); rotateButtonId.setOnClickListener(new rotateOnClickListener()); } class translateOnClickListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub AnimationSet animationSet = new AnimationSet(true); TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,3f); translateAnimation.setDuration(2000); animationSet.addAnimation(translateAnimation); imageView.startAnimation(animationSet); } } class scaleOnClickListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub AnimationSet animationSet = new AnimationSet(true); ScaleAnimation scaleAnimation = new ScaleAnimation( 1,0.1f,1,0.1f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); scaleAnimation.setDuration(2000); animationSet.addAnimation(scaleAnimation); imageView.startAnimation(animationSet); } } class alphaOnClickListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub AnimationSet animationSet = new AnimationSet(true); AlphaAnimation alphaAnimation = new AlphaAnimation(1,0); alphaAnimation.setDuration(2000); animationSet.addAnimation(alphaAnimation); imageView.startAnimation(animationSet); } } class rotateOnClickListener implements OnClickListener{ @Override public void onClick(View arg0) { // TODO Auto-generated method stub AnimationSet animationSet = new AnimationSet(true); RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT,1, Animation.RELATIVE_TO_PARENT,0); rotateAnimation.setDuration(2000); animationSet.addAnimation(rotateAnimation); imageView.startAnimation(animationSet); } } }
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" > <Button android:id="@+id/scaleButtonId" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="scale按钮" /> <Button android:id="@+id/rotateButtonId" android:layout_above="@id/scaleButtonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="rotateButton按钮" /> <Button android:id="@+id/alphaButtonId" android:layout_above="@id/rotateButtonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="alpha按钮" /> <Button android:id="@+id/translateButtonId" android:layout_above="@id/alphaButtonId" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="translateButton按钮" /> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/imageViewId" android:layout_centerInParent="true" android:layout_marginTop="100dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"/> </LinearLayout> </RelativeLayout>