Forked from MadBrowser/CustomDialogFragment.java
Created
December 13, 2020 04:01
Star
You must be signed in to star a gist
Loading dialog that shows a flip animation using a random list of icons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<!-- Before rotating, immediately set the alpha to 0. --> | |
<objectAnimator | |
android:valueFrom="1.0" | |
android:valueTo="0.0" | |
android:propertyName="alpha" | |
android:duration="0" /> | |
<!-- Rotate. --> | |
<objectAnimator | |
android:valueFrom="180" | |
android:valueTo="0" | |
android:propertyName="rotationY" | |
android:interpolator="@android:interpolator/accelerate_decelerate" | |
android:duration="@integer/card_flip_time_full" /> | |
<!-- Half-way through the rotation (see startOffset), set the alpha to 1. --> | |
<objectAnimator | |
android:valueFrom="0.0" | |
android:valueTo="1.0" | |
android:propertyName="alpha" | |
android:startOffset="@integer/card_flip_time_half" | |
android:duration="1" /> | |
</set> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<!-- Rotate. --> | |
<objectAnimator | |
android:valueFrom="0" | |
android:valueTo="-180" | |
android:propertyName="rotationY" | |
android:interpolator="@android:interpolator/accelerate_decelerate" | |
android:duration="@integer/card_flip_time_full" /> | |
<!-- Half-way through the rotation (see startOffset), set the alpha to 0. --> | |
<objectAnimator | |
android:valueFrom="1.0" | |
android:valueTo="0.0" | |
android:propertyName="alpha" | |
android:startOffset="@integer/card_flip_time_half" | |
android:duration="1" /> | |
</set> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.animationsdemo; | |
import android.animation.Animator; | |
import android.animation.AnimatorInflater; | |
import android.animation.AnimatorListenerAdapter; | |
import android.animation.AnimatorSet; | |
import android.app.Dialog; | |
import android.app.DialogFragment; | |
import android.content.DialogInterface; | |
import android.os.Bundle; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.Window; | |
import android.widget.ImageView; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.List; | |
public class CustomDialogFragment extends DialogFragment { | |
/** | |
* ImageViews for the animation (front, back) | |
*/ | |
private ImageView vImageFront; | |
private ImageView vImageBack; | |
/** | |
* Flag for continuous animation | |
*/ | |
private boolean mContinueAnimation; | |
/** | |
* Flag for displayed face | |
*/ | |
private boolean mFrontVisible; | |
/** | |
* Array with Image resources (this implementation assumes that we have at least two resources) | |
*/ | |
private List<Integer> mImageResources = new ArrayList<>( | |
Arrays.asList( | |
R.drawable.bearish, | |
R.drawable.card_in_use, | |
R.drawable.money_bag, | |
R.drawable.self_service_kiosk, | |
R.drawable.shop, | |
R.drawable.shopping_cart | |
) | |
); | |
/** | |
* Iterator over the drawable list | |
*/ | |
private Iterator<Integer> mImagesIterator; | |
/** | |
* Current drawable id | |
*/ | |
private int mCurrentDrawable; | |
public CustomDialogFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
View v = inflater.inflate(R.layout.fragment_custom_dialog, container, false); | |
vImageFront = (ImageView) v.findViewById(R.id.image_front); | |
vImageBack = (ImageView) v.findViewById(R.id.image_back); | |
mContinueAnimation = true; | |
setUpIterator(); | |
vImageFront.setImageResource(mImagesIterator.next()); | |
vImageBack.setImageResource(mImagesIterator.next()); | |
startFlipAnimation(vImageBack, vImageFront); | |
// Inflate the layout for this fragment | |
return v; | |
} | |
/** | |
* The system calls this only when creating the layout in a dialog. | |
*/ | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
// The only reason you might override this method when using onCreateView() is | |
// to modify any dialog characteristics. For example, the dialog includes a | |
// title by default, but your custom layout might not need it. So here you can | |
// remove the dialog title, but you must call the superclass to get the Dialog. | |
Dialog dialog = super.onCreateDialog(savedInstanceState); | |
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); | |
return dialog; | |
} | |
@Override | |
public void onDismiss(DialogInterface dialog) { | |
this.mContinueAnimation = false; | |
super.onDismiss(dialog); | |
} | |
private AnimatorSet getShowAnimation() { | |
return (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(), | |
R.animator.card_flip_right_in); | |
} | |
private AnimatorSet getHideAnimation() { | |
return (AnimatorSet) AnimatorInflater.loadAnimator(getActivity(), | |
R.animator.card_flip_right_out); | |
} | |
private void startFlipAnimation(Object A, Object B) { | |
if(!mContinueAnimation) { | |
return; | |
} | |
AnimatorSet showAnimation = getShowAnimation(); | |
showAnimation.setTarget(A); | |
AnimatorSet hideAnimation = getHideAnimation(); | |
hideAnimation.setTarget(B); | |
AnimatorSet flipAnimation = new AnimatorSet(); | |
flipAnimation.playTogether(showAnimation, hideAnimation); | |
flipAnimation.addListener(new AnimatorListenerAdapter() { | |
@Override | |
public void onAnimationEnd(Animator animation) { | |
super.onAnimationEnd(animation); | |
mCurrentDrawable = mImagesIterator.next(); | |
if (mFrontVisible) { | |
vImageBack.setImageResource(mCurrentDrawable); | |
startFlipAnimation(vImageBack, vImageFront); | |
} else { | |
vImageFront.setImageResource(mCurrentDrawable); | |
startFlipAnimation(vImageFront, vImageBack); | |
} | |
if (!mImagesIterator.hasNext()) { | |
setUpIterator(); | |
} | |
mFrontVisible = !mFrontVisible; | |
} | |
}); | |
flipAnimation.start(); | |
} | |
private void setUpIterator() { | |
Collections.shuffle(mImageResources); | |
this.mImagesIterator = mImageResources.iterator(); | |
// Prevents the same image is displayed twice in a row | |
if(mCurrentDrawable == mImageResources.get(0)) { // If next image is the same as current | |
this.mImagesIterator.next(); // Advance the iterator | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.android.animationsdemo; | |
import android.app.Activity; | |
import android.app.FragmentManager; | |
import android.os.Bundle; | |
import android.view.View; | |
public class DialogFlipActivity extends Activity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_dialog_flip); | |
findViewById(R.id.text).setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
showDialog(); | |
} | |
}); | |
} | |
private void showDialog() { | |
FragmentManager fragmentManager = getFragmentManager(); | |
CustomDialogFragment newFragment = new CustomDialogFragment(); | |
newFragment.show(fragmentManager, "DialogFragment"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<integer name="card_flip_time_full">1000</integer> | |
<integer name="card_flip_time_half">500</integer> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment