5

I am trying to make an animation with two textviews. Both are in a relative layout. The functionality of the animation is left textview will go little bit left and at the same time right textview will also go little bit left. I have tried:

http://nineoldandroids.com/ and default way. enter image description here

But for both case I am getting a gap during the process. I already put one question but i am not getting any positive reply:

Android slider animation is not synchronized

Nineoldandroids code:

xml file:

 <RelativeLayout  
android:layout_width="match_parent" android:layout_height="wrap_content"  
android:orientation="horizontal"
android:layout_below="@+id/target"
android:layout_marginTop="50dp"> 

<TextView
    android:id="@+id/TextView01"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"     
    android:background="#f00"
    android:gravity="center"
    android:text="RED"
    android:textColor="#000" />

<Button
        android:id="@+id/TextView02"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/TextView01"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/TextView01"
        android:background="#0F0"
        android:text="TXT"
        android:visibility="invisible" />
</RelativeLayout>

MainActivity.java:

public class MainActivity extends Activity {
  double counter = 0;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.toggles);
    final View target = findViewById(R.id.target);
    final int duration = 5*1000;
    final int duration1 = 300;

    final View textView1 = findViewById(R.id.TextView01);
    final View textView2 = findViewById(R.id.TextView02);
    textView1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (counter == 0) {
          textView2.setVisibility(View.VISIBLE);
          ObjectAnimator.ofFloat(textView1, "translationX", 0, -50).setDuration(duration1).start();
          ObjectAnimator.ofFloat(textView2, "translationX", 100, 0).setDuration(duration1).start();
          counter++;

        }
        else {
          ObjectAnimator.ofFloat(textView1, "translationX", -50,0).setDuration(duration1).start();
          ObjectAnimator.ofFloat(textView2, "translationX", 0,100).setDuration(duration1).start();
          counter--;
        }
      }
    });
  }
}

How can I fix it?

otherwise I am trying to put both textview inside a layout where second textview will be outside the screen and using the animation I will move the whole layout. how can I make the xml like this? enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kousik
  • 21,485
  • 7
  • 36
  • 59
  • 2
    You used in your blackRight from 85 to 100. You should use from 100 to 115. To put it back, use from 115 to 100. – Sherif elKhatib Oct 28 '13 at 10:36
  • sorry,its not working. – Kousik Oct 28 '13 at 10:53
  • Can you show the current result? If the first textview is currently sliding correctly, try to slide the second textview using `0, 50` and `50, 0` instead of `100, 0` and `0, 100`. – Sherif elKhatib Oct 28 '13 at 11:09
  • i think you are not geting my question. i am geting the textview in proper position before and after the animation. only at the time of animation i got some small gap. – Kousik Oct 28 '13 at 11:23

2 Answers2

2

Sorry for misunderstanding your question.

Anyway, your issue is not about time exactly. Both animations have the same duration but the problem is the sizes of the animated regions are different. Logically, moving a longer distance in the same duration will look slower than moving a smaller distance.

For example, to solve your problem, I used the following code in the OnClickListener:

public void onClick(View v) {
    int width =textView2.getWidth();
    if (counter == 0) {
        textView2.setVisibility(View.VISIBLE);
        ObjectAnimator.ofFloat(textView1, "translationX", 0, -1*width).setDuration(duration1).start();
        ObjectAnimator.ofFloat(textView2, "translationX", 1*width, 0).setDuration(duration1).start();
        counter++;

    }
    else {
        ObjectAnimator.ofFloat(textView1, "translationX", -1*width, 0).setDuration(duration1).start();
        ObjectAnimator.ofFloat(textView2, "translationX", 0, 1*width).setDuration(duration1).start();
        counter--;
    }
}
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • Thanks @sherif its working fine.I was assuming that as textView2 width is 50dp thats why the movement will be 0 to -50. but I was wrong. thanks once again. – Kousik Oct 28 '13 at 11:52
1

Try this

<!-- YOUR CONTAINER -->
<LinearLayout
    android:id="@+id/target"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/target"
    android:layout_marginTop="50dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:background="#f00"
        android:gravity="center"
        android:text="RED"
        android:textColor="#000" />

    <Button
        android:id="@+id/TextView02"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/TextView01"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/TextView01"
        android:background="#0F0"
        android:text="TXT"
        android:visibility="invisible" />
</LinearLayout>

Then use a ViewPropertyAnimator like this (if you can use it)

//Translate your view -50 to the left. 
yourLinearLayout.animate().translationX(-50).setDuration(1000);

With the ObjectAnimator I achieved the same results by using this code

    Button textView02 = (Button) findViewById(R.id.textView02);
    LinearLayout target = (LinearLayout) findViewById(R.id.target);
    int width = textView02.getWidth();

    ObjectAnimator.ofFloat(target, "translationX", 0, -width).setDuration(1000).start();

Hope this helps you

the-ginger-geek
  • 7,041
  • 4
  • 27
  • 45
  • hey @Neil I just tried whatever you did. but only the red textview is moving and i am not able to see the button. coz its is overlap with the red textview.I think inside one LinearLayout if you put one textview having width equal to match_parent then the other view will be invisible. – Kousik Oct 28 '13 at 11:37
  • No in the layout I specified the button WILL be to the right of the view off screen. Remove the visibility="invisible" and see what the results are. I think that you should replace the 50 value in the translation with the button width. It could be that the width of the button is 50dp but the translation is using 50px or some other size. – the-ginger-geek Oct 28 '13 at 11:47
  • sorry dude i also tried just now. its not working. the button is no coming.only the red textview1 is moving – Kousik Oct 28 '13 at 12:12
  • Did you use the width of the button to translate with? see my edit. – the-ginger-geek Oct 28 '13 at 12:15
  • ya i did that also. i think when i click on the layout then only the textview1 is moving the other part is out side the screen may be thats why its not moving. – Kousik Oct 28 '13 at 12:18