How can I add additional transitions to my animation on Android? - android

I have a transition animation that works very nicely. How can I add an additional translation after I pause for 1000 milliseconds?
view.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int button1_position[] = new int[2];
ibContact1.getLocationInWindow(button1_position);
Log.e(">>>>", "button1_position[0] "+button1_position[0] + " button1_position[1] " + button1_position[1]);
mSlidingImage = (ImageView) view.findViewById(R.id.slidingImage);
Animation translation = new TranslateAnimation(button1_position[0]-200, button1_position[0]-25, 0, button1_position[1] - 110);
translation.setStartOffset(1000);
translation.setDuration(2000);
translation.setRepeatCount(1);
//translation.setRepeatCount(Animation.INFINITE);
//translation.setRepeatMode(Animation.REVERSE);
mSlidingImage.startAnimation(translation);
//mSlidingImage.setVisibility(View.INVISIBLE);
}
});
return view;
}

Related

ViewPropertyAnimator messing with view layout position

I created a button, set the position using top and left margin and added to the layout. If i just do this the buttons sits there in the right location.
Now I need to add a translation animation, and I am using viewPropertyAnimator.
Unfortunately this voids the initial button position, making the animation start from (0, 0).
Following is the code I wrote.
final Button bonus_button = new Button(this);
int bonus_y_start, bonus_x_start, bonus_y_end, bonus_x_end;
bonus_x_start = random.nextInt(2) == 1 ? layout_width + button_size : -1 * button_size;
bonus_y_start = random.nextInt(layout_height + 2 * button_size) - button_size;
if (bonus_x_start < 0)
bonus_x_end = layout_width + button_size;
else
bonus_x_end = -1 * button_size;
bonus_y_end = random.nextInt(layout_height + button_size) - button_size;
RelativeLayout.LayoutParams bonus_l = new RelativeLayout.LayoutParams(button_size, button_size);
bonus_l.leftMargin = bonus_x_start;
bonus_l.topMargin = bonus_y_start;
bonus_button.setLayoutParams(bonus_l);
bonus_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mHandler.sendEmptyMessage(MSG_PAUSE_TIMER);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
mHandler.sendEmptyMessage(MSG_RESUME_TIMER);
}
}, 2000);
}
});
bonus_button.bringToFront();
ViewPropertyAnimator animator = bonus_button.animate().x(bonus_x_end).y(bonus_y_end);
animator.setDuration(2000);
animator.withEndAction(new Runnable() {
#Override
public void run() {
layout.removeView(bonus_button);
}
});
layout.addView(bonus_button);
Anybody has any idea on where I'm doing wrong?
Use bonus_l.setX(bonus_x_start) and bonus_l.setY(bonus_x_start) instead of margins

Android: move imageView thats lay on window manager with animation

a have some problem and didn't find a solution. The problem is, i have a image view that's lay on layout and this layout lay on window manager, this image view is clickable. This image view have to move to right and to the left and be clickable. Here is my code
`public class FloatingAnimationService extends Service {
private WindowManager windowManager;
private ImageView floatingUnit;
private boolean isClicked;
public void onCreate() {
super.onCreate();
isClicked = false;
floatingUnit = new ImageView(this);
//a unit as imageView
Picasso.with(this).load(new File(Environment.getExternalStorageDirectory().getPath() +"/" + "cat.png")).into(floatingUnit);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(AppMethods.getPxFromDp(120 ,this), AppMethods.getPxFromDp(120 , this));
floatingUnit.setLayoutParams(layoutParams);
final LinearLayout floatingUnitLayout = new LinearLayout(this);
floatingUnitLayout.addView(floatingUnit);
windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
//here is all the science of params
final LayoutParams myParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_PHONE,
LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
myParams.gravity = Gravity.TOP | Gravity.LEFT;
myParams.x=0;
myParams.y=100;
windowManager.addView(floatingUnitLayout, myParams);
// add a floatingUnit icon in window
Animation animation = AnimationUtils.loadAnimation(this ,R.anim.floating_unit_animation);
floatingUnit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startQuestion();
stopSelf();
windowManager.removeViewImmediate(floatingUnitLayout);
isClicked = true;
}
});
floatingUnit.startAnimation(animation);
startAnimationTimer(floatingUnitLayout);
AnimationSet animationSet = new AnimationSet(true);
myParams.x = 100;
myParams.y = 100;
windowManager.updateViewLayout(floatingUnitLayout, myParams);
Animation animationToRight = AnimationUtils.loadAnimation(this ,R.anim.left_to_right_animation);
Animation animationToLeft = AnimationUtils.loadAnimation(this , R.anim.right_to_left_animation);
animationSet.addAnimation(animationToRight);
floatingUnit.startAnimation(animation);
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private void startQuestion (){
Intent intentQuestionActivity = new Intent(this, QuestionActivity.class);
intentQuestionActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentQuestionActivity);
}
private void startAnimationTimer(final View floatingUnitLayout){
long animationLifeTime = 10000;
new CountDownTimer(animationLifeTime, 1000) {
public void onTick(long millisUntilFinished) {
Log.i("animation remaining: " , Long.toString(millisUntilFinished / 1000));
}
public void onFinish() {
Log.i("animation: " , "DONE");
if(!isClicked) {
windowManager.removeViewImmediate(floatingUnitLayout);
stopSelf();
}
}
}.start();
}
`
Please help. Thanks !
Thanks all . Found the solution by myself. The trick is: to move a image that lay on window manager, you have to update the Layout Params.
here is the code below
private void startAnimationTimer(final View floatingUnitLayout) {
long animationLifeTime = AppData.first(AppData.class).getCountSecondsPicShow();
countDownTimer = new CountDownTimer(animationLifeTime, 100) {
public void onTick(long millisUntilFinished) {
Log.i("animation remaining: ", Long.toString(millisUntilFinished / 1000));
myParams.x = myParams.x + 2;
myParams.y = myParams.y + 2;
windowManager.updateViewLayout(floatingUnitLayout, myParams);
}
public void onFinish() {
Log.i("animation: ", "DONE");
if (!isClicked) {
windowManager.removeViewImmediate(floatingUnitLayout);
stopSelf();
}
}
}.start();
}

