【问题标题】:Animate a Fragment so that it is partially offscreen为片段设置动画,使其部分在屏幕外
【发布时间】:2012-07-30 08:52:16
【问题描述】:

我想在出现新片段时部分隐藏片段。

我正在 GitHub 上的 ScienceGuy 代码上构建,如 Fragment Animation like Gmail Honeycomb App 中所引用的那样

我从三个片段开始。最左边的权重为 6,中间的权重为 4,最右边的权重为 4。将 weightSum 设置为 10 会使左侧和中间的片段出现,而最右侧则在屏幕外。这就是我想要的。

当用户选择中间片段中的项目时,我想转换左侧片段,使其三分之二离开屏幕左侧。由于权重,我实际上会得到 2, 4, 4。此时,右侧片段已完全移动到屏幕上。

使用 ScienceGuy 的代码,当左侧片段滚动关闭时,我可以让右侧片段出现,但左侧片段完全消失。

我已尝试使用以下代码:

final float middleFragmentWidth = getMiddleFragment().getView().getWidth();
ObjectAnimator animIn = ObjectAnimator.ofFloat(null, "x",
        -middleFragmentWidth/2, 0).setDuration(
        trans.getDuration(LayoutTransition.APPEARING));
trans.setAnimator(LayoutTransition.APPEARING, animIn);


ObjectAnimator animOut = ObjectAnimator.ofFloat(null, "x", -middleFragmentWidth,
        -middleFragmentWidth/2).setDuration(
            trans.getDuration(LayoutTransition.CHANGE_DISAPPEARING));
trans.setAnimator(LayoutTransition.DISAPPEARING, animOut);

我怀疑 Android 希望在“隐藏”片段时将片段完全隐藏。有没有办法为片段设置动画,使它们实际上不是隐藏,而是部分在屏幕外?

或者,我可以将三个片段放入一个 Horizo​​ntalScrollView,然后尝试对其进行动画处理。但是,我没有这样做,因为中间和最右边的片段包含列表视图,并且要避免在 Horizo​​ntalScrollView 中使用列表视图according to the docs

【问题讨论】:

  • 嗨,我也面临同样的问题。你解决了吗?
  • 我结束了基于 LinearLayout 创建自定义视图。我会用一个例子来回答我的问题。

标签: android android-fragments android-animation


【解决方案1】:

我最终创建了一个基于 LinearLayout 的自定义视图,它是设备宽度的 150%。

public class AutoSizingLinearLayout extends LinearLayout {
    private static final String TAG = AutoSizingLinearLayout.class.getSimpleName();
    final int myWidth;

public AutoSizingLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    myWidth = setScaledWidth(context);
}

public AutoSizingLinearLayout(Context context) {
    super(context);
    myWidth = setScaledWidth(context);
}

private int setScaledWidth(Context context) {
    if (this.isInEditMode()){
        return 1000;
    } else {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        return (int) (1.5 * wm.getDefaultDisplay().getWidth());
    }
}

@Override
public android.view.ViewGroup.LayoutParams getLayoutParams() {
    android.view.ViewGroup.LayoutParams params = super.getLayoutParams();
    params.width = myWidth;
    return params;
}

然后我像这样为它制作动画。

                    ObjectAnimator a = ObjectAnimator.ofFloat(linearView,"translationX", -scrollamount).setDuration(900);
                a.start();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多