【问题标题】:Translate a view and get click event翻译视图并获取点击事件
【发布时间】:2015-01-29 16:54:33
【问题描述】:

如果这个问题已经被问到,请原谅我。

我是 android 动画的新手。

我想要一个视图让我们说一个图像视图(带有动画,即平滑移动)将一个位置永久移动到屏幕的另一个位置让我们说如果当前 X 坐标到 X+50 并且类似地 Y坐标为 Y+50。我能够使用 XML 翻译动画文件移动,但之后无法获得点击事件。我能够在布局中的原始位置而不是从翻译位置获得点击事件

谁能引导我走上正轨?

提前致谢。

【问题讨论】:

标签: android android-animation


【解决方案1】:

您使用的动画已被弃用。我展示了一个 ObjectAnimator 类的示例...这个示例位于片段中,所以我编写了整个 onCreateView。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main_screen, container, false);

    ImageView image = (ImageView) rootView.findViewById(R.id.image);

    ObjectAnimator translationY = ObjectAnimator.ofFloat(image, "translationY", 100);
    translationY.setInterpolator(new AccelerateInterpolator());

    ObjectAnimator translationX = ObjectAnimator.ofFloat(image, "translationX", 100);
    translationX.setInterpolator(new AccelerateInterpolator());

    AnimatorSet as = new AnimatorSet();
    as.playTogether(translationX, translationY);
    as.setDuration(2000);
    as.start();


    image.setOnClickListener(new View.OnClickListener() {
        int sum = 0;
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Clicked: " +  sum++, Toast.LENGTH_SHORT).show();
        }
    });


    return rootView;
}

【讨论】:

    【解决方案2】:

    以编程方式制作动画。试试这个代码。它可能会有所帮助

    TranslateAnimation translateAnimation = new TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta);
                translateAnimation.setDuration(500);
                translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                         //before animation
                    }
    
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        //after animation
    
                    }
    
                    @Override
                    public void onAnimationRepeat(Animation animation) {
    
                    }
                });
                imageView.startAnimation(translateAnimation);
    

    【讨论】:

      【解决方案3】:

      您可能使用view.startAnimation();。这里的关键是以编程方式执行您的animation

      Animation animation = AnimationUtils.loadAnimation (this.context, R.anim.your_translate_animation);
      animation .setStartOffset (2000);
      yourView.startAnimation (animation);
      
      animation.setAnimationListener (new AnimationListener () {
      
          @Override
          public void onAnimationStart (Animation animation) {
      
          }
      
          @Override
          public void onAnimationRepeat (Animation animation) {
      
          }
      
          @Override
          public void onAnimationEnd (Animation animation) {
      
              //do you click functionality here...
      
          }
      });
      

      附:如果你想以编程方式点击查看

      yourView.performClick();
      

      【讨论】:

      • 谢谢 Murtaza Hussain...我也做了同样的...我不想以编程方式调用点击事件。视图移动正常,但翻译后的点击事件没有得到..视图仍然在原始位置,从那里我得到点击事件。
      • 哦,你的意思是你无法点击?
      • animation.startAnimation()之后使用animation.setFillAfter (true);
      • 特别是翻译 xml 文件
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多