ziselos
ziselos

Reputation: 514

Detect moving imageview x position on touch screen

I have an activity with two imageviews like this for example: example. The first on creation of activity starts moving from left side of screen to the right (the second imageview does not move) and i want to detect wether the user touches the screen when the first imageview is in range of the second one or not. I tried writing this code:(homeLayout is the layout which contains the 2 images)

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ButterKnife.bind(this);

    homeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            homeLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            int[] locations = new int[2];
            image2.getLocationOnScreen(locations);
            xImage2 = locations[0];
        }
    });

    homeLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            float xCurrentPos = image1.getLeft();
            if (xCurrentPos >= xImage2 && (xCurrentPos <= xImage2 + image2.getWidth())) {
                inRange = true;
            }
            if (inRange) {
                 Toast.makeText(getApplicationContext(), "In range", Toast.LENGTH_SHORT).show();
            } else {
                 Toast.makeText(getApplicationContext(), "Out of range", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    });
}

My problem is that i am getting wrong the current x position of image1 and thus boolean inRange is always false getting "Out of range" toast message. How can i take current x position of image1 and check if image1 is in range of image2 when the user touch the screen? I'm stuck and I can not solve it.

Upvotes: 1

Views: 202

Answers (1)

ziselos
ziselos

Reputation: 514

After searching a few hours i managed to retrieve current x position of moving image using this code:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ButterKnife.bind(this);

    homeLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            homeLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            int[] locations = new int[2];
            image2.getLocationOnScreen(locations);
            xImage2 = locations[0];
        }
    });
 ObjectAnimator translateXAnimation = ObjectAnimator.ofFloat(image1, "translationX", image1.getLeft(), xFinalPos);

    translateXAnimation.setDuration(4000);
    translateXAnimation.start();

    translateXAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator valueAnimator) {
            xCurrentPos = (float)valueAnimator.getAnimatedValue();
        }
    });


    homeLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (xCurrentPos >= xImage2 && (xCurrentPos <= xImage2 + image2.getWidth())) {
                inRange = true;
            }
            if (inRange) {
                 Toast.makeText(getApplicationContext(), "In range", Toast.LENGTH_SHORT).show();
            } else {
                 Toast.makeText(getApplicationContext(), "Out of range", Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    });
}

I had to use AnimatorUpdateListener in order to take back the current x position of my moving image

Upvotes: 1

Related Questions