Android自动伸展动画,Android:展开/折叠动画

本文介绍了如何在Android中实现视图的展开和折叠动画,包括使用Animation、ValueAnimator和布局参数的变化来平滑地控制视图高度的动态调整。通过示例代码展示了不同方法,如使用Handler延迟执行,以及使用Interpolator增加动画的平滑性。
摘要由CSDN通过智能技术生成

90a2f01ed2c41911f00a94b50e6bff83.png

赞同来自:

你走在正确的轨道上。确保在动画开始之前将v1设置为布局高度为零。您希望在开始动画之前将设置初始化为动画的第一帧。

对于平滑动画,请使用Handler with run method .....并享受展开/折叠动画

class AnimUtils{

public void expand(final View v) {

int ANIMATION_DURATION=500;//in milisecond

v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

final int targtetHeight = v.getMeasuredHeight();

v.getLayoutParams().height = 0;

v.setVisibility(View.VISIBLE);

Animation a = new Animation()

{

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

v.getLayoutParams().height = interpolatedTime == 1

? LayoutParams.WRAP_CONTENT

: (int)(targtetHeight * interpolatedTime);

v.requestLayout();

}

@Override

public boolean willChangeBounds() {

return true;

}

};

// 1dp/ms

a.setDuration(ANIMATION_DURATION);

// a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));

v.startAnimation(a);

}

public void collapse(final View v) {

final int initialHeight = v.getMeasuredHeight();

Animation a = new Animation()

{

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

if(interpolatedTime == 1){

v.setVisibility(View.GONE);

}else{

v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);

v.requestLayout();

}

}

@Override

public boolean willChangeBounds() {

return true;

}

};

// 1dp/ms

a.setDuration(ANIMATION_DURATION);

// a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));

v.startAnimation(a);

}}

并使用此代码调用:

private void setAnimationOnView(final View inactive ) {

//I am applying expand and collapse on this TextView ...You can use your view

//for expand animation

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

new AnimationUtililty().expand(inactive);

}

}, 1000);

//For collapse

new Handler().postDelayed(new Runnable() {

@Override

public void run() {

new AnimationUtililty().collapse(inactive);

//inactive.setVisibility(View.GONE);

}

}, 8000);

}其他解决方案是:

public void expandOrCollapse(final View v,String exp_or_colpse) {

TranslateAnimation anim = null;

if(exp_or_colpse.equals("expand"))

{

anim = new TranslateAnimation(0.0f, 0.0f, -v.getHeight(), 0.0f);

v.setVisibility(View.VISIBLE);

}

else{

anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, -v.getHeight());

AnimationListener collapselistener= new AnimationListener() {

@Override

public void onAnimationStart(Animation animation) {

}

@Override

public void onAnimationRepeat(Animation animation) {

}

@Override

public void onAnimationEnd(Animation animation) {

v.setVisibility(View.GONE);

}

};

anim.setAnimationListener(collapselistener);

}

// To Collapse

//

anim.setDuration(300);

anim.setInterpolator(new AccelerateInterpolator(0.5f));

v.startAnimation(anim);

}

1f814faa272bb68cb51d11f917fa65b7.png

赞同来自:

droidQuery非常简单。对于开始,请考虑以下布局:

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="vertical" >

android:id="@+id/v1"

android:layout_width="wrap_content"

android:layout_height="wrap_content" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="View 1" />

android:id="@+id/v2"

android:layout_width="wrap_content"

android:layout_height="0dp" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="View 2" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="View 3" />

我们可以使用以下代码将高度设置为所需的值 - 例如100dp:

//convert 100dp to pixel value

int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());然后使用droidQuery进行动画处理。最简单的方法是:

$.animate("{ height: " + height + "}", new AnimationOptions());要使动画更具吸引力,请考虑添加缓动:

$.animate("{ height: " + height + "}", new AnimationOptions().easing($.Easing.BOUNCE));您还可以使用duration()方法更改AnimationOptions的持续时间,或处理动画结束时发生的情况。有关复杂示例,请尝试:

$.animate("{ height: " + height + "}", new AnimationOptions().easing($.Easing.BOUNCE)

.duration(1000)

.complete(new Function() {

@Override

public void invoke($ d, Object... args) {

$.toast(context, "finished", Toast.LENGTH_SHORT);

}

}));

44d605d99223e8345b613f6e16cecb3f.png

赞同来自:

我创建了一个版本,您无需指定布局高度,因此使用起来更简单,更清晰。解决方案是在动画的第一帧中获得高度(当时可用,至少在我的测试期间)。这样,您可以提供具有任意高度和底部边距的视图。

构造函数中还有一个小错误 - 底部边距设置为-10000,以便视图在转换之前保持隐藏(防止闪烁)。

public class ExpandAnimation extends Animation {

private View mAnimatedView;

private ViewGroup.MarginLayoutParams mViewLayoutParams;

private int mMarginStart, mMarginEnd;

public ExpandAnimation(View view) {

mAnimatedView = view;

mViewLayoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();

mMarginEnd = mViewLayoutParams.bottomMargin;

mMarginStart = -10000; //hide before viewing by settings very high negative bottom margin (hack, but works nicely)

mViewLayoutParams.bottomMargin = mMarginStart;

mAnimatedView.setLayoutParams(mViewLayoutParams);

}

@Override

protected void applyTransformation(float interpolatedTime, Transformation t) {

super.applyTransformation(interpolatedTime, t);

//view height is already known when the animation starts

if(interpolatedTime==0){

mMarginStart = -mAnimatedView.getHeight();

}

mViewLayoutParams.bottomMargin = (int)((mMarginEnd-mMarginStart) * interpolatedTime)+mMarginStart;

mAnimatedView.setLayoutParams(mViewLayoutParams);

}

}

2ead56aff3c03ce3c8685a4e71f67f6b.png

赞同来自:

// http://easings.net/

Interpolator easeInOutQuart = PathInterpolatorCompat.create(0.77f, 0f, 0.175f, 1f);

public static Animation expand(final View view) {

int matchParentMeasureSpec = View.MeasureSpec.makeMeasureSpec(((View) view.getParent()).getWidth(), View.MeasureSpec.EXACTLY);

int wrapContentMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.U

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值