80653

How to animate to wrap_content?

<h3>Question</h3>

Is it possible to animate using ValueAnimator to wrap_content? this only seems to work with constant values.

public static void valueAnimate(final View obj, int from, int to, Interpolator interpolator, long duration, long delay){ ValueAnimator anim = ValueAnimator.ofInt(from, to); anim.setInterpolator(interpolator == null ? DEFAULT_INTERPOLATOR : interpolator); anim.setDuration(duration); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); obj.getLayoutParams().height = value.intValue(); obj.requestLayout(); } }); anim.setStartDelay(delay); anim.start(); }

How could I pass a the to parameter as wrap_content?

Animator.valueAnimate(mapUtilsContainer, CURR_MAPUTILC_H, 800, OVERSHOOT, 300, 0);
<h3>Answer1:</h3>

You can do something like this. Pass a view that is set initially to gone.

public static void expand(final View view) { view.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); final int targetHeight = view.getMeasuredHeight(); // Set initial height to 0 and show the view view.getLayoutParams().height = 0; view.setVisibility(View.VISIBLE); ValueAnimator anim = ValueAnimator.ofInt(view.getMeasuredHeight(), targetHeight); anim.setInterpolator(new AccelerateInterpolator()); anim.setDuration(1000); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); layoutParams.height = (int) (targetHeight * animation.getAnimatedFraction()); view.setLayoutParams(layoutParams); } }); anim.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { // At the end of animation, set the height to wrap content // This fix is for long views that are not shown on screen ViewGroup.LayoutParams layoutParams = view.getLayoutParams(); layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT; } }); anim.start(); }
<h3>Answer2:</h3>

An easy solution is to use android:animateLayoutChanges attribute in your layout which includes your component. This is for Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN (Android 4.3). For example, I have one EditText which needs to change from a certain height to wrap_content.

<ol><li>

Add the android:animateLayoutChanges attribute to my ScrollView.

< ScrollView ... android:animateLayoutChanges="true"> </li> <li>

Add this in your onCreateView()

scrollView.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)

</li> </ol>

Then you will see a fairly smooth change of the EditText height.

<ol start="3"><li>

Code to change the height of EditText to wrap_content

fun wrapText() { val layoutParams = this.layoutParams layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT this.layoutParams = layoutParams isExpanded = false } </li> </ol>

来源:https://stackoverflow.com/questions/55251650/how-to-animate-to-wrap-content

Recommend

  • How to animate to wrap_content?
  • Load new data when refresh RecyclerView
  • Get tapped cell in UITapGestureRecognizer handler
  • passing member-function as argument to function-template
  • Eclipse C macro automatic formatting
  • TensorFlow : How do i find my output node in my Tensorflow trained model?
  • error: XML document must have a top level element
  • Is there a better approach to use BeautifulSoup in my python web crawler codes?
  • SQL Query to conditionally retrieve records based on a key value list
  • Matplotlib: subplot
  • JavaFX TabPane System like in a browser
  • Bootstrap select input area bigger than parent
  • File structure for PHP-based website
  • Create intance of any C# class by generic way
  • CRM Dynamics How to set short list - long list relationship
  • How to Implement INotifyPropertyChanged on Entity Framework Entity
  • Starting Bootstrap tour with a button after ending tour once
  • How to work with Mailgun API in CodeIgniter; Forbidden error in curl_exe()
  • Reveal Icon Button - Bootstrap 3
  • How to detect beginning of line, or: “The name 'getCharPositionInLine' does not exist in t
  • ODBC connection to an .accdb file
  • Why is search query table displaying table Headers, and not data in BeautifulSoup (Python)?
  • What is the diff. between default.properties and project.properties?
  • Making query to find nearest multiple(Lat,Long) from the single(Lat,Long)
  • Using loops in Jasmine (with injected service)
  • Reader monad - how does it conform to Monad interface?
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • What is the difference between dynamically creating a script tag and statically embed a script tag?
  • C++ STL stack pop operation giving segmentation fault
  • Drag and drop unicode TText in DelphiXe4
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}