我在视图上实现了这样的扩展动画:
@Override
protected void applyTransformation(float interpolatedTime,
Transformation t) {
LayoutParams lp =
(LayoutParams) view.getLayoutParams();
if(rightAnimation){
lp.width = (int) (mStartWidth + mDeltaWidth *
interpolatedTime);
view.setLayoutParams(lp);
}else{
}
}
有了这个动画,我的视图会扩大和缩小。
我想做的是将此动画也实现到另一侧(到 -x 侧)。但是我有点困惑,因为缩小宽度是行不通的。(因为不允许负宽度或高度)
有谁知道在左侧(-x)或向上(-y)侧实现扩展动画的更好方法?
或者也许镜像视图?
最后我实现了制作左侧扩展动画。这是代码:
private class ExpandAnimation extends Animation implements Animation.AnimationListener {
private final int mStartWidth;
private final int mDeltaWidth;
int delta;
public ExpandAnimation(int startWidth, int endWidth) {
mStartWidth = startWidth;
mDeltaWidth = endWidth - startWidth;
RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) getLayoutParams();
delta = param.width;
setAnimationListener(this);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) getLayoutParams();
delta = param.width;
if(param.width <= 0){
delta = 0;
}
param.width = (int) (mStartWidth + mDeltaWidth *
interpolatedTime);
if(delta != 0){
delta = (int) (mStartWidth + mDeltaWidth *
interpolatedTime) - delta;
}
param.leftMargin -=delta;
setLayoutParams(param);
}
@Override
public boolean willChangeBounds() {
return true;
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
setText(text);
requestLayout();
mExpanded = !mExpanded;
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
出于我不知道的原因,宽度在开始时为 -2。所以我通过 if 过滤掉它。应用转换方法即可完成这项工作。