Last active
September 11, 2015 11:34
-
-
Save andyb129/355cbbd3681eea25d10b to your computer and use it in GitHub Desktop.
AnimActionBarFragment
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
import android.content.Context; | |
import android.graphics.drawable.Drawable; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.animation.Animation; | |
import android.view.animation.AnimationUtils; | |
import android.widget.ImageView; | |
import android.widget.Toast; | |
/** | |
* Fragment for adding extra custom action bar icons to an activity (nothing is displayed for this fragment). | |
* | |
* Remember this is a UIless fragment, it is attached via a tag to the activity not to content :s | |
* | |
*/ | |
public class AnimActionFragment extends Fragment { | |
public static final String TAG = AnimActionFragment.class.getSimpleName(); | |
private LayoutInflater mInflater; | |
private Menu mMenu; | |
public static AnimActionFragment newInstance(String param1, String param2) { | |
AnimActionFragment fragment = new AnimActionFragment(); | |
return fragment; | |
} | |
public AnimActionFragment() { | |
// Required empty public constructor | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setHasOptionsMenu(true); | |
mInflater = LayoutInflater.from(getActivity()); | |
} | |
@Override | |
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { | |
inflater.inflate(R.menu.action_anim, menu); | |
mMenu = menu; | |
// set default icon if passed in - not used at the mo | |
/*if (mCurrentDrawableId != 0) { | |
changeActionBarIcon(getActivity(), menu.findItem(R.id.action_bar_button_show), mCurrentDrawableId, false, 0); | |
}*/ | |
super.onCreateOptionsMenu(menu, inflater); | |
} | |
@Override | |
public boolean onOptionsItemSelected(final MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.action_bar_button_android: | |
Toast.makeText(getActivity(), "android action button tapped!", 3000).show(); | |
animate(); | |
return true; | |
default: | |
return super.onOptionsItemSelected(item); | |
} | |
} | |
private void animate() { | |
changeActionBarIcon(getActivity(), mMenu.findItem(R.id.action_bar_button_android), -1, true, R.anim.pull_down); | |
} | |
/** | |
* method to change the an action bar button icon | |
* | |
* @param context | |
* @param actionBarButtonToChange | |
* @param newDrawableResId | |
* @param animate | |
* @param animResId | |
*/ | |
public void changeActionBarIcon(final Context context, final MenuItem actionBarButtonToChange, final int newDrawableResId, final boolean animate, | |
final int animResId) { | |
final Drawable newActionBarButtonDrawable; | |
if (newDrawableResId > 0) { | |
newActionBarButtonDrawable = getResources().getDrawable(newDrawableResId); | |
} else { | |
newActionBarButtonDrawable = actionBarButtonToChange.getIcon(); | |
} | |
if (animate && animResId != 0) { | |
final ImageView iv = (ImageView) mInflater.inflate(R.layout.element_custom_action_view, null); | |
iv.setImageDrawable(newActionBarButtonDrawable); | |
iv.clearAnimation(); | |
Animation anim = AnimationUtils.loadAnimation(getActivity(), animResId); | |
anim.setAnimationListener(new Animation.AnimationListener() { | |
@Override | |
public void onAnimationStart(final Animation animation) { | |
} | |
@Override | |
public void onAnimationRepeat(final Animation animation) { | |
} | |
@Override | |
public void onAnimationEnd(final Animation animation) { | |
// iv.clearAnimation(); | |
// added addition null checks, encase this is repeatedly run | |
if (actionBarButtonToChange != null) { | |
View actionView = actionBarButtonToChange.getActionView(); | |
if (actionView != null) { | |
actionView.clearAnimation(); | |
} | |
actionBarButtonToChange.setActionView(null); | |
actionBarButtonToChange.setIcon(newActionBarButtonDrawable); | |
} | |
} | |
}); | |
iv.startAnimation(anim); | |
actionBarButtonToChange.setActionView(iv); | |
} else { | |
if (newActionBarButtonDrawable != null) { | |
actionBarButtonToChange.setIcon(newActionBarButtonDrawable); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment