2013-10-28 2 views
3

두 개의 텍스트 뷰가있는 애니메이션을 만들려고합니다. 둘 다 상대 레이아웃에 있습니다. 애니메이션의 기능은 그대로두고 텍스트 뷰는 약간 왼쪽으로 가고 동시에 텍스트 뷰도 만듭니다. 조금 남았 어. 시도 : -여러보기가있는 슬라이딩 애니메이션 동기화 문제

http://nineoldandroids.com/ 및 기본 방법.

Android slider animation is not synchronized

Nineoldandroids 코드 :

- : enter image description here

그러나 두 경우에 나는 process.I 동안 격차가 이미 하나 개의 질문을 넣어 무엇입니까하지만 난 어떤 긍정적 인 답변을 받고 있지 않다 xml 파일 : -

<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--; 
     } 
     } 
    }); 
    } 
} 

어떻게 해결할 수 있습니까?

그렇지 않으면 두 번째 textview가 화면 외부에 있고 애니메이션을 사용하여 layoutview 내부에 textview를 넣으려고합니다. 전체 layout.how를 움직일 수 있습니까? 이처럼 xml을 만들 수 있습니까? enter image description here

+2

당신은 당신은 다시 말하면 115 (100)에서 사용한다 (100)에 85에서 blackRight에 사용, 115에서 100까지 사용하십시오. –

+0

죄송합니다. 작동하지 않습니다. – Nullify

+0

현재 결과를 표시 할 수 있습니까? 첫 번째 텍스트 뷰가 현재 올바르게 슬라이딩되는 경우 '100, 0'및 '0, 100'대신 '0, 50'및 '50, 0'을 사용하여 두 번째 텍스트 뷰를 슬라이드합니다. –

답변

2

죄송합니다. 질문에 대한 오해.

어쨌든, 문제는 정확한 시간이 아닙니다. 두 애니메이션의 지속 시간은 같지만 애니메이션 영역의 크기가 다른 경우 문제가 발생합니다. 논리적으로 동일한 기간 동안 더 먼 거리를 이동하는 것이 더 작은 거리를 이동하는 것보다 느리게 보입니다.

예를 들어, 당신의 문제를 해결하기 위해, 나는 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--; 
    } 
} 
+0

고마워요. @sherif 잘 작동합니다. textView2 너비가 50dp 인 것으로 가정했는데, 왜 운동이 0에서 -50이되는지. 그러나 나는 틀렸다. 다시 한번 감사합니다. – Nullify

1

(당신이 그것을 사용할 수있는 경우)이

<!-- 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> 

그런 다음이 코드

를 사용하여 내가 같은 결과를 달성 ObjectAnimator

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

이 같은 ViewPropertyAnimator을 사용하십시오

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(); 

희망이 있으면 도움이됩니다.

+0

안녕하세요, @Neil 방금 한 일을 시도했습니다. 빨간색 텍스트 뷰만 움직이며 버튼을 볼 수 없습니다. coz 자사의 빨간색 textview.i와 중복되는 경우 하나의 LinearLayout 내부 match_parent 너비와 같은 하나의 textview 넣어 다음 다른보기가 보이지 않을 것이라고 생각합니다. – Nullify

+0

레이아웃을 지정하지 않으면 화면의 오른쪽에 버튼이 표시됩니다.visibility = "invisible"을 제거하고 결과가 무엇인지 확인하십시오. 번역의 50 값을 버튼 폭으로 대체해야한다고 생각합니다. 버튼의 너비는 50dp이지만 번역은 50px 또는 다른 크기를 사용하고있을 수 있습니다. – Neil

+0

미안 나는 또한 지금 막 시도했다. 그것의 작동하지 않습니다. 버튼이 나타나지 않습니다. 빨간색 텍스트 뷰 1 만 움직입니다. – Nullify