2017-01-05 61 views
1

我在CollapsingToolbarLayout內部有一個ImageView,ImageView佔據了底部的一些信息的整個屏幕。當用戶點擊ImageView時,我想讓CollapsingToolBarLayout的高度變爲150dp。如何將CollapsingToolBarLayout的高度設置爲具有平滑動畫設置爲高度150dp。我希望高度順利,慢慢地去150dp不是很快,有一個不錯的動畫。 我怎樣才能做到這一點?如何平滑地設置佈局高度

activity_scrolling.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context="com.myapplication.foodapp.ScrollingActivity" 
android:id="@+id/coordinator_layout" 
android:animateLayoutChanges="true"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:theme="@style/AppTheme.PopupOverlay" 
    android:animateLayoutChanges="true"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_tool_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="fill_parent" 
     android:fitsSystemWindows="true" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     android:animateLayoutChanges="true"> 

     <ImageView 
      android:id="@+id/chipotle_image" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_alignParentTop="true" 
      android:layout_gravity="fill_vertical" 
      android:src="@mipmap/food1" 
      android:adjustViewBounds="true" 
      android:scaleType="fitXY"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Chipotle Chicken Fajitas" 
      android:textSize="27sp" 
      android:layout_gravity="bottom" 
      android:layout_marginBottom="40dp" 
      android:layout_marginLeft="5dp" 
      android:textColor="#fff" 
      android:id="@+id/first_food_title"/> 

     <me.grantland.widget.AutofitTextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="16 ingredients | 284 Calories | 1 hour" 
      android:singleLine="true" 
      android:textAlignment="center" 
      android:textColor="#fff" 
      android:layout_gravity="bottom" 
      android:textSize="20sp" 
      android:id="@+id/spec_text"/> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="150dp" 
      app:layout_collapseMode="pin" 
      android:animateLayoutChanges="true"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <ImageView 
        android:layout_width="25dp" 
        android:layout_height="25dp" 
        android:src="@mipmap/arrow" 
        android:id="@id/back_Arrow" 
        android:layout_marginTop="5dp" 
        /> 




      </LinearLayout> 


     </android.support.v7.widget.Toolbar> 




    </android.support.design.widget.CollapsingToolbarLayout> 

</android.support.design.widget.AppBarLayout> 

<!-- ** CONTENT SCROLLING LAYOUT ** --> 
<include layout="@layout/content_scrolling"> 

</include> 
</android.support.design.widget.CoordinatorLayout> 

回答

0

您可以ValueAnimator實現這一目標。

ValueAnimator anim = ValueAnimator.ofInt(viewToIncreaseHeight.getMeasuredHeight(), finalValue); 
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
    @Override 
    public void onAnimationUpdate(ValueAnimator valueAnimator) { 
     int val = (Integer) valueAnimator.getAnimatedValue(); 
     ViewGroup.LayoutParams layoutParams = viewGroup.getLayoutParams(); 
     layoutParams.height = val; 
     viewGroup.setLayoutParams(layoutParams); 
    } 
}); 
anim.start(); 
相關問題