2

我正在为活动中的视图运行从屏幕顶部到底部的过渡动画。它位于屏幕标题视图的顶部。如何仅在视图中(仅针对子级)或从特定的 Y 或 X 位置应用动画?

我正在使用下面的代码

XML 代码

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromYDelta="-100%p" android:toYDelta="0" android:duration="500" />
</set>

Java 代码

Animation in = AnimationUtils.loadAnimation(_activity, R.anim.in_from_top);
view.setAnimation(in);

现在这个视图来自屏幕顶部。我希望动画在特定的 X、Y 点而不是屏幕顶部开始。动画视图位于 Activity 标题之上,根据我的要求,这是一个缺陷。

4

3 回答 3

1

如果您已将两个孩子添加到单亲,则会出现此问题。将子项添加到另一个布局,然后将此布局添加到实际布局。这将解决您的问题。

于 2012-10-09T17:13:53.310 回答
0

您只能从 java 代码中尝试动画

Animation animation1=new TranslateAnimation(0.0f, 0.0f, 10.0f, 250.0f);
animation1.setDuration(5000);
view.startAnimation(animation1);

TranslateAnimation(fromX, toX, fromY, toY)你可以像我在上面的代码中所做的那样设置你的 X 和 Y 坐标。

于 2012-10-10T20:30:47.313 回答
0

您将需要创建一个自定义动画类,如下所示:

public class ExpandAnimation extends Animation {
private View mAnimatedView;
private LayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mIsVisibleAfter = false;
private boolean mWasEndedAlready = false;

/**
 * Initialize the animation
 * 
 * @param view
 *            The layout we want to animate
 * 
 * @param duration
 *            The duration of the animation, in ms
 */
public ExpandAnimation(View view, int duration) {
    setDuration(duration);
    mAnimatedView = view;
    mViewLayoutParams = (LayoutParams) view.getLayoutParams();
    mIsVisibleAfter = (mViewLayoutParams.bottomMargin == 0);
    mMarginStart = mViewLayoutParams.bottomMargin;
    mMarginEnd = (mMarginStart == 0 ? (0 - view.getHeight()) : 0);
    view.setVisibility(View.VISIBLE);
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
    super.applyTransformation(interpolatedTime, t);

    if (interpolatedTime < 0.5f) {
        mViewLayoutParams.bottomMargin = mMarginStart + (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
        mAnimatedView.requestLayout();
    } else if (!mWasEndedAlready) {
        mViewLayoutParams.bottomMargin = mMarginEnd;
        mAnimatedView.requestLayout();

        if (mIsVisibleAfter) {
            mAnimatedView.setVisibility(View.GONE);
        }
        mWasEndedAlready = true;
    }
}

}

然后将此动画应用于所需的 x,y 坐标。

假设您在某个 x,y 坐标处有一个按钮,单击它时我们会为视图设置动画,然后将其向后滚动。你将不得不做这样的事情:

 private View previous = null;
 private void doTransformation() {
    if (previous != null) {
        ((LinearLayout.LayoutParams) previous.getLayoutParams()).bottomMargin = -200;
        ExpandAnimation anim = new ExpandAnimation(previous, 300);
        previous.startAnimation(anim);
        previous = null;
    } else {
        View yourlayout= findViewById(R.id.your_layout);
        ExpandAnimation anim = new ExpandAnimation(yourLayout, 300);
        detailLayout.startAnimation(anim);
        previous = yourLayout;
    }
}
于 2012-10-08T07:48:20.603 回答