Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. public class TextViewAnimator {
  2.  
  3. private TextValueAnimator textValueAnimator;
  4.  
  5. public static TextViewAnimator perLetter(TextView textView){
  6.  
  7. int steps = textView.getText().length();
  8. TextViewAnimator textViewAnimator =
  9. new TextViewAnimator(textView,
  10. new TextEvaluatorPerLetter(),
  11. new TextInterpolator(steps));
  12. return textViewAnimator;
  13. }
  14.  
  15. public static TextViewAnimator perWord(TextView textView){
  16.  
  17. int steps = textView.getText().toString().split(" ").length;
  18.  
  19. TextViewAnimator textViewAnimator =
  20. new TextViewAnimator(textView,
  21. new TextEvaluatorPerWord(),
  22. new TextInterpolator(steps));
  23. return textViewAnimator;
  24. }
  25.  
  26. public TextViewAnimator(TextView textView,
  27. TypeEvaluator typeEvaluator,
  28. TextInterpolator textInterpolator){
  29.  
  30. this.textValueAnimator = new TextValueAnimator(textView, textView.getText().toString());
  31. textValueAnimator.setEvaluator(typeEvaluator);
  32. textValueAnimator.setInterpolator(textInterpolator);
  33. }
  34.  
  35. private static class TextValueAnimator extends ValueAnimator implements ValueAnimator.AnimatorUpdateListener {
  36.  
  37. private WeakReference<TextView> weakTextView;
  38.  
  39. public TextValueAnimator(TextView textView, String text) {
  40.  
  41. weakTextView = new WeakReference<>(textView);
  42. setObjectValues(text);
  43. addUpdateListener(this);
  44. }
  45.  
  46. @Override
  47. public void onAnimationUpdate(ValueAnimator animation) {
  48. String text = (String) animation.getAnimatedValue();
  49. TextView textView = weakTextView.get();
  50. if(textView != null) {
  51. textView.setText(text);
  52. }
  53. }
  54. }
  55.  
  56. private static class TextEvaluatorPerLetter implements TypeEvaluator {
  57.  
  58. @Override
  59. public Object evaluate(float fraction, Object startValue, Object endValue) {
  60. int step = (int) fraction;
  61. return ((String) endValue).substring(0, step);
  62. }
  63. }
  64.  
  65. private static class TextEvaluatorPerWord implements TypeEvaluator {
  66.  
  67. private String[] words;
  68. @Override
  69. public Object evaluate(float fraction, Object startValue, Object endValue) {
  70.  
  71. int step = (int) fraction;
  72. if(words == null){
  73. words = ((String) endValue).split(" ");
  74. }
  75. String textAtStep = "";
  76. for (int i = 1; i <= step; i++) {
  77. textAtStep += words[i-1] + " ";
  78. }
  79.  
  80. return textAtStep;
  81. }
  82. }
  83.  
  84. private static class TextInterpolator implements TimeInterpolator {
  85.  
  86. private int steps;
  87. public TextInterpolator(int steps) {
  88.  
  89. this.steps = steps;
  90. }
  91. @Override
  92. public float getInterpolation(float input) {
  93. return input * steps;
  94. }
  95. }
  96.  
  97. public void start(){
  98. textValueAnimator.start();
  99. }
  100. public void cancel(){
  101. textValueAnimator.cancel();
  102. }
  103. public void end(){
  104. textValueAnimator.end();
  105. }
  106.  
  107. @RequiresApi(19)
  108. public void pause(){
  109. textValueAnimator.pause();
  110. }
  111. @RequiresApi(19)
  112. public void resume(){
  113. textValueAnimator.resume();
  114. }
  115. @RequiresApi(19)
  116. public boolean isStarted(){
  117. return textValueAnimator.isStarted();
  118. }
  119. @RequiresApi(19)
  120. public float getAnimatedFraction(){
  121. return textValueAnimator.getAnimatedFraction();
  122. }
  123. public void setRepeatCount(int value){
  124. textValueAnimator.setRepeatCount(value);
  125. }
  126. public void setRepeatMode(int repeatMode){
  127. textValueAnimator.setRepeatMode(repeatMode);
  128. }
  129. public void setDuration(long duration){
  130. textValueAnimator.setDuration(duration);
  131. }
  132. public void setStartDelay(long startDelay){
  133. textValueAnimator.setStartDelay(startDelay);
  134. }
  135. public void addUpdateListener(ValueAnimator.AnimatorUpdateListener listener){
  136. textValueAnimator.addUpdateListener(listener);
  137. }
  138. public void removeUpdateListener(ValueAnimator.AnimatorUpdateListener listener){
  139. textValueAnimator.removeUpdateListener(listener);
  140. }
  141. public boolean isRunning(){
  142. return textValueAnimator.isRunning();
  143. }
  144. }
  145.  
  146. public class MainActivity extends AppCompatActivity {
  147.  
  148. TextView textView;
  149. Button button;
  150. TextViewAnimator textViewAnimator;
  151.  
  152. @Override
  153. protected void onCreate(Bundle savedInstanceState) {
  154. super.onCreate(savedInstanceState);
  155. setContentView(R.layout.activity_main);
  156.  
  157. textView = (TextView) findViewById(R.id.text1);
  158.  
  159. textViewAnimator = TextViewAnimator.perLetter(textView);
  160. textViewAnimator.setDuration(5000);
  161. textViewAnimator.setRepeatCount(2);
  162. textViewAnimator.setRepeatMode(ValueAnimator.REVERSE);
  163.  
  164. button = (Button)findViewById(R.id.button);
  165. button.setOnClickListener(new View.OnClickListener() {
  166. @Override
  167. public void onClick(View v) {
  168. textViewAnimator.start();
  169. }
  170. });
  171. }
  172. }
  173.  
  174. public class Typewriter extends TextView {
  175.  
  176. private CharSequence mText;
  177. private int mIndex;
  178. private long mDelay = 500; //Default 500ms delay
  179.  
  180.  
  181. public Typewriter(Context context) {
  182. super(context);
  183. }
  184.  
  185. public Typewriter(Context context, AttributeSet attrs) {
  186. super(context, attrs);
  187. }
  188.  
  189. private Handler mHandler = new Handler();
  190. private Runnable characterAdder = new Runnable() {
  191. @Override
  192. public void run() {
  193. setText(mText.subSequence(0, mIndex++));
  194. if(mIndex <= mText.length()) {
  195. mHandler.postDelayed(characterAdder, mDelay);
  196. }
  197. }
  198. };
  199.  
  200. public void animateText(CharSequence text) {
  201. mText = text;
  202. mIndex = 0;
  203.  
  204. setText("");
  205. mHandler.removeCallbacks(characterAdder);
  206. mHandler.postDelayed(characterAdder, mDelay);
  207. }
  208.  
  209. public void setCharacterDelay(long millis) {
  210. mDelay = millis;
  211. }
  212. }
  213.  
  214. <genesysgeneration.pokemaos.Typewriter
  215. android:id="@+id/meuTxt"
  216. android:layout_width="match_parent"
  217. android:layout_height="wrap_content"/>
  218.  
  219. Typewriter t = (Typewriter) findViewById(R.id.meuTxt);
  220. t.setCharacterDelay(100);
  221. t.animateText("Olha só que legal");
  222.  
  223. TextAnimatedView textAnimatedView = (TextAnimatedView) findViewById(R.id.tv);
  224. textAnimatedView.setCharacterDelay(150);
  225. textAnimatedView.animPerWord("Desta forma vai funcionar como esperado");
  226.  
  227. <seu.pacote.TextAnimatedView
  228. android:id="@+id/tv"
  229. android:layout_width="wrap_content"
  230. android:layout_height="wrap_content"/>
  231.  
  232. public class TextAnimatedView extends TextView {
  233.  
  234. private Handler handler = new Handler();
  235. private StringBuilder stringBuilder = new StringBuilder();
  236. private CharSequence text;
  237. private String[] arr;
  238. private int i;
  239. private long delay = 450;
  240.  
  241. public TextAnimatedView(Context context) {
  242. super(context);
  243. }
  244.  
  245. public TextAnimatedView(Context context, AttributeSet attrs) {
  246. super(context, attrs);
  247. }
  248.  
  249. private Runnable runnable = new Runnable() {
  250. @Override
  251. public void run() {
  252. setText(text.subSequence(0, i++));
  253. if (i <= text.length()) {
  254. handler.postDelayed(runnable, delay);
  255. }
  256. }
  257. };
  258.  
  259. public void animPerLetter(CharSequence text) {
  260. this.text = text;
  261. i = 0;
  262.  
  263. setText("");
  264. handler.removeCallbacks(runnable);
  265. handler.postDelayed(runnable, delay);
  266. }
  267.  
  268. private Runnable runnablePerWord = new Runnable() {
  269. @Override
  270. public void run() {
  271.  
  272. stringBuilder.append(arr[i++]).append(" ");
  273. setText(stringBuilder.toString());
  274. if (i < arr.length) {
  275. handler.postDelayed(runnablePerWord, delay);
  276. }
  277. }
  278. };
  279.  
  280. public void animPerWord(CharSequence text) {
  281. this.text = text;
  282. i = 0;
  283.  
  284. setText("");
  285. arr = text.toString().split(" ");
  286. handler.removeCallbacks(runnablePerWord);
  287. handler.postDelayed(runnablePerWord, delay);
  288. }
  289.  
  290. public void setCharacterDelay(long delay) {
  291. this.delay = delay;
  292. }
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement