2014-11-03 77 views
0

我試圖在一些DP滾動後將視圖設置爲動畫並將其隱藏,並且我將所有內容都設置爲正常,但問題在於當它彈出時您在應該觸發動畫的Y值之前或之後緩慢滾動。爲視圖平滑隱藏/顯示動畫並填充空白空間

我認爲輕彈是因爲我有它的可見性設置爲過去了,更新其他視圖match_parent,它不會只用TraslationY工作:

  view.animate() 
        .translationY(-view.getBottom()) 
        .alpha(0) 
        .setDuration(HEADER_HIDE_ANIM_DURATION) 
        .setInterpolator(new DecelerateInterpolator()); 

我試圖設置佈局相對和View 2作爲match_parent來看看我是否能避免可見性改變,但它沒有工作...

我已經實現了從谷歌I/O 2014 BaseActivity.java文件所需的所有代碼: https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/BaseActivity.java#L17

而動畫的作品......但我認爲,因爲我的customview不是具有疊加屬性的動作條,所以customview不會離開,下面的LinearLayout將不會填充空白空間(沒有)。

因此,我使它與animationlistener一起工作,並在動畫結束時設置自定義視圖的可見性消失,但當您接近預期的觸發動畫的Y點時,它會以可怕的方式輕彈(輕彈爲自定義視圖可視性消失了,下面的LinearLayout需要調整自己的大小以填充空白區域,並且如果您在那裏緩慢滾動,則會很快重複)。

XML:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 


    android:clickable="false" 
    android:animateLayoutChanges="true"> 

    <com.project.app.layouts.TabsLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
/> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/tabs"> 
    <FrameLayout 
     android:id="@+id/content_frame_header" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    <android.support.v4.view.ViewPager 
     android:id="@+id/viewpager" 
     android:layout_width="match_parent" 
     android:layout_height="0px" 
     android:layout_weight="1" 
     android:background="@android:color/white"/> 
    </LinearLayout> 

</RelativeLayout> 

有沒有辦法做到這一點時,它不是一個動作條?

編輯: 添加關於如何緩慢滾動Y點觸發動畫以隱藏/顯示時它將如何彈出的註釋。

+0

我建議您在一張紙上繪製您想要的動畫步驟,並將其作爲圖像張貼在這裏,或者您可以使用一些繪圖程序。通過這種方式,我們將展現您所期待的最終效果。 – vovahost 2014-11-03 23:31:58

+0

很簡單,只需要一個視圖1消失並查看2填充視圖1左側的空白區域。就像當您在Google I/O 2014中向下滾動操作欄時隱藏操作欄,但使用自定義視圖而不是操作欄時。 – Motheus 2014-11-04 00:53:51

+0

也許這很容易實現,但很難理解視圖1和視圖2如何在佈局中定位。你提到將可見性設置消失,但我們沒有看到任何代碼。如果你想得到一個很好的答案,那麼請添加一些代碼,也許草圖。 – vovahost 2014-11-04 08:24:45

回答

1

我建議你在你的Manifest文件中使用android:hardwareAccelerated =「true」屬性。它將使用您的設備的GPU繪製視圖和動畫。

+0

對不起,我忘記了添加一條評論。我認爲這個問題是,當你慢慢滾動Y點觸發動畫時,會發生輕彈。如果*你快速滾動,那麼它會沒事的。 – Motheus 2014-11-04 12:09:22

+1

此外,我的minSDK是14,所以android:hardwareAccelerated默認爲true。 – Motheus 2014-11-04 12:12:54

+0

@Motheus你有沒有試過另一個Interpolator?嘗試在清單中添加加速屬性,因爲有時它並未默認啓用,即使您的MINSDK是14 – Arshak92 2014-11-04 12:14:55

1

我建議你在兩種情況下檢查view.getBottom()的值(當它工作和不工作時)。
它可能是因爲view.getBottom()返回的值非常大而彈開。
在代碼中加入這一行:

Log.i("YourAppName", "view.getBottom(): -" + view.getBottom()); 
view.animate() 
     .translationY(-view.getBottom()) 
     .alpha(0) 
     .setDuration(HEADER_HIDE_ANIM_DURATION) 
     .setInterpolator(new DecelerateInterpolator()); 

然後檢查你的日誌,看看是否值相同與否。

0

我已經在一個稍微不同的方式。請注意,我正在使用的通話需要SDK 19(KitKat),但仍可以使用ViewPropertyAnimatorCompat

我有一個FrameLayout,它具有標題視圖和主視圖,標題視圖在前面。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:fab="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <include layout="@layout/main_layout" android:id="@+id/main_layout" /> 
    <include layout="@layout/header_layout" android:id="@+id/header_layout" /> 
</FrameLayout> 

一旦視圖被測量(發佈一個可運行的onResume期間)予設定的主視圖的topPadding成爲報頭的高度。

在隱藏和顯示動畫中,我添加了一個更新偵聽器,並在其中更新了主視圖的頂部填充爲標題的高度+ Y上的轉換。

final View header = findViewById(R.id. header_layout);    
header.animate() 
.translationY(-header.getBottom()) 
.setDuration(200) 
.setInterpolator(new DecelerateInterpolator()) 
.setUpdateListener(new AnimatorUpdateListener() {   
    @Override 
    public void onAnimationUpdate(ValueAnimator animation) { 
     int top = (int) (header.getHeight()+header.getTranslationY()); 
     findViewById(R.id.main_view).setPadding(0, top, 0, 0); 
    } 
}); 

這使得它更平滑一些,因爲填充與翻譯一起被更新。

實際上,使用ViewPropertyAnimatorCompat設置AnimationListener不起作用。監聽器永遠不會被調用,所以爲了向下兼容,我選擇了這個解決方案,不優雅,但至少在預奇巧設備的工作:

final View mainView = findViewById(R.id.main_view); 
Runnable mainUpdateRunnable = new Runnable() {   
    @Override 
    public void run() { 
     int top = (int) (header.getHeight()+header.getTranslationY()); 
     mainView.setPadding(0, top, 0, 0); 
     if (mAnimatingHeader) { 
      mainView.post(this); 
     } 
    } 
}; 
mainView.post(mainUpdateRunnable); 

變量mAnimatingHeader使用AnimationListener(工作)更新