Android custom control implements display text functionality
Custom control-– Text display
One Goal, one passion!
Objective:
The effect you want to achieve today. Let's show the text one at a time. On the effect chart:
Implementation principle:
1, get the text you want to display.
2, calculate the rate of text display
Font Display speed v = Total font length/total display time
3, the text is displayed on the control according to the rate.
Custom View:
public class Printtextview extends TextView {/** * font display time * * Private int DURATION = 8000;
Public Printtextview {This (context, NULL);
Public Printtextview (context, AttributeSet attrs) {This (context, attrs, 0); Printtextview (context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr)
; public void printstring (String str) {if (str!= null && str!= "") {//length of string final int
Lenght = Str.length ();
Final char[] C = new char[str.length ()];
Converts a string to a character array for (int i = 0; i < str.length (); i++) {C[i] = Str.charat (i);
} valueanimator animator = valueanimator.offloat (0, 1); Animator.addupdatelistener (New Valueanimator.animatorupdatelistener () {@Override public void Onanimationu Pdate (Valueanimator animation) {//font display speed v = Total font length/Total display time float v = (float) lenGht/(float) DURATION;
Animation execution speed Float fraction = (float) animation.getanimatedvalue ();
The number of different stages of the animation the font should display int s = (int) (v * fraction * DURATION);
SetText (c, 0, s);
}
});
Animator.setduration (DURATION);
Animator.start ();
}
}
}
Run up:
public class Scaleactivity extends Appcompatactivity {
private printtextview print_text;
String str = "I kiss you Good-bye, in the street of no one." I Kissed you goodbye in the frantic night. This wave gives you 103 points, "+
" a more tolerant, more patience, more important is a more than a father love. ";
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_scale);
Initview ();
}
Print_text = (Printtextview) Findviewbyid (r.id.print_text);
Print_text.printstring (str);
}
R.layout.activity_scale Layout file
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/ Apk/res/android "
xmlns:tools=" Http://schemas.android.com/tools "
android:layout_width=" Match_parent "
android:layout_height= "match_parent"
android:layout_margin= "20DP"
android:orientation= "vertical"
tools:context= "Com.example.customview.activity.ScaleActivity" >
< Com.example.customview.view.printTextView
android:id= "@+id/print_text"
android:layout_width= "Match_ Parent "
android:layout_height=" wrap_content "
/>
</LinearLayout>
Ok. Our text is ready to be printed and displayed to the screen.
Thank you for reading, I hope to help you, thank you for your support for this site!