2014-05-09 10 views
0

我使用的是佈局類同此:的Android RealtiveLayout不能處理重疊的權

<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="wrap_content" 
    android:paddingLeft="5dp" 
    android:paddingTop="5dp" 
    android:paddingRight="5dp" 
    android:paddingBottom="5dp" 
    android:background="@drawable/list_item_background" 
    android:descendantFocusability="blocksDescendants" 
    > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:textColor="#000000" 
     android:textSize="35sp" 
     android:text="18:15" /> 

    <!-- some more views on the right side--> 

    <RelativeLayout 
     android:id="@+id/relativeLayoutActive" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:layout_toLeftOf="@id/textView1"> 

     <!-- contains several other views, a RelativeLayout and a FrameLayout--> 
    </RelativeLayout> 

</RelativeLayout> 

我現在要在佈局上進行兩個動畫:

  1. 淡出relativeLayoutActive(AlphaAnimation )並將右側的所有視圖翻譯爲左側
  2. 淡入relativeLayoutActive並將所有視圖重新回到左側

這裏是我的代碼,我使用的動畫:

public void activate(boolean isActive){ 
    if(!this.active && isActive && checkIfActivatable()){ // start animation 2 
     if(animating) 
      return; 
     animating = true; 
     animatingActive = isActive; 
     rootView.addView(relativeLayoutActive); 
     translate(rootView, clock, true); 
     fade(relativeLayoutActive, true, 1, 0, null, this, false); 
        //... 
    }else if(this.active && !isActive){ // start Animation 1 
     if(animating) 
      return; 
     animating = true; 
     animatingActive = isActive; 
     translate(rootView, clock, false); 
     fade(relativeLayoutActive, false, 1, 0, null, this, false); 
        //... 
     view.removeView(relativeLayoutActive); 
    } 
} 

private void translate(View targetParent, View target, boolean right){ 
     Animation a = new TranslateAnimation(0.0f, 
       (right?1:-1)*(targetParent.getWidth() - target.getWidth() - view.getPaddingLeft() - 
       view.getPaddingRight()), 0.0f, 0.0f); 
     a.setDuration(500); 
     a.setInterpolator(AnimationUtils.loadInterpolator(getContext(), 
         android.R.anim.decelerate_interpolator)); 
     a.setFillAfter(true); 
      target.startAnimation(a); 
    } 

    private void fade(View target, boolean in, float inValue, float outValue, AnimationSet as, AnimationListener listener, boolean useAs){ 
     Animation a = new AlphaAnimation(in?outValue:inValue, in?inValue:outValue); 
     a.setDuration(500); 
     a.setFillAfter(true); 
     if(listener!=null){ 
      a.setAnimationListener(listener); 
     } 
     if(!useAs) 
      target.startAnimation(a); 
     else{ 
      as.addAnimation(a); 
      target.startAnimation(as); 
     } 
    } 

當動畫結束「動漫變」是由通過調用view.clearAnimations永久的(),並設置權的LayoutParams。

動畫1完美地工作,但2沒有。除了relativeLayoutActive的淡入之外,一切都像它應該那樣工作。直到動畫結束relativeLayoutActive變得可見,即在動畫期間relativeLayoutActive不可見。

我想這是因爲RelativeLayout不能處理重疊和自relativeLayoutActive由視圖上的覆蓋開始的2至動畫結束

我怎樣才能解決這個問題它變得不可見的?

關於

+0

你能分享完整的佈局文件嗎? –

回答

0

看來您的問題源於視圖/視圖組的z排序。

我想這是因爲RelativeLayout的不能處理重疊 以來relativeLayoutActive是通過在視圖中的覆蓋開始的2 它,直到動畫結束變得不可見

我不是確定你的意思是overlapping。但您可以撥打:

relativeLayoutActive.bringToFront(); 

更改/修復z排序。您需要後打電話到這一點:

rootView.addView(relativeLayoutActive); 

這裏是View#bringToFront()做:

公共無效bringToFront()

更改樹視圖的Z順序,因此它的上其他兄弟的頂部 意見。如果父容器 使用與訂單相關的佈局方案(例如,LinearLayout),則此排序更改可能會影響佈局。在 KITKAT之前,應在此方法之後調用requestLayout()和 invalidate(),以強制父級重新繪製 新子級的順序。

此外,如果您使用API​​ 11及更高版本,我會推薦使用android的新動畫框架。它高度優化並提供更好的用戶體驗。 Link