格式化

廣告

廣告2

2012年8月22日 星期三

[Android程式範例] 製作電視關閉的特效動畫Animation

Android程式教學-製作電視關閉的特效動畫Animation



先新增一個java類別-TVOffAnimation


import android.graphics.Matrix;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * @author Roger Zhang
 */

public class TVOffAnimation extends Animation {

 private int halfWidth;

 private int halfHeight;

 @Override
 public void initialize(int width, int height, int parentWidth,
   int parentHeight) {

  super.initialize(width, height, parentWidth, parentHeight);
  setDuration(500);
  setFillAfter(true);
  //保存View的中心點
  halfWidth = width / 2;
  halfHeight = height / 2;
  setInterpolator(new AccelerateDecelerateInterpolator());
  
 }

 @Override
 protected void applyTransformation(float interpolatedTime, Transformation t) {

  final Matrix matrix = t.getMatrix();
  if (interpolatedTime < 0.8) {
   matrix.preScale(1+0.625f*interpolatedTime, 1-interpolatedTime/0.8f+0.01f,halfWidth,halfHeight);
  }else{
   matrix.preScale(7.5f*(1-interpolatedTime),0.01f,halfWidth,halfHeight);
  }
 }
}

主程式只要新增需要套用此動畫的圖片實體便可
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TVOffDemo extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  Button b = (Button) findViewById(R.id.Button01);
  b.setOnClickListener(new OnClickListener() {

   public void onClick(View v) {
    View img = findViewById(R.id.ImageView01);
    img.startAnimation(new TVOffAnimation());
   }
  });
 }
}
資料來源:http://www.cnblogs.com/mudoot/articles/1985142.html