2014-05-09 11 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