TextView all have run lanterns effect, if say let you go to monitor the performance of the Lantern, I think this demand a bit two, but also to achieve. Ideas: 1. Customizing view inheritance TextView This method is too cumbersome, just listening to a marquee. 2. Customize the span. Easy to use, extend the functionality of TextView.
The key code is as follows:
@Override public void draw (canvas canvas, charsequence text, int start, int end, & nbsp; float x, int top, int y, int bottom, paint paint) { &nbs p; canvas.save (); Canvas.drawtext (text.toString (), X- StartX, y, paint); Canvas.restore (); int measuretext = (int) paint.measuretext (text, 0, text.length ()); if (valueanimator = = null) { InitAnimator ( Measuretext-mview.getwidth ()); } } private int startX = 0; private final static int defult_duration =; Priv ate int duration = defult_duration; @Override public int GetSize (paint paint, charsequence text, int start, int end, & nbsp; fontmetricsint FM) { return 40; } private void initanimator (int width) { valueanimator = valueanimator.ofint (0, width, 0); Valueanimator.addupdatelistener (New Animatorupdatelistener () { @Override public void Onanimationupdate ( Valueanimator animation) { StartX = (Integer) animation.getanimatedvalue (); mView.invalidate (); } }); Valueanimator.setinterpolator (New Linearinterpolator ()); valueanimator.setduration (Duration); Valueanimator.addlistener (New Animatorlisteneradapter () { @Override public void Onanimationrepeat (Animator Animation) { if (Measyspanlistener! = null) { Measyspanlistener.over (); } } }); Valueanimator.setrepeatcount (Integer.max_value); Valueanimator.start (); }
span itself is a function enhancement to textview. Use: string Text = "This is used to test the textview of the marquee effect, want to listen to his run out of events with." "; spannablestring spannablestring = new Spannablestring (text); Easyspan easyspan =new easyspan (mainactivity.this,test); easyspan.setduration (10000); Easyspan.seteasyspanlistener (New Easyspanlistener () { @Override public void over () { log.i ("Test", "over"); } } ); Spannablestring.setspan (easyspan, 0, Text.length (), Spannable.SPAN_ inclusive_inclusive); Test.settext (spannablestring);
Very simple small function, everyone read the code bar. Source: Https://github.com/flyme2012/EasyListenerSpan
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Custom TextView Marquee Monitor