Play ripple animation without user interaction - android

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);
}

Related

How to make a proper move in/out animation?

In onViewCreate I set the view behind the screen:
holderMenus.post(new Runnable() {
#Override public void run() {
if (holderMenus != null) {
if (DeviceType.INSTANCE.tablet) {
holderMenus.animate().translationY(-holderMenus.getHeight());
} else {
holderMenus.animate().translationY(holderMenus.getHeight());
}
}
}
});
Then, when I want to move the view in:
holderMenus.animate().translationY(0).setDuration(300);
That works well; the view slides in. Next I want to move the view off the screen:
if (DeviceType.INSTANCE.tablet) {
holderMenus.animate().translationY(-holderMenus.getHeight()).setDuration(300);
} else {
holderMenus.animate().translationY(holderMenus.getHeight()).setDuration(300);
}
But instead of sliding, the view disappears, as if I had set the property to .setVisibility(View.GONE);
How can I make it slide instead?
I have made a custom action button that slides from below the screen and slides back under the screen when listview is scrolled.
Hides:
TranslateAnimation anim = new TranslateAnimation(0, 0, 0, 500);
anim.setDuration(300l);
anim.setFillAfter(true);
layout.startAnimation(anim); //layout is the layout with the button
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
layout.clearAnimation();
layout.setVisibility(View.GONE);
}
}, 300l);
Shows:
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
try {
layout.setVisibility(View.VISIBLE);
TranslateAnimation anim = new TranslateAnimation(0, 0, 500, 0);
anim.setDuration(300l);
anim.setFillAfter(true);
layout.startAnimation(anim);
} catch (Exception e) {
e.printStackTrace();
}
}
}, 500l);
I guess that slide out animation is not starting from the point that you think it is. It probably "animates" in the same position off the screen.
You might want to try ObjectAnimator, where you can specify both starting and ending points for your animation using ofFloat method for example.

How would i make imageView rolling in from bottom left corner to the bottom right corner?

Does anyone know how would i do this? And imageView should come from outside of the activity.
Here is the code:
rollingImageView = (ImageView) findViewById(R.id.imageView1);
AnimationSet rollingIn = new AnimationSet(true);
Animation moving = new TranslateAnimation(Animation.RELATIVE_TO_PARENT,-1f,Animation.RELATIVE_TO_PARENT,0,Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT, 0);
moving.setDuration(5000);
rollingIn.addAnimation(moving);
Animation rotating = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotating.setDuration(5000);
rollingIn.addAnimation(rotating);
rollingImageView.startAnimation(rollingIn);
Assuming you have your imageview set to the bottom left down, (this will only help you go from left to right)
In your activity file...
public void callthismethodtostartrolling(){
final Thread myThread = new Thread(new Runnable()
{
#Override
public void run()
{
while (!<imageviewInRightCorner>) {//<<define this yourself
try {
Thread.sleep(4);
runOnUiThread(new Runnable()
{
#Override
public void run() {
rollingImageView.setX((float) (rollingImageView.getX() + 5));
}
});
} catch (InterruptedException e) {
// this should not happen
}
}
}
});
myThread.start
}
PROS
I think it gives the effect you want.
CONS
Ive tried this before, it works, but you cant interrupt the Thread inside the Thread if you ever need to do so. The link to the original problem/con of this is here,if you would like to help Dusan and I ._.
ALTERNATIVE
final Thread myThread = new Thread(new Runnable()
{
#Override
public void run()
{
while (!<imageviewInRightCorner>) {//<<define this yourself
try {
Thread.sleep(4);
runOnUiThread(new Runnable()
{
#Override
public void run() {
rollingImageView.setX((float) (rollingImageView.getX() + 5));
}
});
} catch (InterruptedException e) {
// this should not happen
}
}
}
});
Using myThread.start anywhere in your code to activate your sliding imageview awesomeness.
PROS
Alternative to first option with a different con to suit your need
*able to use myThread.interrupt within Thread
CONS
Cant call the Thread more than once per launch :/ the reason why is all in here
potential while condition explained(as requested by asker)...

Android's Overflow Menu animation?

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;
}

Animation Class called inside another class

I am creating a boardgame for Android and I have an animation that I wish to do when a specific motion event occurs.
I have a Boardview class which draws on a surfaceview with bitmaps and this shows the user the board.
When a motion event happens, this code executes
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
Intent animate = new Intent(Tempboardview.this, Moving.class);
animate.putExtra("xOld", oldStones.get(i).getx());
animate.putExtra("yOld", oldStones.get(i).gety());
animate.putExtra("xNew", newStones.get(i).getx());
animate.putExtra("yNew", newStones.get(i).gety());
animate.putExtra("color", 0);
startActivity(animate);
}
}
where newStones and oldStones are simply ArrayList objects and gety() and getx() return doubles.
This is my Moving class
public class Moving extends Activity {
private static final String TAG = "Animation";
double xOld = 0, yOld = 0, xNew = 0, yNew = 0, color = 0;
/** Called when the activity is first created. */#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
LayoutInflater inflater = getLayoutInflater();
addContentView(inflater.inflate(R.layout.move, null),
new ViewGroup.LayoutParams(800, 480));
final ImageView image = (ImageView) findViewById(R.id.stoneimager);
image.setVisibility(View.GONE);
image.bringToFront();
Bundle extras = getIntent().getExtras();
if (extras != null) {
xOld = extras.getDouble("xOld");
yOld = extras.getDouble("yOld");
xNew = extras.getDouble("xNew");
yNew = extras.getDouble("yNew");
color = extras.getDouble("color");
}
Animation animate = new TranslateAnimation((float) xOld, (float) xNew, (float) yOld, (float) yNew);
animate.setDuration(1000);
animate.setStartOffset(0);
animate.setInterpolator(new LinearInterpolator());
animate.setRepeatCount(0);
animate.setFillAfter(true);
animate.setZAdjustment(Animation.ZORDER_TOP);
Animation.AnimationListener listener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.d("boardtag", "animation started");
image.setVisibility(View.VISIBLE);
image.bringToFront();
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image.setVisibility(View.GONE);
finish();
}
};
animate.setAnimationListener(listener);
Log.d("boardtag", "animation started");
image.startAnimation(animate);
}
}
The problem is the animation is not able to take place while the board is still displayed.
I have used the android manifest to change the theme of .Moving to Transparent, but this did not work.
Any advice would be appreciated.
This is probably going to come off more harsh than I intend, but I think you are completely abusing the Intent/Activity features. You need to seriously consider redesigning your UI layer to be more closely coupled with the active Activity.
What you have above should be painfully slow since motion events are common and Activity creation and Intent dispatching is expensive (relatively). What you need is a single main "game" Activity that is tightly coupled with a SurfaceView (probably OpenGL since this has animations and it's a game). This activity would take user input (UI events) and translate them into drawing commands for your surface view directly.

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