1

To achieve a animated pop up, I have used a custom DialogFragment extending the DialogFragment class. Then, I designed the overlay layout in xml and added that during loading of the pop up. As, I needed to do the animation, I designed 2 animation xml files and added that in styles.xml and added the animation during the loading of the overlay.

I am showing the overlay animation in 3 places. It shows perfectly i.e. grows from the Bee icon and disappears in the bee icon when I use it as a component in a list item and when I use it along with a image view. But when I use it along with a VideoView or Exoplayer View the starting point of the animation shifts to the left for the x axis and goes up for the Y axis.

Also, the the pop up along with the animation works perfectly using the below code for Samsung Note Edge but causes the above problem for Samsung Galaxy S5 and Nexus 4.

It also works fine i.e. the overlay is at the exact position where the bee is for all 3 screens when I do not use the animation. So, I guess the error is in the animation.

I am giving sample code, I wrote.

Custom Dialog Fragment:

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>

Can someone kindly help me on zeroing in on the problem here?

Thanks.

user2284140
  • 197
  • 1
  • 4
  • 18

0 Answers0