ScrollView中包含一个高度远超屏幕的LinearLayout,可以正常滚动。当使用ObjectAnimator把ScrollView向上平移100个px,在updateLisnter中重设ScrollView的高度后,Scrollview没有完全显示,也无法滚动,这是什么原因呢?
代码:
ll_wrap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(scrollview, "translationY", -100f);
objectAnimator.setDuration(500);
objectAnimator.start();
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
//重设ScrollView高度
ViewGroup.LayoutParams params = scrollview.getLayoutParams();
params.height = (int) (scrollview.getHeight() - (float) valueAnimator.getAnimatedValue());
scrollview.setLayoutParams(params);
}
});
}
});
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ll_wrap"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="400dp">
</FrameLayout>
<FrameLayout
android:background="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="400dp">
</FrameLayout>
</LinearLayout>
</ScrollView>
阿神2017-04-18 09:15:07
首先,很遺憾的告訴你,你的屬性動畫方法弄錯了,translationY
这个属性View
有,也就是所有的View及其子类都会有,你设置了属性名称,那么它会自动去调用对应的Set方法
,比如,你现在这样写,那么它会自动调用View.setTranslationY(float translationY)
方法,所以你無需再執行下面的程式碼:
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
ViewGroup.LayoutParams params = scrollview.getLayoutParams();
params.height = (int) (params.height + (float)valueAnimator.getAnimatedValue());
scrollview.setLayoutParams(params);
}
});
其次還有個問題,LayoutParams.height 對應的有
public static final int FILL_PARENT = -1;
/**
* Special value for the height or width requested by a View.
* MATCH_PARENT means that the view wants to be as big as its parent,
* minus the parent's padding, if any. Introduced in API Level 8.
*/
public static final int MATCH_PARENT = -1;
/**
* Special value for the height or width requested by a View.
* WRAP_CONTENT means that the view wants to be just large enough to fit
* its own internal content, taking its own padding into account.
*/
public static final int WRAP_CONTENT = -2;
你的程式碼執行起來後,設定的LayoutParams.height
<-2,這很明顯是不對的,一般都是大於0的.
刪掉這段程式碼區塊後,如果你希望每次點擊都網路上平移100px,那麼你可以這樣寫:
ll_wrap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
float curTranslationY = scrollview.getTranslationY();
Log.e("test","curTranslationY: "+curTranslationY);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(scrollview, "translationY", curTranslationY,curTranslationY-100f);
objectAnimator.setDuration(500);
objectAnimator.start();
}
});