animation的基本實現——Scale、Rotate、alpha、translate

 效果圖

 

 

一、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.RotateAnimation;
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
   AnimationSet animationSet = new AnimationSet(true);
   ScaleAnimation scaleAnimation = new ScaleAnimation(1,2f,1,2f,
     Animation.RELATIVE_TO_SELF,0.5f,   //相對於自身縮放到自身的哪個點。
     Animation.RELATIVE_TO_SELF,0.5f);
   scaleAnimation.setDuration(1000);
   animationSet.addAnimation(scaleAnimation);
   animationSet.setFillAfter(true); //讓其保持動畫結束時的狀態。
   imageView.startAnimation(animationSet);
   
   
  }
     
    }
    private class RotateListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   AnimationSet animationSet = new AnimationSet(true);
   RotateAnimation rotateAnimation = new RotateAnimation(0,360,
     Animation.RELATIVE_TO_PARENT,1f,
     Animation.RELATIVE_TO_PARENT,0f);
   rotateAnimation.setDuration(1000);
   animationSet.addAnimation(rotateAnimation);
   imageView.startAnimation(animationSet);
  }
     
    }
    private class AlphaListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   AnimationSet animationSet = new AnimationSet(true);
   AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
   alphaAnimation.setDuration(1000);
   animationSet.addAnimation(alphaAnimation);
   imageView.startAnimation(animationSet);
  }
     
    }
    private class TranslateListener implements OnClickListener{

  @Override
  public void onClick(View v) {
   // 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,1f);
   TranslateAnimation.setDuration(1000);
   animationSet.addAnimation(TranslateAnimation);
   imageView.startAnimation(animationSet);
   
  }
     
    }
}

二、 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="測試rotate動畫效果" />
 <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>

 

發佈了55 篇原創文章 · 獲贊 28 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章