2015-06-05 54 views
2

我有,我想按鈕,點擊後擴大,如下圖所示的佈局:Android的 - 更新視圖的高度上translationY()動畫

Expectation

問題是我需要使用動畫,所以我決定使用View.animate.translationY()。這裏是我的代碼:

private void showBottomThreeLines(boolean show){ 
    if(show) 
     mShiftContainer.animate().translationY(0); 
    else 
     mShiftContainer.animate().translationY(-(mFifthLineContainer.getHeight() * 3)); 
} 

但是,這是我測試後得到:

Reality

當前高度仍然是一樣的先前的高度!視圖的高度使用MATCH_PARENT。我甚至試圖將其更改爲1000dp,但它仍然具有相同的高度。如何在translationY()動畫中更新視圖的高度?

+0

翻譯不設置高度..它的舉動使用mShiftContainer.animate()。scaleY( - (mFifthLineContainer.getHeight()* 3))。setDuration(2000);使用mShiftContainer.animate()來查看x,y ... scale爲您完成的工作。 –

+0

是的,它不。這就是爲什麼我認爲我需要別的東西......我嘗試了'mShiftContainer.animate()。scaleY( - (mFifthLineContainer。getHeight()* 3))。setDuration(2000);',但佈局上升,它使我的屏幕變白。我認爲'scaleY()'的參數是錯誤的。 – Harry

+0

嘗試不同的屬性...如mShiftContainer.animate()。scaleYBy()或mShiftContainer.animate()。y() –

回答

1

經過一番研究,我發現定義了翻譯後的視角高度根本不會增加其高度。看起來整個視圖高度的總和不能超過其父級佈局的高度。也就是說,如果您將父級佈局的高度設置爲MATCH_PARENT,並且您的屏幕尺寸爲960 dp,則即使您定義其高度(例如,高度),您的子視圖的最大高度也將爲960 dp。 android:layout_height="1200dp"

因此,我決定動態重新調整父級佈局的高度,並使頁腳佈局的高度爲MATCH_PARENT。默認情況下,我的父母佈局的高度爲MATCH_PARENT,但我撥打以下方法上onCreateView()

private void adjustParentHeight(){ 
    WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); 
    DisplayMetrics metrics = new DisplayMetrics(); 
    wm.getDefaultDisplay().getMetrics(metrics); 
    ViewGroup.LayoutParams params = mView.getLayoutParams(); 
    mFifthLineContainer.measure(0, 0); 
    params.height = metrics.heightPixels + (mFifthLineContainer.getMeasuredHeight() * 3); 
    mView.setLayoutParams(params); 
} 

這將使我的頁腳佈局成爲關閉屏幕。然後我試圖使用View.animate().translationY(),但後來我又碰到了另一個問題! Android動畫中存在一個錯誤,當您致電View.setY()onAnimationEnd()會導致閃爍。看起來原因是onAnimationEnd()在動畫真正結束之前被調用。下面是我用來解決這個問題的引用:

Android Animation Flicker

Android Flicker when using Animation and onAnimationEnd Listener

因此,我改變了我的showBottomThreeLines()方法:

private void showBottomThreeLines(boolean show){ 
    if(show){ 
     TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, -(mFifthLineContainer.getHeight() * 3), 0); 
     translateAnimation.setDuration(300); 
     translateAnimation.setFillAfter(true); 
     translateAnimation.setFillEnabled(true); 
     translateAnimation.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 
       mShiftContainer.setY(mShiftContainer.getY() + mFifthLineContainer.getHeight() * 3); 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 

      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
     }); 
     mShiftContainer.startAnimation(translateAnimation); 
    } else{ 
     TranslateAnimation translateAnimation = new TranslateAnimation(0, 0, mFifthLineContainer.getHeight() * 3, 0); 
     translateAnimation.setDuration(300); 
     translateAnimation.setFillAfter(true); 
     translateAnimation.setFillEnabled(true); 
     translateAnimation.setAnimationListener(new Animation.AnimationListener() { 
      @Override 
      public void onAnimationStart(Animation animation) { 
       mShiftContainer.setY(mFifthLineContainer.getY()); 
      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 

      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 

      } 
     }); 
     mShiftContainer.startAnimation(translateAnimation); 
    } 
} 
0

使用此:

private ActionMode mActionMode; 

... 


private void expandView(View summary, int height, final boolean isSearch) { 
     if (isSearch) summary.setVisibility(View.VISIBLE); 

     final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.EXACTLY); 
     summary.measure(widthSpec, height); 

     Animator animator = slideAnimator(summary.getHeight(), height, summary); 
     animator.start(); 
    } 

    private void collapseView(final View summary, int height, final boolean isSearch) { 
     int finalHeight = summary.getHeight(); 

     ValueAnimator mAnimator = slideAnimator(finalHeight, height, summary); 
     final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.EXACTLY); 
     summary.measure(widthSpec, height); 

     Animator animator = slideAnimator(summary.getHeight(), height, summary); 
     animator.start(); 
     mAnimator.start(); 
    } 

    /** 
    * Slide animation 
    * 
    * @param start start animation from position 
    * @param end  end animation to position 
    * @param summary view to animate 
    * @return valueAnimator 
    */ 
    private ValueAnimator slideAnimator(int start, int end, final View summary) { 

     ValueAnimator animator = ValueAnimator.ofInt(start, end); 

     animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
      @Override 
      public void onAnimationUpdate(ValueAnimator valueAnimator) { 
       //Update Height 
       int value = (Integer) valueAnimator.getAnimatedValue(); 

       ViewGroup.LayoutParams layoutParams = summary.getLayoutParams(); 
       layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, value, getResources().getDisplayMetrics());//value; 
       summary.setLayoutParams(layoutParams); 
      } 
     }); 
     return animator; 
    } 

    private ActionMode.Callback mActionModeCallBack = new ActionMode.Callback() { 
     //Contextual action menu. Shows different options in action bar when a list item is long clicked! 
     @Override 
     public boolean onCreateActionMode(ActionMode actionMode, Menu menu) { 
      // Inflate a menu resource providing context menu items 
      MenuInflater inflater = actionMode.getMenuInflater(); 
      inflater.inflate(R.menu.contextual_menu_options, menu); 
      return true; 
     } 

     @Override 
     public boolean onPrepareActionMode(ActionMode actionMode, Menu menu) { 
      return false; 
     } 

     @Override 
     public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem) { 
      mActionMode.finish(); 
      return true; 
     } 

     @Override 
     public void onDestroyActionMode(ActionMode actionMode) { 
      mActionMode = null; 
     } 
    }; 

希望這將幫助你!

+0

感謝您的回答!我試過你的代碼,但仍然沒有運氣。高度發生了變化,但版面Y位置不變。是否有任何部分代碼需要修改才能實現我的目標? – Harry

+0

@哈利你可以發佈你的XML文件 – ArbenMaloku