2014-04-16 3 views
3

Translate 애니메이션으로 두 가지보기 (하나는 올라가고 다른 것은 내려감)를 시뮬레이트하려고합니다.Android Translate 애니메이션 두 가지보기 교환

기본적으로 스왑하고 다시 스왑합니다.

이것은 내 활동입니다.

LinearLayout topView, belowView; 
    TextView foo, bar; 
    int viewHeight; 
    boolean noSwap = true; 
    private static int ANIMATION_DURATION = 3000; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_slide); 

    topView = (LinearLayout) findViewById(R.id.top_view); 
    belowView = (LinearLayout) findViewById(R.id.below_view); 

    foo = (TextView) findViewById(R.id.foo); 
    bar = (TextView) findViewById(R.id.bar); 

    ImageButton btnSwitch = (ImageButton) findViewById(R.id.switch_btn); 

    ViewTreeObserver viewTreeObserver = foo.getViewTreeObserver(); 
    if (viewTreeObserver.isAlive()) { 
     viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      foo.getViewTreeObserver().addOnGlobalLayoutListener(this); 
      viewHeight = foo.getHeight(); 
      foo.getLayoutParams(); 
     } 
     }); 
    } 

    btnSwitch.setOnClickListener(new View.OnClickListener() { 
     @Override public void onClick(View v) { 
     if (noSwap) { 
      //Log.d("Swap Status", "Not Swapping yet ? " + true); 

      TranslateAnimation ta1 = new TranslateAnimation(0, 0, 0, viewHeight); 
      ta1.setDuration(ANIMATION_DURATION); 
      ta1.setFillAfter(true); 
      topView.startAnimation(ta1); 
      topView.bringToFront(); 

      belowView.setY(0); 
      TranslateAnimation ta2 = new TranslateAnimation(0, 0, 0, -viewHeight); 
      ta2.setDuration(ANIMATION_DURATION); 
      ta2.setFillAfter(true); 
      belowView.startAnimation(ta2); 
      belowView.bringToFront(); 

      noSwap = false; 
     } else { 
      //Log.d("Swap Status", "Swapped already ? " + true); 

      TranslateAnimation ta1 = new TranslateAnimation(0, 0, viewHeight, 0); 
      ta1.setDuration(ANIMATION_DURATION); 
      ta1.setFillAfter(true); 
      topView.startAnimation(ta1); 
      topView.bringToFront(); 

      belowView.setY(0); 

      TranslateAnimation ta2 = new TranslateAnimation(0, 0, 0, viewHeight); 
      ta2.setDuration(ANIMATION_DURATION); 
      ta2.setFillAfter(true); 
      belowView.startAnimation(ta2); 
      belowView.bringToFront(); 

      noSwap = true; 
     } 
     } 
    }); 
    } 

이것은 레이아웃입니다. 평면도가 아래로보기가 이동하는 것입니다 이하로 가정 할 때 나는 세 번째 시간에 대한 Switch 버튼을 클릭 할 때까지

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="${packageName}.${activityClass}"> 

    <LinearLayout 
     android:id="@+id/top_view" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/foo" 
     android:text="@string/foo" 
     android:textSize="30sp" 
     android:textColor="@android:color/holo_purple" 
     android:padding="10dp" 
     android:fontFamily="sans-serif-condensed" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/below_view" 
     android:layout_below="@+id/top_view" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/bar" 
     android:text="@string/bar" 
     android:textSize="30sp" 
     android:textColor="@android:color/holo_red_light" 
     android:padding="10dp" 
     android:fontFamily="sans-serif-condensed" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 
    </LinearLayout> 

    <ImageButton android:id="@+id/switch_btn" 
     style="?android:borderlessButtonStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_action_import_export" 
     android:contentDescription="@string/swap" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"/> 

</RelativeLayout> 

모든 것이 잘 보인다. 그러나 아래보기의 시작 위치가 올바르지 않습니다. 내가보기 아래에 여러 위치를 설정했는데 여전히 권리를 얻을 수 없습니다.

나는 그것에 관한 스크린 캐스트를 만들었습니다 here.

+0

왜 android.widget.ViewSwitcher를 사용하지 않습니까? – pskink

+0

@pskink 전에 ViewSwitcher를 사용한 적이 없습니다. 나는 그것을 시도 할 것이다. –

답변

6

나는 그것을 올바르게 생각한다. 여기에 노동 계급이 있습니다.

LinearLayout topView, belowView; 
    TextView foo, bar; 
    int viewHeight; 
    boolean noSwap = true; 
    private static int ANIMATION_DURATION = 300; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_slide); 

    topView = (LinearLayout) findViewById(R.id.top_view); 
    belowView = (LinearLayout) findViewById(R.id.below_view); 

    foo = (TextView) findViewById(R.id.foo); 
    bar = (TextView) findViewById(R.id.bar); 

    ImageButton btnSwitch = (ImageButton) findViewById(R.id.switch_btn); 

    ViewTreeObserver viewTreeObserver = foo.getViewTreeObserver(); 
    if (viewTreeObserver.isAlive()) { 
     viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      foo.getViewTreeObserver().addOnGlobalLayoutListener(this); 
      viewHeight = foo.getHeight(); 
      foo.getLayoutParams(); 
     } 
     }); 
    } 

    btnSwitch.setOnClickListener(new View.OnClickListener() { 
     @Override public void onClick(View v) { 
     if (noSwap) { 
      TranslateAnimation ta1 = new TranslateAnimation(0, 0, 0, viewHeight); 
      ta1.setDuration(ANIMATION_DURATION); 
      ta1.setFillAfter(true); 
      topView.startAnimation(ta1); 
      topView.bringToFront(); 

      TranslateAnimation ta2 = new TranslateAnimation(0, 0, 0, -viewHeight); 
      ta2.setDuration(ANIMATION_DURATION); 
      ta2.setFillAfter(true); 
      belowView.startAnimation(ta2); 
      belowView.bringToFront(); 

      noSwap = false; 
     } else { 
      TranslateAnimation ta1 = new TranslateAnimation(0, 0, viewHeight, 0); 
      ta1.setDuration(ANIMATION_DURATION); 
      ta1.setFillAfter(true); 
      topView.startAnimation(ta1); 
      topView.bringToFront(); 

      TranslateAnimation ta2 = new TranslateAnimation(0, 0, -viewHeight, 0); 
      ta2.setDuration(ANIMATION_DURATION); 
      ta2.setFillAfter(true); 
      belowView.startAnimation(ta2); 
      belowView.bringToFront(); 

      noSwap = true; 
     } 
     } 
    }); 
    }