Object animator not removing update listener android

Good day.I have an scenario where this half normal object animator keeps firing over and over causing heap grow and ofcourse out of memory issue at some point.Here is how it go. I have made static method for rainbow animation like this.
public static ObjectAnimator startRainbowAnimation(Context context,
String textToShow,
final TextView textViewToAttach) {
AnimatedColorSpan span = new AnimatedColorSpan(context);
final SpannableString spannableString = new SpannableString(textToShow);
String substring = textToShow;
int start = textToShow.indexOf(substring);
int end = start + substring.length();
spannableString.setSpan(span, start, end, 0);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
span, ANIMATED_COLOR_SPAN_FLOAT_PROPERTY, 0, 100);
objectAnimator.setEvaluator(new FloatEvaluator());
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
System.gc();
Log.d("Fafasfasfas", "onAnimationUpdate: inside true");
textViewToAttach.setText(spannableString);
}
});
objectAnimator.setInterpolator(new LinearInterpolator());
objectAnimator.setDuration(DURATION);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
objectAnimator.start();
return objectAnimator;
}
private static final Property<AnimatedColorSpan, Float> ANIMATED_COLOR_SPAN_FLOAT_PROPERTY
= new Property<AnimatedColorSpan, Float>(Float.class, "ANIMATED_COLOR_SPAN_FLOAT_PROPERTY") {
#Override
public void set(AnimatedColorSpan span, Float value) {
span.setTranslateXPercentage(value);
}
#Override
public Float get(AnimatedColorSpan span) {
return span.getTranslateXPercentage();
}
};
I am calling this method like this inside an recycler view adapter
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
ChatModel chatModel = chatModelList.get(position);
System.gc();
String messageBody = chatModel.getMessage().replaceAll("userid=" + chatModel.getUserId() + ":" + Constants.TYPE_MESSAGE_ATTACHMENT, "").replaceAll("userid=" + chatModel.getOpponentId() + ":" + Constants.TYPE_MESSAGE_ATTACHMENT, "");
holder.message.setText(messageBody);
if (showAsRainbow) {
if (holder.message.getTag() == null) {
objectAnimator = RainbowAnimation.startRainbowAnimation(mContext, messageBody, holder.message);
holder.message.setTag(ANIMATED);
}
} else {
objectAnimator.removeAllUpdateListeners();
objectAnimator.removeAllListeners();
objectAnimator.end();
objectAnimator.cancel();
holder.message.setTag(null);
}
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) holder.chatViewBubble.getLayoutParams();
LinearLayout.LayoutParams deliveryStatusParams = (LinearLayout.LayoutParams) holder.deliveryStatus.getLayoutParams();
LinearLayout.LayoutParams gifChatViewLayoutParams = (LinearLayout.LayoutParams) holder.imageViewWrapper.getLayoutParams();
checkForGif(chatModel, holder);
if (mCurrentUserId.equals(chatModel.getUserId())) {
layoutParams.gravity = Gravity.RIGHT;
layoutParams.rightMargin = Dp.toDps(mContext, 16);
deliveryStatusParams.gravity = Gravity.RIGHT;
gifChatViewLayoutParams.gravity = Gravity.RIGHT;
holder.chatViewBubble.setLayoutParams(layoutParams);
holder.imageView.setLayoutParams(gifChatViewLayoutParams);
holder.chatViewBubble.setBackground(ContextCompat.getDrawable(mContext, R.drawable.outgoing_message_bg));
if (chatModel.getDeliveryStatus().equals(Constants.STATUS_DELIVERED)) {
if (position >= chatModelList.size() - 1) {
holder.deliveryStatus.setVisibility(View.VISIBLE);
} else {
holder.deliveryStatus.setVisibility(View.INVISIBLE);
}
holder.deliveryStatus.setText(mContext.getString(R.string.sentText));
} else if (chatModel.getDeliveryStatus().equals(Constants.STATUS_NOT_DELIVERED)) {
holder.deliveryStatus.setVisibility(View.VISIBLE);
if (updating) {
holder.deliveryStatus.setText(mContext.getString(R.string.sendingNowText) + percentage + " %");
} else {
holder.deliveryStatus.setText(mContext.getString(R.string.sendingNowText));
}
}
} else {
holder.chatViewBubble.setBackground(ContextCompat.getDrawable(mContext, R.drawable.incoming_message_bg));
layoutParams.gravity = Gravity.LEFT;
gifChatViewLayoutParams.gravity = Gravity.LEFT;
holder.chatViewBubble.setLayoutParams(layoutParams);
holder.imageView.setLayoutParams(gifChatViewLayoutParams);
holder.deliveryStatus.setVisibility(View.INVISIBLE);
}
}
Thi issue is that if you noticed the Log.d() keeps firing even after the cancle and end was called on the objectAnimator and yes i have checked that cancel and end is being called.So i have no clue what i have done wrong.Can anyone please help me?
I see lot's of guys been looking at the post,meaning they got this issue too so i got the solution somehow fixed...Issue is that this damned ObjectAnimator when done inside an loop even with static method,each time is being created new reference to it.So you have to do something like this.Have an array list of object animators,on each call add items to array list.Whenever you want to stop it just simply loop through array and stop all object animators.Here is the code
private ArrayList<ObjectAnimator> objectAnimators = new ArrayList<>();
public void startRainbowAnimation(Context context,
final String textToShow,
final TextView textViewToAttach) {
stopCalled = false;
AnimatedColorSpan span = new AnimatedColorSpan(context);
final SpannableString spannableString = new SpannableString(textToShow);
String substring = textToShow;
int start = textToShow.indexOf(substring);
int end = start + substring.length();
spannableString.setSpan(span, start, end, 0);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(
span, animatedColorSpanFloatProperty, 0, 100);
objectAnimator.setEvaluator(new FloatEvaluator());
objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
System.gc();
if (!stopCalled) {
textViewToAttach.setText(spannableString);
}
}
});
objectAnimator.setInterpolator(new LinearInterpolator());
objectAnimator.setDuration(DateUtils.MINUTE_IN_MILLIS * 3);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
objectAnimator.start();
objectAnimators.add(objectAnimator);
}
objectAnimators here is an array list of object animators like this
And here is how you stop all of them to get ride of infinite update listener call firing.
public void stopRainbowAnimation() {
System.gc();
stopCalled = true;
if (!objectAnimators.isEmpty()) {
for (int i = 0; i < objectAnimators.size(); i++) {
ObjectAnimator eachAnimator = objectAnimators.get(i);
eachAnimator.setRepeatCount(0);
eachAnimator.end();
eachAnimator.cancel();
eachAnimator.removeAllListeners();
eachAnimator.removeAllUpdateListeners();
}
objectAnimators.clear();
}
}
Hope this will help someone

