Android中自定义对话框片段的动画

时间:2016-04-03 20:50:00

标签: android android-animation android-dialogfragment android-overlay

为了实现动画弹出,我使用了扩展DialogFragment类的自定义DialogFragment。然后,我在xml中设计了叠加布局,并在加载弹出窗口期间添加了它。因为,我需要做动画,我设计了2个动画xml文件,并在styles.xml中添加了它,并在加载叠加层时添加了动画。

我在3个地方展示叠加动画。当我将它用作列表项中的一个组件并且当我将它与图像视图一起使用时,它显示完美地显示,即从蜜蜂图标生长并在蜜蜂图标中消失。但是当我将它与VideoView或Exoplayer View一起使用时,动画的起点向左移动到x轴并向上移动到Y轴。

此外,使用以下三星Note Edge代码可以完美地弹出动画,但会导致Samsung Galaxy S5和Nexus 4的上述问题。

它也可以正常工作,即当我不使用动画时,叠加层位于蜜蜂适用于所有3个屏幕的确切位置。所以,我猜错误是在动画中。

我写的是示例代码。

自定义对话框片段:

public class ConfirmBox extends DialogFragment {
private View source;
private RelativeLayout relLayout;

public ConfirmBox() {
}

public ConfirmBox(View source) {
    this.source = source;
}

public ConfirmBox(View source,RelativeLayout relLayout) {
    this.source = source;
    this.relLayout =relLayout;
}

public static ConfirmBox newInstance(View source) {
    return new ConfirmBox(source);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setStyle(STYLE_NO_TITLE, 0);
}


@Override
public void onStart() {
    super.onStart();

    // Less dimmed background; see http://stackoverflow.com/q/13822842/56285
    Window window = getDialog().getWindow();
    WindowManager.LayoutParams params = window.getAttributes();
    params.dimAmount = 0.2f; // dim only a little bit
    window.setAttributes(params);

    // Transparent background; see http://stackoverflow.com/q/15007272/56285
    // (Needed to make dialog's alpha shadow look good)
    window.setBackgroundDrawableResource(android.R.color.transparent);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Put your dialog layout in R.layout.view_confirm_box
    final View view = inflater.inflate(R.layout.overlay_view, container, false);

    RelativeLayout layout = (RelativeLayout) view.findViewById(R.id.overlayLayout);


    layout.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View arg0) {

            getDialog().dismiss();

        }

    });

    setDialogPosition();

    return view;
}

/**
 * Try to position this dialog next to "source" view
 */
private void setDialogPosition() {
    if (source == null) {
        return; // Leave the dialog in default position
    }


    // Find out location of source component on screen
    // see http://stackoverflow.com/a/6798093/56285
    int[] location = new int[2];
    source.getLocationOnScreen(location);
    int sourceX = location[0];
    int sourceY = location[1];

    Log.d("CONFIRM BOX:", "X: " + sourceX + "Y: " + sourceY);
    Window window = getDialog().getWindow();

    window.getAttributes().windowAnimations = R.style.PauseDialogAnimation;

    // set "origin" to top left corner
    window.setGravity(Gravity.TOP| Gravity.LEFT);

    WindowManager.LayoutParams params = window.getAttributes();

    params.x = sourceX;
    params.y = sourceY - dpToPx(25);

    Log.d("CONFIRM BOX:","X: "+ sourceX + "Y: "+ sourceY);
    Log.d("CONFIRM BOX:","-----------------------------------------------------------");
    window.setAttributes(params);

    // Show the animation of the DialogueFragment
    window.getAttributes().windowAnimations = R.style.PauseDialogAnimation;

}

public int dpToPx(float valueInDp) {
    DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, valueInDp, metrics);
}
}

anim_in.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
    android:interpolator="@android:anim/linear_interpolator"
    android:fromXScale="0.0"
    android:toXScale="1.0"
    android:fromYScale="0.0"
    android:toYScale="1.0"
    android:fillAfter="false"
    android:startOffset="100"
    android:duration="200"
    android:pivotX = "5%"
    android:pivotY = "-40%"
    />
<translate
    android:fromYDelta="50%"
    android:toYDelta="0"
    android:startOffset="100"
    android:duration="200"
    />
</set>

anim_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
    android:interpolator="@android:anim/linear_interpolator"
    android:fromXScale="1.0"
    android:toXScale="0.0"
    android:fromYScale="1.0"
    android:toYScale="0.0"
    android:fillAfter="false"
    android:duration="200"
    android:pivotX = "5%"
    android:pivotY = "-40%"
    />
<translate
    android:fromYDelta="0"
    android:toYDelta="50%"
    android:duration="200"
    />
</set>

styles.xml:

<style name="PauseDialogAnimation">
    <item name="android:windowEnterAnimation">@anim/anim_in</item>
    <item name="android:windowExitAnimation">@anim/anim_out</item>
</style>

有人可以帮我解决这个问题吗?

感谢。

0 个答案:

没有答案