22
33import android .animation .Animator ;
44import android .animation .AnimatorListenerAdapter ;
5+ import android .animation .TimeInterpolator ;
56import android .animation .ValueAnimator ;
67import android .content .Context ;
78import android .content .res .TypedArray ;
89import android .os .Build ;
910import android .util .AttributeSet ;
1011import android .view .ViewGroup ;
12+ import android .view .animation .AccelerateDecelerateInterpolator ;
1113import android .widget .TextView ;
1214
1315import java .lang .reflect .Field ;
1416
17+ import at .blogc .expandabletextview .BuildConfig ;
1518import at .blogc .expandabletextview .R ;
1619
1720/**
2831 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2932 * See the License for the specific language governing permissions and
3033 * limitations under the License.
34+ *
35+ * @author Cliff Ophalvens (Blogc.at)
3136 */
3237public class ExpandableTextView extends TextView
3338{
34- private static final int DEFAULT_DURATION = 750 ;
39+ // copy off TextView.LINES
3540 private static final int MAXMODE_LINES = 1 ;
3641
3742 private OnExpandListener onExpandListener ;
43+ private TimeInterpolator expandInterpolator ;
44+ private TimeInterpolator collapseInterpolator ;
3845
46+ private final int maxLines ;
3947 private long animationDuration ;
4048 private boolean animating ;
4149 private boolean expanded ;
42- private int maxLines ;
4350 private int originalHeight ;
4451
4552 public ExpandableTextView (Context context )
@@ -58,11 +65,15 @@ public ExpandableTextView(Context context, AttributeSet attrs, int defStyle)
5865
5966 // read attributes
6067 final TypedArray attributes = context .obtainStyledAttributes (attrs , R .styleable .ExpandableTextView , defStyle , 0 );
61- this .animationDuration = attributes .getInt (R .styleable .ExpandableTextView_animation_duration , DEFAULT_DURATION );
68+ this .animationDuration = attributes .getInt (R .styleable .ExpandableTextView_animation_duration , BuildConfig . DEFAULT_ANIMATION_DURATION );
6269 attributes .recycle ();
6370
6471 // keep the original value of maxLines
6572 this .maxLines = this .getMaxLines ();
73+
74+ // create default interpolators
75+ this .expandInterpolator = new AccelerateDecelerateInterpolator ();
76+ this .collapseInterpolator = new AccelerateDecelerateInterpolator ();
6677 }
6778
6879 @ Override
@@ -163,6 +174,9 @@ public void onAnimationEnd(final Animator animation)
163174 }
164175 });
165176
177+ // set interpolator
178+ valueAnimator .setInterpolator (this .expandInterpolator );
179+
166180 // start the animation
167181 valueAnimator
168182 .setDuration (this .animationDuration )
@@ -217,6 +231,9 @@ public void onAnimationEnd(final Animator animation)
217231 }
218232 });
219233
234+ // set interpolator
235+ valueAnimator .setInterpolator (this .collapseInterpolator );
236+
220237 // start the animation
221238 valueAnimator
222239 .setDuration (this .animationDuration )
@@ -255,6 +272,52 @@ public OnExpandListener getOnExpandListener()
255272 return onExpandListener ;
256273 }
257274
275+ /**
276+ * Sets a {@link TimeInterpolator} for expanding and collapsing.
277+ * @param interpolator the interpolator
278+ */
279+ public void setInterpolator (final TimeInterpolator interpolator )
280+ {
281+ this .expandInterpolator = interpolator ;
282+ this .collapseInterpolator = interpolator ;
283+ }
284+
285+ /**
286+ * Sets a {@link TimeInterpolator} for expanding.
287+ * @param expandInterpolator the interpolator
288+ */
289+ public void setExpandInterpolator (final TimeInterpolator expandInterpolator )
290+ {
291+ this .expandInterpolator = expandInterpolator ;
292+ }
293+
294+ /**
295+ * Returns the current {@link TimeInterpolator} for expanding.
296+ * @return the current interpolator, null by default.
297+ */
298+ public TimeInterpolator getExpandInterpolator ()
299+ {
300+ return this .expandInterpolator ;
301+ }
302+
303+ /**
304+ * Sets a {@link TimeInterpolator} for collpasing.
305+ * @param collapseInterpolator the interpolator
306+ */
307+ public void setCollapseInterpolator (final TimeInterpolator collapseInterpolator )
308+ {
309+ this .collapseInterpolator = collapseInterpolator ;
310+ }
311+
312+ /**
313+ * Returns the current {@link TimeInterpolator} for collapsing.
314+ * @return the current interpolator, null by default.
315+ */
316+ public TimeInterpolator getCollapseInterpolator ()
317+ {
318+ return this .collapseInterpolator ;
319+ }
320+
258321 /**
259322 * Is this {@link ExpandableTextView} expanded or not?
260323 * @return true if expanded, false if collapsed.
0 commit comments