Android's Overflow Menu animation? - android

When I press the overflow menu in the action bar a dropdown menu pops down with an animation. I'm checking the sdk's res/anim folder and I would like to know which animation does this button use, so I can use it to pop down some other stuff when the user presses other buttons I have in the action bar.
Thanks!

As far as I know, on Android Api 23 the default dropdown animation is implemented by Transition. you can read PopupWindow.java's source code to know what was done and how to do.
you can find two xml files which define transitions for dropdown animtions at sdk's platforms/android-23/datas/res/transition directory.(popup_window_enter.xml, popup_window_exit.xml)
unfortunately, you can't refer it via android.R.transition.popup_window_enter.xml because it's hidden. but you can copy it in to your project and using it.
below there are some codes which I use in my project.
public static void showOverflow(final RelativeLayout holder, final View anchor, final CardView cv) {
holder.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideOverflow(holder, cv);
}
});
if (cv.getVisibility() != View.VISIBLE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
try {
final Rect episodeCenter = getTransitionEpicenter(anchor, cv);
Transition transition = TransitionInflater.from(holder.getContext()).inflateTransition(R.transition.popup_window_enter);
transition.setEpicenterCallback(new Transition.EpicenterCallback() {
#Override
public Rect onGetEpicenter(Transition transition) {
return episodeCenter;
}
});
TransitionManager.beginDelayedTransition(cv, transition);
} catch (Exception e) {
e.printStackTrace();
}
}
cv.setVisibility(View.VISIBLE);
}
}
private static Rect getTransitionEpicenter(View anchor, View popup) {
final int[] anchorLocation = new int[2];
final int[] popupLocation = new int[2];
anchor.getLocationOnScreen(anchorLocation);
popup.getLocationOnScreen(popupLocation);
final Rect bounds = new Rect(0, 0, anchor.getWidth(), anchor.getHeight());
bounds.offset(anchorLocation[0] - popupLocation[0], anchorLocation[1] - popupLocation[1]);
return bounds;
}

Related

Play ripple animation without user interaction

I know that I can set android:background="?android:attr/selectableItemBackground" to any View to get the nice ripple effect from Android Lollipop.This works well for views which are touched by the user. But what if I want to play the ripple animation without user interaction?
In my application I want to draw attention to a view by playing a ripple animation on this view. How can I do this? I do not found a way to get an Animatior object for the ripple animation. So how can I set a ripple animation to a view from code without user interaction?
Unfortunately no accessible method is available to play ripple animation. But I could think of two possible ways to achieve this
#1 Using reflection
public static void simulateButtonPress(final View view)
{
Drawable drawable = view.getBackground();
if( drawable instanceof RippleDrawable )
{
try
{
final RippleDrawable rd = ((RippleDrawable)drawable);
final Method setRippleActive = rd.getClass().getDeclaredMethod("setRippleActive", boolean.class);
setRippleActive.setAccessible(true);
setRippleActive.invoke(rd, true); //setRippleActive(true)
//exit ripple effect after 250 milliseconds
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
try
{
setRippleActive.invoke(rd, false); //setRippleActive(false)
}
catch (Exception e)
{
e.printStackTrace();
}
}
}, 250);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
#2 By simulating Motion Events ACTION_DOWN and ACTION_CANCEL
public static void simulateButtonPress(final View view)
{
final long now = SystemClock.uptimeMillis();
final MotionEvent pressEvent = MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN, view.getWidth()/2, view.getHeight()/2, 0);
view.dispatchTouchEvent(pressEvent);
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
final long now = SystemClock.uptimeMillis();
final MotionEvent cancelEvent = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, view.getWidth()/2, view.getHeight()/2, 0);
view.dispatchTouchEvent(cancelEvent);
}
}, 250);
}

Change actionbar alpha during scroll event

I try to create a fade alpha on my actionbar during a scroll event of my main ScrollView :
I tried this :
//Active Overlay for ActionBar before set my content in the activity
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
//create a new Drawable for the AB background
final ColorDrawable newColor = new ColorDrawable(getResources().getColor(R.color.primaryColor));
newColor.setAlpha(0);
getActionBar().setBackgroundDrawable(newColor);
//create a OnScrollListener to get ScrollY position
final ScrollView mainScrollView = (ScrollView) findViewById(R.id.place_detail_scrollView);
mainScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
final int scrollY = mainScrollView.getScrollY();
final int boundY = 300;
final float ratio = (float) (scrollY/boundY) ;
newColor.setAlpha((int)(ratio*255));
}
});
In theory, my actionbar will fade its alpha between position 0 and 300 of the ScrollView. Unfortunately, my AB still transparent everytime... Any idea ?
I found the problem, I use a pre Jelly Bean device so there's a workaround for this issue.
In fact, with pre-JELLY_BEAN_MR1 device, we have to attach the following Callback in the onCreate(Bundle) method for the Drawable :
private Drawable.Callback mDrawableCallback = new Drawable.Callback() {
#Override
public void invalidateDrawable(Drawable who) {
getActionBar().setBackgroundDrawable(who);
}
#Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
}
#Override
public void unscheduleDrawable(Drawable who, Runnable what) {
}
};
then set the callback
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
mActionBarBackgroundDrawable.setCallback(mDrawableCallback);
}
And that's all, thank you to Cyril Mottier for this !

Replace Icon with ProgressBar

I want there to be an icon, which when clicked on, will be replaced by a spinning progress bar. Once the appropriate task in the background has finished, the ProgressBar should be replaced by the icon again.
This is similar to the progress bar we all are used to in the action bar (e.g. as described here), but I want to achieve the same thing within a Fragment (dialog), so setActionView() is not available.
What would be the best way to tackle this?
So your next stop is ProgressButton by SundeepK (MIT License)
ProgressButton can be used to display a simple rotating Drawable to give the user
the effect of a loading button. The Drawable will be displayed once the user clicks the button and will have to be manually dismissed using the stopLoadingAnimation() method.
ProgressButton class:
public class ProgressButton extends ImageButton {
private boolean _shouldDisplayLoadingAnimation = false;
private Drawable _loadingAnimation;
private TextPaint _textPaint;
private Rect _textBounds;
private String _defaultText;
public ProgressButton(Context context_, AttributeSet attrs_, int defStyle_) {
super(context_, attrs_, defStyle_);
final TypedArray a = context_.obtainStyledAttributes(attrs_, R.styleable.ProgressButton,
R.attr.progressButtonStyle, R.style.ProgressButton_attrs);
this.setBackgroundColor(a.getInteger(R.styleable.ProgressButton_defaultBackgroundColor, Color.WHITE));
_loadingAnimation = getDrawable();
_loadingAnimation.setAlpha(0);
_defaultText = a.getString(R.styleable.ProgressButton_defaultText);
_textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
_textPaint.density = getResources().getDisplayMetrics().density;
_textPaint.setColor(a.getInteger(R.styleable.ProgressButton_defaultFontColor, Color.BLACK));
_textPaint.setTextAlign(Align.CENTER);
_textPaint.setTextSize(a.getInteger(R.styleable.ProgressButton_defaultFontSize, 40));
_textPaint.setFakeBoldText(true);
_textBounds = new Rect();
a.recycle();
}
public ProgressButton(Context context_, AttributeSet attrs_) {
this(context_, attrs_, 0);
}
public ProgressButton(Context context_) {
this(context_, null);
}
#Override
public boolean performClick() {
boolean isClicked = super.performClick();
if (isClicked) {
_shouldDisplayLoadingAnimation = true;
this.invalidate();
}
return isClicked;
};
public void startLoadingAnimation() {
_shouldDisplayLoadingAnimation = true;
this.invalidate();
}
public void stopLoadingAnimation() {
_shouldDisplayLoadingAnimation = false;
this.invalidate();
}
#Override
protected void onDraw(Canvas canvas_) {
if (_shouldDisplayLoadingAnimation) {
shouldShowAnimation(true);
} else {
_textPaint.getTextBounds(_defaultText, 0, _defaultText.length(), _textBounds);
canvas_.drawText( _defaultText , getWidth()/2, (getHeight()/2)+((_textBounds.bottom-_textBounds.top)/2) , _textPaint);
shouldShowAnimation(false);
_loadingAnimation.setVisible(false, false);
}
super.onDraw(canvas_);
}
private void shouldShowAnimation(boolean shouldShow_) {
if (_loadingAnimation instanceof Animatable) {
if (shouldShow_) {
_loadingAnimation.setAlpha(255);
((Animatable) _loadingAnimation).start();
} else {
_loadingAnimation.setAlpha(0);
((Animatable) _loadingAnimation).stop();
}
}
}
}
define ProgressButton in your layout:
<com.sun.progressbutton.ProgressButton
android:id="#+id/progressView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:padding="10dp"
android:src="#drawable/progress_view"
/>

