繁体   English   中英

Android:使imageView在动画上可点击

[英]Android: Make imageView clickable on animation

我有一个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);
        }
    });



}
}

谢谢。

我来到的解决方案:

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

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

流利的方法更加令人愉快:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM