Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Most flexible (and pretty easy) way to create custom animation is to extend <code>Animation</code> class.</p> <p>In general:</p> <ol> <li>Set duration of your animation using <code>setDuration()</code> method.</li> <li>Optionally set the interpolator for your animation using <code>setInterpolator()</code> (for exapmle you can use <code>LinearInterpolator</code> or <code>AccelerateInterpolator</code> etc.)</li> <li>Override <code>applyTransformation</code> method. Here we interested in <code>interpolatedTime</code> variable which changes between 0.0 and 1.0 and represent the your animation progress.</li> </ol> <p>Here is an example (I'm using this class to change ofsset of my <code>Bitmap</code>. <code>Bitmap</code> itself is drawn in <code>draw</code> method):</p> <pre><code>public class SlideAnimation extends Animation { private static final float SPEED = 0.5f; private float mStart; private float mEnd; public SlideAnimation(float fromX, float toX) { mStart = fromX; mEnd = toX; setInterpolator(new LinearInterpolator()); float duration = Math.abs(mEnd - mStart) / SPEED; setDuration((long) duration); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); float offset = (mEnd - mStart) * interpolatedTime + mStart; mOffset = (int) offset; postInvalidate(); } } </code></pre> <p>Also you can modify <code>View</code> by using <code>Transformation#getMatrix()</code>.</p> <p><strong>UPDATE</strong></p> <p>In case if you're using Android Animator framework (or compatibility implementation - <code>NineOldAndroids</code>) you can just declare setter and getter for your custom <code>View</code> property and animate it directly. Here is an another example:</p> <pre><code>public class MyView extends View { private int propertyName = 50; /* your code */ public int getPropertyName() { return propertyName; } public void setPropertyName(int propertyName) { this.propertyName = propertyName; } /* There is no need to declare method for your animation, you can, of course, freely do it outside of this class. I'm including code here just for simplicity of answer. */ public void animateProperty() { ObjectAnimator.ofInt(this, "propertyName", 123).start(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload