LinearLayout onclick does not translate after TranslateAnimation

Here is my problem. I have a LinearLayout that has clickable = true for the onTouch event so that when you touch LinearLayout it slides up the screen. It works, but then when the onTouch event firing from a new place, nothing happens.

Steps:

  • I touch LinearLayout and it moves up as it should.
  • I touch it again and nothing happens
  • I touch the part of the screen where LinearLayout was originally LinearLayout will switch as it should.

It seems that the performance has moved to a new place, but in reality it is not.

Below is my xml and code.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/julyContainer" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@+id/rel01"/> <ImageView /> </RelativeLayout> <ImageView android:id="@+id/shadow" android:paddingTop="6dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/header_barshadow"/> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="@drawable/bg_calendar" android:id="@+id/calScroller"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:id="@+id/circleLayout" android:orientation="horizontal" android:clickable="true" android:onClick="@string/circleAction" android:paddingTop="10dip" android:paddingLeft="10dip" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/circleCal" android:background="@drawable/cal_circle_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="@string/circleAction"/> <LinearLayout android:id="@+id/circleLayout01" android:orientation="vertical" android:paddingLeft="10dip" android:paddingRight="3dip" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView/> <TextView/> <TextView/> <LinearLayout android:id="@+id/julylayout2" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical"> <TextView/> </LinearLayout> </LinearLayout> </LinearLayout> <ImageView android:id="@+id/etch1" android:src="@drawable/etch_calendar" android:paddingTop="15dip" android:paddingBottom="15dip" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/circleLayout"/> <LinearLayout android:id="@+id/squareLayout" android:clickable="true" android:onClick="@string/squareAction" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="10dip" android:layout_below="@id/etch1"> <Button android:id="@+id/squareCal" android:background="@drawable/cal_square_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="@string/squareAction"/> <LinearLayout android:orientation="vertical" android:paddingLeft="10dip" android:paddingRight="3dip" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView/> <TextView/> <TextView/> <LinearLayout android:id="@+id/layout3" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical"> <TextView/> </LinearLayout> </LinearLayout> </LinearLayout> </RelativeLayout> </ScrollView> </LinearLayout> 

CODE:

 private void slideUp(View view) { Animation slide = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -0.25f); slide.setDuration(1000); slide.setFillAfter(true); slide.setFillEnabled(true); view.startAnimation(slide); } private void slideDown(View view) { Animation slide = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -0.25f, Animation.RELATIVE_TO_SELF, 0.0f); slide.setDuration(1000); slide.setFillAfter(true); slide.setFillEnabled(true); view.startAnimation(slide); } 

NEW CHANGES: the withdrawal of new positions

07-05 13: 20: 22.084: I / System.out (15187): onAnimationStart 0, 120

07-05 13: 20: 23.053: I / System.out (15187): onAnimationEnd 0, 120

 private void slideUp(final View view) { Animation slide = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -0.75f); slide.setDuration(1000); slide.setFillAfter(true); slide.setFillEnabled(true); view.startAnimation(slide); slide.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { int[] startPosition = new int[2]; view.getLocationOnScreen(startPosition); System.out.println("onAnimationStart " + startPosition[0] + " , " + startPosition[1]); } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { final int left = view.getLeft(); final int top = view.getTop(); final int right = view.getRight(); final int bottom = view.getBottom(); int offset = (int) 0.75; view.layout(left, top + offset * top, right, bottom + offset * bottom); int[] endPosition = new int[2]; view.getLocationOnScreen(endPosition); System.out.println("onAnimationEnd " + endPosition[0] + " , " + endPosition[1]); } }); } 
0
source share
1 answer

This is the usual behavior for android animations. This is because the animation does not actually move the layout, so the position on the display remains unchanged. If you want to move the layout to the end point of the animation, you need to call the yourLayout.layout() method and pass 4 parameters there that describe the new layout position. Keep in mind that layout() gets the parameters relative to the parent.

See code example below

 private AnimationListener slideDownAnimationListener = new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { final int left = view.getLeft(); final int top = view.getTop(); final int right = view.getRight(); final int bottom = view.getBottom(); view.layout(left, top - 0.25 * top, right, bottom - 0.25 * bottom); } }; private Animation slideDownAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -0.25f, Animation.RELATIVE_TO_SELF, 0.0f ); private void slideDown(final View view) { slide.setDuration(1000); slide.setFillAfter(true); slide.setFillEnabled(true); slide.setAnimationListener(slideDownAnimationListener); view.startAnimation(slideDownAnimation); } 
+2
source

Source: https://habr.com/ru/post/1500096/


All Articles