动画的Android RelativeLayout的变化幅度 [英] Animate change width of android RelativeLayout
本文介绍了动画的Android RelativeLayout的变化幅度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从0改变RelativeLayout的宽度为600px的编程方式,以动画的方式。我用下面的code:
I need to change the width of a RelativeLayout from 0 to 600px programmatically, in an animated way. I'm using the following code:
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
RelativeLayout.LayoutParams drawerParams = (LayoutParams) drawer.getLayoutParams();
drawerParams.width = 600;
drawer.setLayoutParams(drawerParams);
}
};
a.setDuration(1000); //Animate for 1s
layout.startAnimation(a);
然而,由于某种原因,这是行不通的。任何人都可以点我朝着正确的方向?
However, for some reason this isn't working. Can anyone point me in the right direction?
推荐答案
我刚刚输入这和完美的作品在这里
I just typed this up and works perfect here
动画
public class ResizeWidthAnimation extends Animation
{
private int mWidth;
private int mStartWidth;
private View mView;
public ResizeWidthAnimation(View view, int width)
{
mView = view;
mWidth = width;
mStartWidth = view.getWidth();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
int newWidth = mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime);
mView.getLayoutParams().width = newWidth;
mView.requestLayout();
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
public boolean willChangeBounds()
{
return true;
}
}
使用
if(animate)
{
ResizeWidthAnimation anim = new ResizeWidthAnimation(leftFrame, leftFragmentWidthPx);
anim.setDuration(500);
leftFrame.startAnimation(anim);
}
else
{
this.leftFragmentWidthPx = leftFragmentWidthPx;
LayoutParams lp = (LayoutParams) leftFrame.getLayoutParams();
lp.width = leftFragmentWidthPx;
leftFrame.setLayoutParams(lp);
}
这篇关于动画的Android RelativeLayout的变化幅度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文