4

I have a vertical linearlayout with three clickable imageviews within the linearlayout. When i rotate the linearlayout by 90 degrees using simple animation the problem arises. The imageviews are rotated correctly but the onclick events for the imageviews are not rotated along with the linearlayout and remain in the original position as before the animation.

Below is my main java code

       westplayer = (LinearLayout) findViewById(R.id.linearLayout_west);

        // Create an animation instance
    Animation an = new RotateAnimation(0.0f, 180.0f, 32, 180);

    // Set the animation's parameters
    an.setDuration(0);               // duration in ms
    an.setRepeatCount(0);                // -1 = infinite repeated
    an.setRepeatMode(Animation.REVERSE); // reverses each repeat
    an.setFillAfter(true);               // keep rotation after animation 

        // Apply animation to linearlayout
    westplayer.setAnimation(an); 

The code above handles the animation part. The code below follows the animation and is supoosed to update the layout positions but is not working for me.

        // Update Layout 
          int top=westplayer.getTop();
      int bottom=westplayer.getBottom();
      int left=westplayer.getLeft();
      int right=westplayer.getRight();
      westplayer.layout(left, top , right, bottom );

The xml is as follows:

     <LinearLayout
    android:id="@+id/linearLayout_west"
    android:layout_width="42dp"
    android:layout_height="250dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/linearLayout_north"
    android:duplicateParentState="false"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageViewW1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/spades_14"

         />

    <ImageView
        android:id="@+id/imageViewW2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/spades_14"
        android:layout_marginTop="-15dp" 
        />

    <ImageView
        android:id="@+id/imageViewW3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/spades_14"
        android:layout_marginTop="-15dp" 
       />

</LinearLayout>

I have got the update layout code from this and also found another solution that i have also tried this and still no positive results. I need this to work for API 10. Any help is much appreciated

Community
  • 1
  • 1

2 Answers2

0

Yes, you got the expected result.

Since Animation in android only try to apply a transformation matrix on the bitmap of view, it doesn't change the position and size of view.

So after your animation, although you setFillAfter(true) to let the view looks rotated, but in fact the view are still in the same place and doesn't move a little bit.

faylon
  • 7,360
  • 1
  • 30
  • 28
  • Thanks for the reply. I thought that the update layout code would re-postion the views after the animation but it does not seem to do so. So how do i get the view of the imageviews to also rotate along with the bitmaps? Any idea? – user2738708 Sep 04 '13 at 06:49
  • @user2738708 Sorry i really don't know if there is a way to layout a view in a rotated way. – faylon Sep 04 '13 at 09:02
  • Thanks for trying to answer my query, if anyone else has a solution, please help me out. – user2738708 Sep 12 '13 at 10:35
0

The problem you face, is because you use a Tween Animation. The View will rotate and only the visible part will move. But the positions of the ImageView will remain the same like defined in the layout. That's why the clickable area stays the same.

To redefine the position, according to the animation you will have to use a Property Animation, like a ObjectAnimator or a ValueAnimator. With this the propertys defined in xml will be updated. Now the clickable areas move along with the views.

Steve Benett
  • 12,843
  • 7
  • 59
  • 79
  • Yes u right. the property animation would solve my problem, but since i did not mention earlier, i need i to work for api 10 and i think property animation is not supported in api 10. My minimum build is gingerbread 2.3. Thank you for your help. – user2738708 Sep 20 '13 at 06:00
  • I tried the nineoldandroid app that is available on google play that has the same implementation(assumption made by me)as the code in the link provided by you. The app displays various animation tests but the one i was intrested in was the viewpropertyanimator demo. The button in the demo is supposed to be clickable after the animation. I tried the app on a jelly bean device and it works but does not work for a gingerbread device. please confirm my information and thank you for your inputs. – user2738708 Oct 04 '13 at 07:11