你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2013-01-20] 来源:Linux社区  作者:loongggdroid [字体:大 中 小]

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;
 }

}

看懂了吗?呵呵。

linux
相关资讯       Android 3D  Android动画效果  Android翻转效果 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款