Why the location of view always resets after I use ObjectAnimator to View.TRANSLATION_X?

I created a simple app, just want when I click a button, the imageview will travel to another position.
Below is the code, but it seems that when I click the button, the imageview always begin with the original position, not the real position. Is there any problem with my code?
public class MainActivity extends Activity{
private ImageView iv;
private Button bt;
private AnimatorSet mAniSet;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ddd);
iv = (ImageView) this.findViewById(R.id.imageView1);
bt = (Button) this.findViewById(R.id.button1);
mAniSet = new AnimatorSet();
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int pointOnScreen[] = new int[2];
iv.getLocationOnScreen(pointOnScreen);
ObjectAnimator AniTranslationX = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, pointOnScreen[0]+100);
mAniSet.playTogether(AniTranslationX);
mAniSet.setTarget(iv);
mAniSet.setDuration(200).start();
}
});
}
}
If you create a ObjectAnimator.ofFloat(Object, Property, values) and there is only one value, so the origin of the animation will be 0f.
You can find the source into the class android.animation.KeyFrameSet and the following method:
public static KeyframeSet ofFloat(float... values) {
boolean badValue = false;
int numKeyframes = values.length;
FloatKeyframe keyframes[] = new FloatKeyframe[Math.max(numKeyframes,2)];
if (numKeyframes == 1) {
keyframes[0] = (FloatKeyframe) Keyframe.ofFloat(0f);
keyframes[1] = (FloatKeyframe) Keyframe.ofFloat(1f, values[0]);
if (Float.isNaN(values[0])) {
badValue = true;
}
} else {
keyframes[0] = (FloatKeyframe) Keyframe.ofFloat(0f, values[0]);
for (int i = 1; i < numKeyframes; ++i) {
keyframes[i] =
(FloatKeyframe) Keyframe.ofFloat((float) i / (numKeyframes - 1), values[i]);
if (Float.isNaN(values[i])) {
badValue = true;
}
}
}
if (badValue) {
Log.w("Animator", "Bad value (NaN) in float animator");
}
return new FloatKeyframeSet(keyframes);
}
You can see, with only one value, the start of the animation is 0f (keyframes[0]).
If you put 2 values, your animation will start from the first value and stop at the second:
ObjectAnimator AniTranslationX = ObjectAnimator.ofFloat(iv, View.TRANSLATION_X, pointOnScreen[0], pointOnScreen[0] + 100);
With 3 or more values, the animation will reach all these values over the time of the animation.

Animation starts only when I touch the screen

I have a listview with entries which have a fade out animation when the user deletes\edits an entry in the list.
For some reason, after I perform the action (edit\delete) the animation will not start until I press the screen again. Only they will the animation actually perform.
From this method the animation is being called:
public void replace(View view, long position) {
int hoursSum = 0;
int minuteSum = 0;
boolean hoursIssue = false;
if (finsihIntMinutes >= startIntMinutes) {
minuteSum = finsihIntMinutes - startIntMinutes;
} else if (finsihIntMinutes < startIntMinutes) {
minuteSum = (finsihIntMinutes + Utility.MINUTES_TIME_UNIT)
- startIntMinutes;
hoursIssue = true;
}
if (finishIntHours >= startIntHours) {
hoursSum = finishIntHours - startIntHours;
if (hoursIssue == true) {
--hoursSum;
}
} else if (finishIntHours < startIntHours) {
hoursSum = (finishIntHours + Utility.HOURS_TIME_UNIT)
- startIntHours;
}
double salper = (minuteSum * Main.sal) / 60;
double salper2 = hoursSum * (Main.sal);
String madeSoFar = ""
+ String.valueOf(formatter.format(salper2 + salper));
String edited = "**Edited**";
String totalTime = "" + hoursSum + ":" + minuteSum;
DB.edit(position, Shifts.editDate, Shifts.editStartTime,
Shifts.editEndTime, Shifts.dayString, totalTime, minuteSum,
madeSoFar, edited, totalHours, totalMinutes);
moneySummary = getMoney();
hoursSummary = "Total Hours: " + getHours();
summary.setText(moneySummary);
**FastScrollAdapter.animate(Shifts.view);**
}
Here is the animation code:
public static void animate(final View v) {
Animation out = new AlphaAnimation(1, 0);
out.setInterpolator(new DecelerateInterpolator());
out.setDuration(350);
v.setAnimation(out);
out.start();
out.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.i("Animation", "Started");
}
#Override
public void onAnimationRepeat(Animation animation) {
// Shifts.setSummaryCursor.requery();
Shifts.getMoneyCursor.requery();
FastScrollAdapter.cursor.requery();
Shifts.CA.notifyDataSetChanged();
}
#Override
public void onAnimationEnd(Animation animation) {
// Shifts.setSummaryCursor.requery();
Shifts.getMoneyCursor.requery();
FastScrollAdapter.cursor.requery();
Shifts.CA.notifyDataSetChanged();
Log.i("Animation", "Ended");
}
});
}
I had exactly the same problem, I solved it by calling
view.clearAnimation();
just before
view.startAnimation(animation);
I think you need to change the order
1. v.setAnimation(out);
2. out.setAnimationListener(new AnimationListener());
3. v.startAnimation(out);
I had a similar problem: the animation was not performet in the UE thread. Try to run the animation posting it in the UE thead:
mHandler.post(new Runnable() {
#Override
public void run() {
yourView.startAnimation(yourAnimation));
}
});

Categories

Resources