java - Android:动画 - 如何确定 xml 文件动画的结束并在 java 中启动 AlphaAnim?

标签 java android xml animation

我有一个动画 (xml) 文件,可以将 ImageView 设置为看起来像电影的演职员表:

<?xml version="1.0" encoding="utf-8"?>

<set

xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator" >

<translate
    android:duration="10000"
    android:fromYDelta="100%p"
    android:toYDelta="0%p" />

</set>

如您所见,图像在全屏时停止“移动”。还行吧。 我的问题是:当这个 IV 到达动画的末端时,我如何设置一个 textView(隐藏)可见,这样文本就可以覆盖在图像上?

java代码:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_final);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    mozart=(ImageView) findViewById(R.id.mozart);

      Picasso.with(this).load(R.drawable.amadeus).fit().centerCrop().into(mozart);

    read=(TextView) findViewById(R.id.read);
    restart=(Button) findViewById(R.id.restart);
    restart.setOnClickListener(this);

    Animation moz= AnimationUtils.loadAnimation(this, R.anim.animation);
    mozart.startAnimation(moz);

    read.setVisibility(View.INVISIBLE);
    read.setText("Text to appear with other alphaAnimation");

}


@Override
public void onClick(View view) {
    if (view == restart) {
        allaTurca.stop();

       Intent a = new Intent(this, MainActivity.class);
        startActivity(a);
        this.finish();
    }

  }

}

这可能吗?我尝试了几种方法,但我不知道如何在图像准备好“接收”文本时告诉程序。感谢您的帮助

最佳答案

对于动画开始重复和结束

你必须像这样给动画添加一个动画监听器

moz.setAnimationListener(new Animation.AnimationListener(){
@Override
public void onAnimationStart(Animation arg0) {
   //Do Something on start
}           
@Override
public void onAnimationRepeat(Animation arg0) {
}           
@Override
public void onAnimationEnd(Animation arg0) {
}
});

当动画开始时,它首先会通过onStart(这是你可以初始化动画变量的地方)......

然后如果你的动画中有重复,onAnimationRepeat 将被调用。

当动画完成时,将调用 onAnimationEnd。 (这是你感兴趣的部分)

编辑 正如评论中指出的那样……更好的方法是使用 AnimatorListenerAdapter ……谢谢 marmor

AnimatorListenerAdapter onEnd = new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator _a) {
            DropZone.this.setTranslationY(getHeight() + 2);
            DropZone.this.setAlpha(0f);
        }
    };

您可以在此处找到示例 http://www.programcreek.com/java-api-examples/index.php?api=android.animation.AnimatorListenerAdapter

关于java - Android:动画 - 如何确定 xml 文件动画的结束并在 java 中启动 AlphaAnim?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40791287/

相关文章:

java - 写入文件的 Printwriter

java - 在运行时从 jar 文件加载的类上查找类

android - 自定义 Facebook 登录按钮 - Android

使用正则表达式的 Python XML 重建

java - 如何在 Hibernate 搜索中索引一组 UUID?

java - 将数组与 Switch 语句一起使用时需要常量表达式

java - 我的绘画课遇到问题

android - 如何将 arrayList 传递给 AlertDialog fragment

python从xml中提取项目

java - XSD xs :choice shouldn't allow empty choice