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;
}
}
}
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;
}
}
}
相关文章推荐
- View 中的 requestLayout(); invalidate();
- Android View重绘和更新: invalidate和requestLayout
- android自定义View (五)view.requestLayout() 与 invalidate()
- Android View重绘和更新: invalidate和requestLayout
- android View中的 requestLayout() 和 invalidate() 原理以及流程
- View[1] requestLayout()、invalidate()
- Android View 重绘和更新invalidate 和requestLayout
- 深入理解 Android 之 View 的绘制流程(五)_invalidate,postInvalidate和requestLayout
- 从源码上深度剖析View invalidate 、 postInvalidate、requestLayout
- Android View重绘和更新: invalidate和requestLayout
- Android中View绘制流程以及invalidate()、requestLayout()等相关方法分析
- Android中View绘制流程以及invalidate()等相关方法分析
- 一图了解View的生命周期,自定义view,invalidate()和requestLayout()调用
- Android view中的requestLayout和invalidate方法
- Android View刷新原理Invalidate()和PostInvalidate()源码分析
- Android View 深度分析requestLayout、invalidate与postInvalidate
- Android中View绘制流程以及invalidate()等相关方法分析
- Android中View绘制流程以及invalidate()等相关方法分析
- view.invalidate()原理
- Android中View绘制流程以及invalidate()等相关方法分析