Android ValueAnimator EditText choppy animation - android

I'm creating sliding animation for my views using Value Animator. Problem is, when subviews is EditText animation is choppy. Code:
private ValueAnimator SlideAnimator(int start,int end, View mView)
{
ValueAnimator animator = ValueAnimator.OfInt(start, end);
animator.SetDuration(2500);
animator.Update +=
(object sender, ValueAnimator.AnimatorUpdateEventArgs e) => {
var value = (int)animator.AnimatedValue;
ViewGroup.LayoutParams layoutParams = mView.LayoutParameters;
layoutParams.Height = value;
mView.LayoutParameters=layoutParams;
mView.RequestLayout();
};
return animator;
if (wrappedChildren[position].Visibility == ViewStates.Visible)
{
int finalHeight = wrappedChildren[position].Height;
ValueAnimator mAnimator = SlideAnimator(finalHeight, 0, wrappedChildren[position]);
mAnimator.Start();
mAnimator.AnimationEnd += (sender, args) =>
{
SetSectionVisibility(position, ViewStates.Gone);
};
}
else
{
int widthSpec = View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified);
int heightSpec = View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified);
wrappedChildren[position].Measure(widthSpec,heightSpec);
int measuredHeight = wrappedChildren[position].MeasuredHeight;
ValueAnimator mAnimator = SlideAnimator(0, measuredHeight,wrappedChildren[position]);
mAnimator.Start();
SetSectionVisibility(position, ViewStates.Visible);
}
wrappedChildren[position] is View that holds EditText subview

Have you tried it on emulator or real device? It may be sluggish just because of emulator...

Related

Animation set view width from X to 0

I am trying to take a CardView (in a RecycleView) from actual width to 0. I am a newbie in android and in android animations. Could you say me what I am doing wrong?
This is my code:
final View v = cardViewHolder.cv;
int actualWidth = v.getMeasuredWidth();
ValueAnimator anim = ValueAnimator.ofInt(actualWidth, 0);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
layoutParams.width = val;
v.setLayoutParams(layoutParams);
}
});
anim.setDuration(R.integer.card_flip_time_full);
anim.start();
With a breakpoint I see that val is always 728 (the width) and never changes. What is my fail?
I solved it:
final View v = cardViewHolder.cv;
int actualWidth = v.getMeasuredWidth();
ValueAnimator anim = ValueAnimator.ofInt(actualWidth, 0);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
// This needs to be declared as Integer instead of int
v.requestLayout(); // #mhenryk advice
Integer val = (Integer) valueAnimator.getAnimatedValue();
int value = val.intValue(); // Then get the int value
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
layoutParams.width = value;
v.setLayoutParams(layoutParams);
}
});
anim.setDuration(R.integer.card_flip_time_full);
anim.start();

Activity transition animation on root layout is executing on every EditText click

I am applying reveal effect on activity transition, when the startActivity is called the activity displayed through reveal effect. Activity is register activity which contains 3 editText, a imageView and a button to take image.
"The problem is whenever I select or click on editText the reveal effect animation is applying."
The animation is applied on root layout and animation is started in OnCreate method, I also tried applying animation through other lifecycle methods like onStart and onResume methods but it is working.
Answer is accepted with an pleasure.
Here is the snapshot of code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Name = (EditText) findViewById(R.id.enter_name_id);
Mobile = (EditText) findViewById(R.id.enter_mobile_id);
Email = (EditText) findViewById(R.id.enter_email_id);
imageView = (ImageView) findViewById(R.id.set_image);
LinearLayout rootview = (LinearLayout) findViewById(R.id.rootview);
rootview.addOnLayoutChangeListener(new View.OnLayoutChangeListener()
{
#Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
{
float finalRadius=
(float)Math.hypot(v.getWidth(),v.getHeight());
int cx1 = (v.getLeft() + v.getRight()) / 2;
int cy1 = (v.getTop() + v.getBottom()) / 2;
Animator anim = ViewAnimationUtils.createCircularReveal(v,
cx1, cy1, 0, finalRadius);
anim.setDuration(1000);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.start();
}
});
}
You can wait using a globalLayoutListener:
final LinearLayout rootview = (LinearLayout) findViewById(R.id.rootview);
rootview .getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
rootview .getViewTreeObserver().removeOnGlobalLayoutListener(this);
// remove listener
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
float finalRadius=
(float)Math.hypot(rootview.getWidth(),rootview.getHeight());
int cx1 = (rootview.getLeft() + rootview.getRight()) / 2;
int cy1 = (rootview.getTop() + rootview.getBottom()) / 2;
Animator anim = ViewAnimationUtils.createCircularReveal(v,
cx1, cy1, 0, finalRadius);
anim.setDuration(1000);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.start();
}
});
You might not need the check for the old version id you do not support lower than JELLY_BEAN
You can try like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
...
final LinearLayout rootview = (LinearLayout) findViewById(R.id.rootview);
ViewTreeObserver observer = rootview.getViewTreeObserver();
if (observer.isAlive()) {
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
showLayout(rootview)
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
rootview.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
rootview.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
}
...
...
}
private void showLayout(#NonNull View v) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
circularReveal(v);
} else {
v.setVisibility(View.VISIBLE);
}
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void circularReveal(#NonNull final View v) {
float finalRadius = (float) Math.hypot(v.getWidth(), v.getHeight());
int cx1 = (v.getLeft() + v.getRight()) / 2;
int cy1 = (v.getTop() + v.getBottom()) / 2;
Animator anim = ViewAnimationUtils.createCircularReveal(v, cx1, cy1, 0, finalRadius);
anim.setDuration(1000);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
v.setVisibility(View.VISIBLE);
anim.start();
}
In xml parent layout put this android:visibility="invisible"
The thing is that whenever you click on the image or the edit text the onLayoutChange method is being called. Just start your animation in onCreate rather than putting it in onLayoutChanged.
root.post(new Runnable() {
#Override
public void run() {
float finalRadius =
(float) Math.hypot(root.getWidth(), root.getHeight());
int cx1 = (root.getLeft() + root.getRight()) / 2;
int cy1 = (root.getTop() + root.getBottom()) / 2;
Animator anim = ViewAnimationUtils.createCircularReveal(root,
cx1, cy1, 0, finalRadius);
anim.setDuration(1000);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
anim.start();
}
});
Please try this.