How can I fire a trigger to click when using slider in android?

I am using Slider like gmail or Facbook in android this is fine. What i have done is i just created a new activity and given animation to it so it looks like slider. Now what i want is when user click any button in slider a new activity should open and perform task what i specify in it. But i am not getting how to pass click event in slider.
My code
public class PlayAudio extends Activity implements OnCompletionListener,
SeekBar.OnSeekBarChangeListener {
boolean alreadyShowing = false;
private int windowWidth;
private int windowHeight;
private PopupWindow fadePopup;
private Animation ta;
private RelativeLayout baseView;
LayoutInflater inflater;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
Display display = getWindowManager().getDefaultDisplay();
windowWidth = display.getWidth();
windowHeight = display.getHeight();
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
findViewById(R.id.imageView1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!alreadyShowing) {
alreadyShowing = true;
openSlidingMenu();
}
}
});
findViewById(R.id.imageView3).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!alreadyShowing) {
alreadyShowing = true;
openSlidingMenu();
}
}
});
}
private void openSlidingMenu() {
showFadePopup();
int width = (int) (windowWidth * 0.8f);
translateView((float) (width));
#SuppressWarnings("deprecation")
int height = LayoutParams.FILL_PARENT;
// creating a popup
final View layout = inflater.inflate(R.layout.option_popup_layout,
(ViewGroup) findViewById(R.id.popup_element));
final PopupWindow optionsPopup = new PopupWindow(layout, width, height,
true);
optionsPopup.setBackgroundDrawable(new PaintDrawable());
optionsPopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
optionsPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
public void onDismiss() {
// Removing the fade effect
fadePopup.dismiss();
// to clear the previous animation transition in
cleanUp();
// move the view out
translateView(0);
// to clear the latest animation transition out
cleanUp();
// resetting the variable
alreadyShowing = false;
}
});
}
private void showFadePopup() {
final View layout = inflater.inflate(R.layout.fadepopup,
(ViewGroup) findViewById(R.id.fadePopup));
fadePopup = new PopupWindow(layout, windowWidth, windowHeight, false);
fadePopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
}
private void translateView(float right) {
ta = new TranslateAnimation(0f, right, 0f, 0f);
ta.setDuration(100);
ta.setFillEnabled(true);
ta.setFillAfter(true);
baseView = (RelativeLayout) findViewById(R.id.Player_Slider);
baseView.startAnimation(ta);
baseView.setVisibility(View.VISIBLE);
}
private void cleanUp() {
if (null != baseView) {
baseView.clearAnimation();
baseView = null;
}
if (null != ta) {
ta.cancel();
ta = null;
}
fadePopup = null;
}
}
Here in click event i am calling openSlidingMenu function which open's new activity option_popup_layout (http://i.share.pho.to/fb125f6f_o.png)
Now what i want is when i click any of the image in option_popup_layout a new activity should open and i want perform some operation over there but i am not getting where i have to use click event to call new activity.
I have tried some piece of code to put click event inside creating function as well as other method too but not getting work.
Please help me to solve this. Thanks in advance.

Text color animation

