安卓中的动画

6

我有一个活动(称为homeActivity),当我从homeActivity启动新的活动(称为nextActivity)时,我希望给它一个动画效果,比如从底部出现。在Android中是否可能实现?

3个回答

6
在调用 startActivity 后,使用 XML 中定义的动画 ID 呼叫 overridePendingTransition,一个是退出 Activity 的动画,另一个是进入 Activity 的动画。有关此方法,请参见文档。请注意保留 HTML 标记。

2

您可以在意图中使用Intent.FLAG_ACTIVITY_NO_ANIMATION标志来防止默认动画(从右侧滑入)。

例如:

Intent myIntent = new Intent(context, MyActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(myIntent);

然后在您的Activity中,您只需指定自己的动画即可。


1
TopListActivity topList;
Vector<BitmapDrawable> images;
int count = 0;
public AnimationAlphaTimer(TopListActivity _topList)
{
  this.topList = _topList;

  this.images = new Vector<BitmapDrawable>();
  for (int i = 0; ; i++) {
  // LOAD IMAGES HERE
  }

  if (this.images.size() > 0) {
    this.topList.slide_0.setBackgroundDrawable(this.images.get(0));

    if (this.images.size() > 1) {
      this.topList.slide_1.setBackgroundDrawable(this.images.get(1));
    }
  }

  this.count = 1;
}

public void launch()
{
  if (this.images.size() >= 2) {
    (new Timer(false)).schedule(this, 100);
  }
}

@Override
public void run()
{
  this.doit();
  this.cancel();
}

private void doit()
{
  if ((this.count % 2) == 0) {
    AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
    animation.setStartOffset(3000);
    animation.setDuration(3000);
    animation.setFillAfter(true);
    animation.setAnimationListener(this);

    this.topList.slide_1.startAnimation(animation);
  } else {
    AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setStartOffset(3000);
    animation.setDuration(3000);
    animation.setFillAfter(true);
    animation.setAnimationListener(this);

    this.topList.slide_1.startAnimation(animation);
  }
}

public void onAnimationEnd(Animation animation)
{
  if ((this.count % 2) == 0) {
    this.topList.slide_1.setBackgroundDrawable(
      this.images.get((this.count + 1) % (this.images.size()))
    );
  } else {
    this.topList.slide_0.setBackgroundDrawable(
      this.images.get((this.count + 1) % (this.images.size()))
    );
  }

  this.count++;
  this.doit();
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation)  {
}}

试试这个,我觉得它会起作用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接