【问题标题】:Android: Make imageView clickable on animationAndroid:使 imageView 在动画上可点击
【发布时间】:2017-08-30 01:19:36
【问题描述】:

我有一个 imageView,它目前只是使用 translateAnimation 从屏幕的左向右移动。我希望用户在 imageView 穿过屏幕时单击它,然后这会将 ImageView 设置为 INVISIBLE。我的问题是因为我使用翻译动画这是无法完成的。我应该怎么做?可以举个例子吗。

我的代码:

package com.example.mr_br.ibcc_bomber_command;

import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class front_gunner extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_front_gunner);
    //sets screen orientation on created
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    //animation
    final TranslateAnimation moveLefttoRight = new TranslateAnimation(-400, 2500, 0, 0);
    moveLefttoRight.setDuration(10000);
    moveLefttoRight.setFillAfter(false);



    //enemies
    final ImageView enemy1 = (ImageView) findViewById(R.id.enemy1);

    enemy1.setAnimation(moveLefttoRight);

    enemy1.setOnClickListener (new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            enemy1.setVisibility(View.INVISIBLE);
        }
    });



}
}

谢谢。

【问题讨论】:

  • 尝试在 OnClickListener 中停止动画。
  • 这很奇怪,因为它在动画的第一秒起作用,并且图像视图变得不可见,但之后它就无法点击了......
  • 为什么需要检测不可见视图的点击事件?
  • 我需要单击它使其不可见,将其视为一个敌人飞过屏幕的游戏,单击即可摧毁它。

标签: java android imageview translate-animation


【解决方案1】:

fluent 方法更令人愉快:

enemy1.animate()
    .translationX(2500.0f)
    .setDuration(10000)
    .withStartAction(() -> enemy1.setTranslationX(-400.0f));

【讨论】:

    【解决方案2】:

    我得出的解决方案:

     ObjectAnimator anim = ObjectAnimator.ofFloat(enemy1, "translationX", -400f, 2500f);
        anim.setDuration(10000);                  // Duration in milliseconds
        anim.start();
    

    我使用了不同类型的动画师。这很好用。如果其他人遇到同样的问题,这是一个不错的解决方案。

    【讨论】:

      猜你喜欢
      • 2011-03-24
      • 2016-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      相关资源
      最近更新 更多