I have two views in different layouts I want to move one to another. What's wrong with my code? Y animation plays wrong. First view is located in fragment's layout, second in status bar
...
int p1[] = new int[2];
int p2[] = new int[2];
viewOne.getLocationInWindow(p1);
viewTwo.getLocationInWindow(p2);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet
.play(ObjectAnimator.ofFloat(expandedImageView, "X", p1[0], p2[0] - p1[0]))
.with(ObjectAnimator.ofFloat(expandedImageView, "Y", p1[1], p2[1] - p1[1]))
.with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScale))
.with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScale));
I have a another solution for you:(move viewOne to viewTwo)
TranslateAnimation animation = new TranslateAnimation(0, viewTwo.getX()-viewOne.getX(),0 , viewTwo.getY()-viewOne.getY());
animation.setRepeatMode(0);
animation.setDuration(3000);
animation.setFillAfter(true);
viewOne.startAnimation(animation);
Try this
private fun moveView(viewToBeMoved: View, targetView: View) {
val targetX: Float =
targetView.x + targetView.width / 2 - viewToBeMoved.width / 2
val targetY: Float =
targetView.y + targetView.height / 2 - viewToBeMoved.height / 2
viewToBeMoved.animate()
.x(targetX)
.y(targetY)
.setDuration(2000)
.withEndAction {
targetView.visibility = View.GONE
}
.start()
}
Related
https://youtu.be/1c2TYKv50Ic
What is the bonus (HEMEN AL)button animation code included in the video. Could you help
code:
int basY =100;
int sonY = 10;
CipView cip = new CipView(context);
AnimatorSet1 = new AnimatorSet();
ObjectAnimator starScaleYAnimator = ObjectAnimator.ofFloat(cip, View.SCALE_Y, 0.5f);
starScaleYAnimator.setDuration(350);
starScaleYAnimator.setStartDelay(300);
ObjectAnimator starScaleXAnimator = ObjectAnimator.ofFloat(cip, View.SCALE_X, 0.5f);
starScaleXAnimator.setDuration(350);
starScaleXAnimator.setStartDelay(300);
AnimatorSet1.playTogether(starScaleYAnimator, starScaleXAnimator);
AnimatorSet1.start();
int basY =100;
int sonY = 10;
CipView cip = new CipView(context);
AnimatorSet1 = new AnimatorSet();
ObjectAnimator starScaleYAnimator = ObjectAnimator.ofFloat(cip, "scaleY", basY, sonY);
starScaleYAnimator.setDuration(350);
starScaleYAnimator.setStartDelay(300);
ObjectAnimator starScaleXAnimator = ObjectAnimator.ofFloat(cip, "scaleX", basY, sonY);
starScaleXAnimator.setDuration(350);
starScaleXAnimator.setStartDelay(300);
AnimatorSet1.playTogether(starScaleYAnimator, starScaleXAnimator);
AnimatorSet1.start();
Is it possible to control the width animation for a view that has wrap_content? Right now I have a TextView with a barrier to its right side. I have another background View that aligns to this barrier. When the TextView changes text it just flashes to the new width but I want to animate the width with an animator where I can control the timing, interpolator etc. I know that I can set something like animateLayoutChanges but the effect is not customisable.
You can use ObjectAnimator along with AnimatorSet for the animation.
Here is an extension in kotlin for the view on which you want to apply the animation.
inline fun View.animateWidth(duration: Long = 1000, startDelay: Long = 0) {
val scaleX = ObjectAnimator.ofFloat(this, "scaleX", /*Change width*/)
//val scaleY = ObjectAnimator.ofFloat(this, "scaleY", /*Change width*/)
val set = AnimatorSet()
//set.playTogether(scaleX, scaleY)
set.playTogether(scaleX)
set.duration = duration
set.startDelay = startDelay
set.interpolator = LinearInterpolator()
set.addAnimatorListener(
onStart = { this.isClickable = false },
onEnd = { this.isClickable = true }
)
set.start()
}
I have simple animation that moves the object in Y axis. I want change the behavior for the REVERSE.
//My Code:
ImageView iv = ... //my view
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "y", 300);
oa.setDuration(100);
oa.setRepeatCount(1);
oa.setRepeatMode(ValueAnimator.REVERSE);
oa.start();
You cannot change the behavior of the reverse mode. Instead, you will need to create an AnimatorSet and play them sequentially.
ImageView iv = ... //my view
ObjectAnimator oa = ObjectAnimator.ofFloat(iv, "y", 300);
oa.setDuration(100);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(/* code here */)
// Add any other code for oa2
AnimatorSet set = new AnimatorSet();
set.playSequentially(oa, oa2);
set.start()
public void animateScreenOn() {
CellLayout layout = (CellLayout) getChildAt(getCurrentPage());
if (layout != null) {
int count = layout.getPageChildCount();
for (int i = count-1; i>=0; i--) {
View v = layout.getPageChild(i);
v.setScaleX(0.95f);
v.setScaleY(0.95f);
AnimatorSet set = new AnimatorSet();
ObjectAnimator animation1 = LauncherAnimUtils.ofPropertyValuesHolder(v,
PropertyValuesHolder.ofFloat("scaleX", 1.0f));
animation1.setDuration(1000L);
animation1.setInterpolator(new BounceInterpolator());
ObjectAnimator animation2 = LauncherAnimUtils.ofPropertyValuesHolder(v,
PropertyValuesHolder.ofFloat("scaleY", 1.0f));
animation2.setDuration(500L);
animation2.setInterpolator(new BounceInterpolator());
List<Animator> items = new ArrayList<Animator>();
items.add(animation1);
items.add(animation2);
set.playTogether(items);
set.setStartDelay((count-1-i)*100L);
set.start();
}
}
}
this code have an issue. the time of this code cost is different on different devices, some is ok, but some will be too short, so i need to change animate time depend on device refresh rate. anyone can tell me how to get it from android code, 3X!
I want to animate the change in the padding of a view. The resting place of the translation animation is the same as the padding I want to apply.
TranslateAnimation moveleft = new TranslateAnimation(Animation.ABSOLUTE, 0.0f,
Animation.ABSOLUTE, PADDING, Animation.ABSOLUTE,
0.0f, Animation.ABSOLUTE, 0.0f);
moveLeft.setDuration(500);
moveLeft.setFillAfter(true);
This starts the view's animation then sets the padding. This doesn't exactly work because it cause a graphical glitch.
v.startAnimation(moveleft);
v.setPadding(PADDING, 0, 0,0);
Use ValueAnimator, its really simple and unclutter
say,
we have to change right padding to _20dp where as left, top and bottom padding are _6dp, _6dp and 0 respectively.
ofInt() is varagrs type. the value we have to animate is pass in it as KeyValue pair (arg1 = current value, arg2 = target value,............)
Here we go,
ValueAnimator animator = ValueAnimator.ofInt(view.getPaddingRight(), _20dp);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator){
view.setPadding(_6dp, _6dp, (Integer) valueAnimator.getAnimatedValue(), 0);
}
});
animator.setDuration(200);
animator.start();
Instead of setting your padding right away, why not try an animation listener to set the padding after the animation has completed?
v.setAnimationListener(new Animation.AnimationListener() {
...
#Override
public void onAnimationEnd(){
v.setPadding(PADDING, 0, 0,0);
}
...
});
v.startAnimation(moveleft);
Here is how to animate setPadding in Kotlin:
private fun setPaddingWithAnimation() {
val paddingDp: Int = 20
val density: Float = requireActivity().getResources().getDisplayMetrics().density
val paddingPixel: Int = (paddingDp * density).toInt()
val animator = ValueAnimator.ofInt(recyclerItems.paddingRight, paddingPixel)
animator.addUpdateListener { valueAnimator -> binding.recyclerHealthTips.recyclerHealthTips.setPadding(
paddingPixel, 0, valueAnimator.animatedValue as Int, 0) }
animator.duration = 500
animator.start()
}