View中的 requestLayout(); invalidate();



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;
		}
	}
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值