I am a beginner in Android. I am developing one animated game in Activity(not use canvas or surface view an not draw any things). I have faced with one problem is that – while vehicle is moving from the left side to the right side of the screen I want to perform some task till vehicle is visible on the screen. But, I’m unable to do it in a right way. How I can get to know if the vehicle is Visible on the screen and also, when it crosses the screen?
For this I was using this code till now-
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="-50%"
android:toXDelta="170%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="6000"
android:zAdjustment="bottom"
/>
and implement it in Activity as–
final Animation a = AnimationUtils.loadAnimation(this, R.anim.translate);
a.reset();
vehicleView.setImageResource(R.drawable.benz_rock_1);
vehicleView.startAnimation(a);
Now How I can get if the car is on the screen or out of screen? Sorry for my english…
I see two possible solutions for that
You can use View.getGlobalVisibleRect. It returns true if your view is in the screen, so you can easily do whatever you need.
Or you can use View.getLocationOnScreen(Int[] location). And as written on the documentation:
You can use any of this two methods to detect if it’s in the screen or not!
Hope it helps!