Skip to content

Commit 1256790

Browse files
committed
Merge branch 'feature/interpolator_support' into develop
2 parents db91667 + 7462ed6 commit 1256790

3 files changed

Lines changed: 76 additions & 3 deletions

File tree

app/src/main/java/at/blogc/android/activities/MainActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.os.Bundle;
55
import android.util.Log;
66
import android.view.View;
7+
import android.view.animation.OvershootInterpolator;
78
import android.widget.Button;
89

910
import at.blogc.android.views.ExpandableTextView;
@@ -40,6 +41,13 @@ protected void onCreate(Bundle savedInstanceState)
4041
// set animation duration via code, but preferable in your layout files by using the animation_duration attribute
4142
expandableTextView.setAnimationDuration(750L);
4243

44+
// set interpolators for both expanding and collapsing animations
45+
expandableTextView.setInterpolator(new OvershootInterpolator());
46+
47+
// or set them separately
48+
expandableTextView.setExpandInterpolator(new OvershootInterpolator());
49+
expandableTextView.setCollapseInterpolator(new OvershootInterpolator());
50+
4351
// toggle the ExpandableTextView
4452
buttonToggle.setOnClickListener(new View.OnClickListener()
4553
{

expandabletextview/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ android {
1616
targetSdkVersion 23
1717
versionCode 1
1818
versionName fullVersion
19+
20+
buildConfigField 'int', 'DEFAULT_ANIMATION_DURATION', '750'
1921
}
2022
buildTypes {
2123
release {

expandabletextview/src/main/java/at/blogc/android/views/ExpandableTextView.java

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
import android.animation.Animator;
44
import android.animation.AnimatorListenerAdapter;
5+
import android.animation.TimeInterpolator;
56
import android.animation.ValueAnimator;
67
import android.content.Context;
78
import android.content.res.TypedArray;
89
import android.os.Build;
910
import android.util.AttributeSet;
1011
import android.view.ViewGroup;
12+
import android.view.animation.AccelerateDecelerateInterpolator;
1113
import android.widget.TextView;
1214

1315
import java.lang.reflect.Field;
1416

17+
import at.blogc.expandabletextview.BuildConfig;
1518
import at.blogc.expandabletextview.R;
1619

1720
/**
@@ -28,18 +31,22 @@
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
*/
3237
public 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

Comments
 (0)