How to animate a view programatically in android?

I am making an animated tutorial for the application. I am using a fragment over the activity to show image view sliding effect. I am using Translate animation to move the image view to the specific view to describe its working, but actually I want to animate the image view to move infinite times after reaching to the view.
Assume that view v is suppose to animate
public void ViewExapand(View v)
{
v.setVisibility(View.VISIBLE);
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(widthSpec, heightSpec);
ValueAnimator mAnimator = slideAnimator(0, v.getMeasuredHeight(),v);
mAnimator.start();
}
private ValueAnimator slideAnimator(int start, int end, final View v)
{
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
//Update Height
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
layoutParams.height = value;
v.setLayoutParams(layoutParams);
}
});
return animator;
}
You can also use ObjectAnimator
ObjectAnimator.ofInt(your view, "left", 100, 250)
.setDuration(250)
.start();

ValueAnimator/Expand/Collapse view not giving correct height

I have a search bar that expands when icon is clicked in toolbar. Now, everything is cool when it is expanded, everything works fine, the thing that is not good is that the height isnt good when the search bar is collapsed. The toolbar upon collapsing search bar has bigger height than it had. Here is the code:
private void expand(View view) {
//set Visible
view.setVisibility(View.VISIBLE);
final int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
final int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(widthSpec, heightSpec);
ValueAnimator mAnimator = slideAnimator(0, view.getMeasuredHeight(), view);
mAnimator.start();
}
private ValueAnimator slideAnimator(int start, int end, final View view) {
ValueAnimator animator = ValueAnimator.ofInt(start, end);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
//Update Height
int value = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = value;
view.setLayoutParams(layoutParams);
}
});
return animator;
}
private void collapse(final View view) {
int finalHeight = view.getHeight();
ValueAnimator mAnimator = slideAnimator(finalHeight, 0, view);
mAnimator.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animator) {
//Height=0, but it set visibility to GONE
view.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
mAnimator.start();
}
Everything is good here, check where you call method if you are changing background or any other property of the toolbar.

Android: how to expand and collapse cardView size in RecyclerView

I need to expand and collapse CardView size in RecyclerView at run time.
I use RecyclerView to display a list and CardView as RecyclerView Item.
As I am new to this concept please help me.
I have expand on run time. like this,
Setting my view on onCreateView()
descriptionCardView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver
.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
detailProductDescription.getViewTreeObserver().removeOnPreDrawListener(this);
// save the full height
descriptionViewFullHeight = detailProductDescription.getHeight();
// initially changing the height to min height
ViewGroup.LayoutParams layoutParams = descriptionCardView.getLayoutParams();
layoutParams.height = (int) getActivity().getResources().getDimension(R.dimen
.product_description_min_height);
descriptionCardView.setLayoutParams(layoutParams);
return true;
}
});
and onClickingcardview
#OnClick(R.id.detail_product_description)
void onProductDescriptionClicked(View view) {
toggleProductDescriptionHeight();
}
i am setting CardView Height to expand and collapse.
private int descriptionViewFullHeight;
private void toggleProductDescriptionHeight() {
int descriptionViewMinHeight = (int) getActivity().getResources().getDimension(R.dimen
.product_description_min_height);
if (descriptionCardView.getHeight() == descriptionViewMinHeight) {
// expand
ValueAnimator anim = ValueAnimator.ofInt(descriptionCardView.getMeasuredHeightAndState(),
descriptionViewFullHeight);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = descriptionCardView.getLayoutParams();
layoutParams.height = val;
descriptionCardView.setLayoutParams(layoutParams);
}
});
anim.start();
} else {
// collapse
ValueAnimator anim = ValueAnimator.ofInt(descriptionCardView.getMeasuredHeightAndState(),
descriptionViewMinHeight);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ViewGroup.LayoutParams layoutParams = descriptionCardView.getLayoutParams();
layoutParams.height = val;
descriptionCardView.setLayoutParams(layoutParams);
}
});
anim.start();
}
}

Categories

Resources