您的位置:首页 > 产品设计 > UI/UE

View中的 requestLayout(); invalidate();

2016-09-22 23:27 417 查看
View.requestLayout() 请求重新布局,重新调用:onMeasure,onLayout,onDraw;

View.invalidate()        刷新视图,相当于调用View.onDraw()方法

举例 : 
ExpandableListView 的一个伸缩动画 :

package com.example.expanddemo;

import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class ExpandAnimation extends Animation {

private View view;
private LinearLayout.LayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mIsVisibleAfter = false;
private boolean mWasEndedAlready = false;

public ExpandAnimation(View view, long duration) {
setDuration(duration);
this.view = view;
mViewLayoutParams = (LayoutParams) view.getLayoutParams();
mIsVisibleAfter = (mViewLayoutParams.bottomMargin == 0);
mMarginStart = mViewLayoutParams.bottomMargin;
view.measure(0, 0);
mMarginEnd = (mMarginStart == 0) ? (0 - view.getMeasuredHeight()) : 0;
// mMarginEnd = mMarginStart - view.getMeasuredHeight();
view.setVisibility(View.VISIBLE);
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
Log.i("info", "mMarginStart-->>" + mMarginStart + ", mMarginEnd-->>" + mMarginEnd + ", interpolatedTime-->>" + interpolatedTime);
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1f) {
mViewLayoutParams.bottomMargin = mMarginStart + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
view.requestLayout();
} else if (!mWasEndedAlready) {
mViewLayoutParams.bottomMargin = mMarginEnd;
view.requestLayout();
if (mIsVisibleAfter)
view.setVisibility(View.GONE);
mWasEndedAlready = true;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: