I have an ImageView
which is a child of a LinearLayout
. If I animate the ImageView
anywhere inside the LinearLayout
, the onTouchListener()
is responsive after the animation, but becomes unresponsive if the animation moves the view outside of the direct LinearLayout
parent.
In other words, whether ImageView
onTouchListener()
is fired or not, depends entirely on whether the x and y offset of my animation are within bounds of the LinearLayout
parent.
ObjectAnimator translateX =
ObjectAnimator.ofFloat(view, "translationX",
offSet_X);
ObjectAnimator translateY =
ObjectAnimator.ofFloat(view, "translationY",
offSet_Y);
translateX.setDuration(translationDurationMill);
translateX.start();
translateY.setDuration(translationDurationMill);
translateY.start();
Is this behavior expected, or could it be a bug in in my code?
edit
So for example, I've reduced the problem into a simple app, and verified the behavior is not a bug, but just the way Android works, but I'm not sure how to work around the behavior.
So here is a simple layout for the main activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:clipChildren="false"
android:clipToPadding="false"
>
<LinearLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@color/colorPrimary"
android:text="Hello World!"
android:gravity="center"
>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:text="hello world" />
</LinearLayout>
</LinearLayout>
Here is the main activity code
public class MainActivity extends AppCompatActivity
implements View.OnTouchListener{
int mTranslationDurationMill = 500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = findViewById(R.id.tv);
tv.setOnTouchListener(this);
animateTextView(tv,10,10);
}
private void animateTextView(View view, int offSet_X,int offSet_Y)
{
ObjectAnimator translateX =
ObjectAnimator.ofFloat(view, "translationX",
offSet_X);
ObjectAnimator translateY =
ObjectAnimator.ofFloat(view, "translationY",
offSet_Y);
translateX.setDuration(mTranslationDurationMill);
translateX.start();
translateY.setDuration(mTranslationDurationMill);
translateY.start();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("TAG","onTouch()");
return false;
}
}
Above I have animateTextView(tv,10,10);
to animate the text view, and after animation I can confirm the log indicates the touch event fired, and here is picture
but If do animateTextView(tv,400,400);
then the view moves outside the direct parent LinearLayout
and touch event does not register. Here is picture.