Is there a way to animate a text color change (from anycolor to white)?
The only variant I came up with, is placing two textviews (with the same text) in one place, and fading the top one, so the bottom one (that has a white color) will become visible.
P.S. I scrapped the variant of the 2 TextViews since it looked weird (edges weren't smooth and, since I have a lot of such elements on the screen it was really lagging the scrolling). What I did, was a crazy hack that does the animation with the use of a Thread and setTextColor (that also forces redraw of a textview).
Since I needed only 2 color changes (from red to white, and from green to white) I hardcoded the values and all of the transition colors between them. So here's how it looks:
public class BlinkingTextView extends TextView {
public BlinkingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void animateBlink(final boolean red) {
if (animator != null) {
animator.drop();
}
animator = new Animator(this, red);
animator.start();
}
public void clearBlinkAnimation() {
if (animator != null) {
animator.drop();
}
}
private Animator animator;
private final static class Animator extends Thread {
public Animator(final TextView textView, final boolean red) {
this.textView = textView;
if (red) {
SET_TO_USE = RED;
} else {
SET_TO_USE = GREEN;
}
}
private TextView textView;
private final int[] SET_TO_USE;
private final static int[] RED = {
-2142396,
-2008754,
-1874854,
-1740697,
-1540490,
-1405563,
-1205099,
-1004634,
-804170,
-669243,
-469036,
-334879,
-200979,
-67337,
-1
};
private final static int[] GREEN = {
-6959821,
-6565826,
-6106293,
-5646758,
-5055894,
-4530309,
-3939444,
-3283042,
-2692177,
-2166592,
-1575728,
-1116193,
-656660,
-262665,
-1
};
private boolean stop;
#Override
public void run() {
int i = 0;
while (i < 15) {
if (stop) break;
final int color = SET_TO_USE[i];
if (stop) break;
textView.post(new Runnable() {
#Override
public void run() {
if (!stop) {
textView.setTextColor(color);
}
}
});
if (stop) break;
i++;
if (stop) break;
try {
Thread.sleep(66);
} catch (InterruptedException e) {}
if (stop) break;
}
}
public void drop() {
stop = true;
}
}
}
You can use new Property Animation Api for color animation:
Integer colorFrom = getResources().getColor(R.color.red);
Integer colorTo = getResources().getColor(R.color.blue);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.addUpdateListener(new AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
textView.setTextColor((Integer)animator.getAnimatedValue());
}
});
colorAnimation.start();
For backward compatability with Android 2.x use Nine Old Androids library from Jake Wharton.
The Easiest solution will be to use Object Animators :
ObjectAnimator colorAnim = ObjectAnimator.ofInt(yourTextView, "textColor",
Color.RED, Color.GREEN);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.start();
No need to keep handles to the two text views. First add the fadeIn/fadeOut animations:
textSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
textSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
then:
TextView currentTextView = (TextView)(textSwitcher.getNextView().equals(
textSwitcher.getChildAt(0)) ?
textSwitcher.getChildAt(1) : textSwitcher.getChildAt(0)
);
// setCurrentText() first to be the same as newText if you need to
textSwitcher.setTextColor(fadeOutColor);
((TextView) textSwitcher.getNextView()).setTextColor(Color.WHITE);
textSwitcher.setText(newText);
Just implemented it like this so proven to work.
best way use ValueAnimator and ColorUtils.blendARGB
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
valueAnimator.setDuration(325);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
float fractionAnim = (float) valueAnimator.getAnimatedValue();
textView.setTextColor(ColorUtils.blendARGB(Color.parseColor("#FFFFFF")
, Color.parseColor("#000000")
, fractionAnim));
}
});
valueAnimator.start();
Although I haven't found a totally distinct method, I have tried to use a TextSwitcher (with the fade animation) to create the colour-change effect. A TextSwitcher is a kind of ViewSwitcher which literally animates between two (internal) TextViews. Did you manually implement the same system unknowingly? ;) It manages a bit more of the process for you, so you may find it easier to work with (especially if you want to try more involved animations). I would create new subclass of TextSwitcher and some methods e.g. setColour() which can set the new colour and then trigger an animation. The animation code can then be moved outside of your main application.
make sure you keep a handle on the two TextViews that are put into the switcher
change the colour of the other TextView and call setText() to animate between them
If you are already using a ViewSwitcher then I don't think there is an easier way to implement this.
As others mention, using ObjectAnimator solves for this. However, in the existing posts - I wasn't seeing how to set duration. For me the color change would happen immediately.
The solution below shows:
setting the animation with some interval; thanks to post: https://plus.google.com/+CyrilMottier/posts/X4yoNHHszwq
a way to continuously cycle back and forth between the 2 colors
void animateTextViewColors(TextView textView, Integer colorTo) {
final Property<TextView, Integer> property = new Property<TextView, Integer>(int.class, "textColor") {
#Override
public Integer get(TextView object) {
return object.getCurrentTextColor();
}
#Override
public void set(TextView object, Integer value) {
object.setTextColor(value);
}
};
final ObjectAnimator animator = ObjectAnimator.ofInt(textView, property, colorTo);
animator.setDuration(8533L);
animator.setEvaluator(new ArgbEvaluator());
animator.setInterpolator(new DecelerateInterpolator(2));
animator.start();
}
void oscillateDemo(final TextView textView) {
final int whiteColor = ContextCompat.getColor(TheApp.getAppContext(), R.color.white);
final int yellowColor = ContextCompat.getColor(TheApp.getAppContext(), R.color.yellow);
final int counter = 100;
Thread oscillateThread = new Thread() {
#Override
public void run() {
for (int i = 0; i < counter; i++) {
final int fadeToColor = (i % 2 == 0)
? yellowColor
: whiteColor;
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
animateTextViewColors(textView, fadeToColor);
}
});
try {
Thread.sleep(2450);
}
catch (InterruptedException iEx) {}
}
}
};
oscillateThread.start();
}
I scrapped the variant of the 2 TextViews since it looked weird (edges weren't smooth and, since I have a lot of such elements on the screen it was really lagging the scrolling). What I did, was a crazy hack that does the animation with the use of a Thread and setTextColor (that also forces redraw of a textview).
Since I needed only 2 color changes (from red to white, and from green to white) I hardcoded the values and all of the transition colors between them. So here's how it looks:
public class BlinkingTextView extends TextView {
public BlinkingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void animateBlink(final boolean red) {
if (animator != null) {
animator.drop();
}
animator = new Animator(this, red);
animator.start();
}
public void clearBlinkAnimation() {
if (animator != null) {
animator.drop();
}
}
private Animator animator;
private final static class Animator extends Thread {
public Animator(final TextView textView, final boolean red) {
this.textView = textView;
if (red) {
SET_TO_USE = RED;
} else {
SET_TO_USE = GREEN;
}
}
private TextView textView;
private final int[] SET_TO_USE;
private final static int[] RED = {
-2142396,
-2008754,
-1874854,
-1740697,
-1540490,
-1405563,
-1205099,
-1004634,
-804170,
-669243,
-469036,
-334879,
-200979,
-67337,
-1
};
private final static int[] GREEN = {
-6959821,
-6565826,
-6106293,
-5646758,
-5055894,
-4530309,
-3939444,
-3283042,
-2692177,
-2166592,
-1575728,
-1116193,
-656660,
-262665,
-1
};
private boolean stop;
#Override
public void run() {
int i = 0;
while (i < 15) {
if (stop) break;
final int color = SET_TO_USE[i];
if (stop) break;
textView.post(new Runnable() {
#Override
public void run() {
if (!stop) {
textView.setTextColor(color);
}
}
});
if (stop) break;
i++;
if (stop) break;
try {
Thread.sleep(66);
} catch (InterruptedException e) {}
if (stop) break;
}
}
public void drop() {
stop = true;
}
}
}
The issue I found with valueAnimator as well as ObjectAnimator is that the animator iterates through a number of random colors, and the transition doesn't look smooth. I wrote the following code which worked smoothly. Hope it helps someone else also.
public static void changeTextColor(final TextView textView, int startColor, int endColor,
final long animDuration, final long animUnit){
if (textView == null) return;
final int startRed = Color.red(startColor);
final int startBlue = Color.blue(startColor);
final int startGreen = Color.green(startColor);
final int endRed = Color.red(endColor);
final int endBlue = Color.blue(endColor);
final int endGreen = Color.green(endColor);
new CountDownTimer(animDuration, animUnit){
//animDuration is the time in ms over which to run the animation
//animUnit is the time unit in ms, update color after each animUnit
#Override
public void onTick(long l) {
int red = (int) (endRed + (l * (startRed - endRed) / animDuration));
int blue = (int) (endBlue + (l * (startBlue - endBlue) / animDuration));
int green = (int) (endGreen + (l * (startGreen - endGreen) / animDuration));
textView.setTextColor(Color.rgb(red, green, blue));
}
#Override
public void onFinish() {
textView.setTextColor(Color.rgb(endRed, endGreen, endBlue));
}
}.start();
}

Categories

Resources