I'm using my own version of https://github.com/mdg-iitr/RotatingText in order to show a rotating text widget. A video is available in this GitHub, allowing you to see the animation. The idea is to set up rows of words. The rows are shown row after row. The whole row rotates (so do its words). A row is shown after the previous row when the rotation animation of the latter has ended.
My problem
I use a DynamicLayout
to show the rows of text. Remember: the rows must rotate.
My problem is: obviously, I can't use the method canvas.drawTextOnPath(dynamicLayoutObject)
. So what I do is: dynamicLayoutObjec.draw(canvas);
. But there isn't any animation then. Indeed, the text (so the DynamicLayout
that contains it) must rotate.
Expected result
The DynamicLayout
(in fact, its text) must be animated (a rotation). The rotation can be find in the illustration of the Github original repo given at the beginning of this SO Question (https://github.com/mdg-iitr/RotatingText).
My question
I don't know how to make my DynamicLayout
(and/or its text) rotate along my path.
Minimal and Testable Example
I have modified the original RotatingText library 8 months ago approx. in order to simplify it (less classes, less methods, no unused methods, etc.). Indeed, I only have two classes:
RotatingTextSwitcher
, which is the XML widgetAnd
Rotatable
, which contains the array of strings to be rotated.A .XML layout containing the XML widget
RotatingTextSwitcher
in order to test itA
Fragment
inflating the previously mentionned layout, setting up the words of each rotating row, and showing them.
To test it, create an activity showing the fragment given below, which in turn uses the other sources, presented above.
Rotatable class
import android.graphics.Path;
import android.view.animation.Interpolator;
public class Rotatable {
private final String[] text;
private final int update_duration;
private int animation_duration;
private Path path_in, path_out;
private int currentWordNumber;
private Interpolator interpolator;
public Rotatable(int update_duration, int animation_duration, Interpolator interpolator, String... text) {
this.update_duration = update_duration;
this.animation_duration = animation_duration;
this.text = text;
this.interpolator = interpolator;
currentWordNumber = -1;
}
private int nextWordNumber() {
currentWordNumber = (currentWordNumber + 1) % text.length;
return currentWordNumber;
}
String nextWord() {
return text[nextWordNumber()];
}
Path getPathIn() {
return path_in;
}
void setPathIn(Path path_in) {
this.path_in = path_in;
}
Path getPathOut() {
return path_out;
}
void setPathOut(Path path_out) {
this.path_out = path_out;
}
int getUpdateDuration() {
return update_duration;
}
int getAnimationDuration() {
return animation_duration;
}
Interpolator getInterpolator() { return interpolator; }
}
RotatingTextSwitcher class
package libs.rotating_text;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.text.DynamicLayout;
import android.text.Layout;
import android.text.SpannableStringBuilder;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import androidx.appcompat.widget.AppCompatTextView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import androidx.annotation.Nullable;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
public class RotatingTextSwitcher extends AppCompatTextView {
Disposable disposable;
private TextPaint textPaint = new TextPaint();
private String text = "", old_text = "";
SpannableStringBuilder base = new SpannableStringBuilder(text);
SpannableStringBuilder base_old = new SpannableStringBuilder(old_text);
private DynamicLayout layout = new DynamicLayout(base, textPaint,500, Layout.Alignment.ALIGN_CENTER,1.0F,0.0F,true);
private DynamicLayout layout_old = new DynamicLayout(base_old, textPaint,500, Layout.Alignment.ALIGN_CENTER,1.0F,0.0F,true);
private Rotatable rotatable;
private Paint paint;
private Path path_in, path_out;
public RotatingTextSwitcher(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
paint = getPaint();
paint.setAntiAlias(true);
}
public void setRotatable(Rotatable rotatable) {
this.rotatable = rotatable;
initialize();
}
private void initialize() {
text = rotatable.nextWord();
base.clear();
base.append(text);
old_text = text;
base_old.clear();
base_old.append(old_text);
setUpPath();
setDisposable();
scheduleUpdateTextTimer();
}
private void setDisposable() {
disposable = Observable.interval(1000 / 60, TimeUnit.MILLISECONDS, Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) {
invalidate();
}
});
}
private void setUpPath() {
post(new Runnable() {
@Override
public void run() {
path_in = new Path();
path_in.moveTo(0.0f, getHeight() - paint.getFontMetrics().bottom);
path_in.lineTo(getWidth(), getHeight() - paint.getFontMetrics().bottom);
rotatable.setPathIn(path_in);
path_out = new Path();
path_out.moveTo(0.0f, (2 * getHeight()) - paint.getFontMetrics().bottom);
path_out.lineTo(getWidth(), (2 * getHeight()) - paint.getFontMetrics().bottom);
rotatable.setPathOut(path_out);
}
});
}
private void scheduleUpdateTextTimer() {
Timer update_text_timer = new Timer();
update_text_timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
((Activity) getContext()).runOnUiThread(new Runnable() {
@Override
public void run() {
animateInHorizontal();
animateOutHorizontal();
old_text = text;
base_old.clear();
base_old.append(old_text);
text = rotatable.nextWord();
base.clear();
base.append(text);
}
});
}
}, rotatable.getUpdateDuration(), rotatable.getUpdateDuration());
}
@Override
protected void onDraw(Canvas canvas) {
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
float size = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 32, metrics);
textPaint.setTextSize(size);
if (rotatable.getPathIn() != null) {
layout.draw(canvas);
//canvas.drawTextOnPath(text, rotatable.getPathIn(), 0.0f, 0.0f, paint);
}
if (rotatable.getPathOut() != null) {
layout_old.draw(canvas);
//canvas.drawTextOnPath(old_text, rotatable.getPathOut(), 0.0f, 0.0f, paint);
}
setHeight(layout.getHeight() + layout_old.getHeight());
}
private void animateInHorizontal() {
ValueAnimator animator = ValueAnimator.ofFloat(0.0f, getHeight());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
path_in = new Path();
path_in.moveTo(0.0f, (Float) valueAnimator.getAnimatedValue() - paint.getFontMetrics().bottom);
path_in.lineTo(getWidth(), (Float) valueAnimator.getAnimatedValue() - paint.getFontMetrics().bottom);
rotatable.setPathIn(path_in);
}
});
animator.setInterpolator(rotatable.getInterpolator());
animator.setDuration(rotatable.getAnimationDuration());
animator.start();
}
private void animateOutHorizontal() {
ValueAnimator animator = ValueAnimator.ofFloat(getHeight(), getHeight() * 2.0f);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
path_out = new Path();
path_out.moveTo(0.0f, (Float) valueAnimator.getAnimatedValue() - paint.getFontMetrics().bottom);
path_out.lineTo(getWidth(), (Float) valueAnimator.getAnimatedValue() - paint.getFontMetrics().bottom);
rotatable.setPathOut(path_out);
}
});
animator.setInterpolator(rotatable.getInterpolator());
animator.setDuration(rotatable.getAnimationDuration());
animator.start();
}
}
A layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<libs.rotating_text.RotatingTextSwitcher
android:id="@+id/textView_presentation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:textSize="35sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
A Fragment
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.androidframework.R;
import libs.rotating_text.Rotatable;
import libs.rotating_text.RotatingTextSwitcher;
public class FragmentHomeSlide extends Fragment {
private View inflated;
private int drawable_id;
private String[] text_presentation;
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
assert getArguments() != null;
text_presentation = new String[];
text_presentation[0] = "One row is set up with several words";
text_presentation[1] = "This is another row";
}
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
inflated = inflater.inflate(R.layout.home_slide, container, false);
setWidgets();
return inflated;
}
private void setWidgets() {
final RotatingTextSwitcher rotating_presentation = inflated.findViewById(R.id.textView_presentation);
rotating_presentation.setRotatable(new Rotatable(1000, 500, new AccelerateInterpolator(), text_presentation));
}
}
DynamicLayout
. So, now I use it... but I can't make it rotate along the path. I've edited the question. The bounty is still available :-) . – Misprize