Android动画分析之3D翻转效果(3)

MainActivity的代码如下:

package com.example.textviewtest;

import Android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 private TextView tv;
 private Button btn;
 private int count = 1;

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  tv = (TextView) findViewById(R.id.tv);
  tv.setText(String.valueOf(count));
  btn = (Button) findViewById(R.id.next_btn);
  applyRotation(0, 90);

btn.setOnClickListener(new View.OnClickListener() {

@Override
   public void onClick(View v) {
    applyRotation(0, 90);
   }
  });

}

private void applyRotation(float start, float end) {
  // 计算中心点
  final float centerX = tv.getWidth() / 2.0f;
  final float centerY = tv.getHeight() / 2.0f;

final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end,
    centerX, centerY, 310.0f, true);
  rotation.setDuration(500);
  rotation.setFillAfter(true);
  rotation.setInterpolator(new AccelerateInterpolator());
  // 设置监听
  rotation.setAnimationListener(new DisplayNextView());

tv.startAnimation(rotation);
 }

private final class DisplayNextView implements Animation.AnimationListener {

public void onAnimationStart(Animation animation) {
  }

// 动画结束
  public void onAnimationEnd(Animation animation) {
   tv.post(new SwapViews());
  }

public void onAnimationRepeat(Animation animation) {
  }
 }

private final class SwapViews implements Runnable {

public void run() {
   final float centerX = tv.getWidth() / 2.0f;
   final float centerY = tv.getHeight() / 2.0f;
   Rotate3dAnimation rotation = null;

tv.requestFocus();

rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f,
     false);
   rotation.setDuration(500);
   rotation.setFillAfter(true);
   rotation.setInterpolator(new DecelerateInterpolator());
   // 开始动画
   tv.startAnimation(rotation);
   tv.setText(String.valueOf(count++));
  }
 }

@Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

看懂了吗?呵呵。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/da8b6adfef568172137f710a99f337a8.html