Objective
Since Android's TextView control doesn't offer a tilt (I haven't found it for a while), we can customize the control to implement it, and here's what we've achieved.
TextView text Tilt
In fact, the implementation is very simple, here we look at the implementation steps:
1, a new class Leantextview inherited TextView
public class Leantextview extends TextView {public int getmdegrees () {return mdegrees;
The public void setmdegrees (int mdegrees) {this.mdegrees = Mdegrees;
Invalidate ();
} private int mdegrees;
Public Leantextview {Super (context, NULL); Public Leantextview (context, AttributeSet attrs) {Super (context, Attrs, Android.
R.attr.textviewstyle);
This.setgravity (Gravity.center);
TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.leantextview);
Mdegrees = a.getdimensionpixelsize (r.styleable.leantextview_degree, 0);
A.recycle (); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeasures
PEC, Heightmeasurespec);
Setmeasureddimension (Getmeasuredwidth (), Getmeasuredwidth ());
} @Override protected void OnDraw (Canvas Canvas) {canvas.save ();
Canvas.translate (Getcompoundpaddingleft (), Getextendedpaddingtop ()); Canvas. Rotate (Mdegrees, this.getwidth ()/2f, this.getheight ()/2f);
Super.ondraw (canvas);
Canvas.restore (); }
}
2, the new styleable.xml in the values file
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<declare-styleable name= "Leantextview" >
<attr name= "degree" format= "Dimension"/>
</declare-styleable>
</resources>
3. Page layout, referencing custom controls
<com.aikaifa.leantextview
android:id= "@+id/lean"
android:layout_width= "Match_parent"
android: layout_height= "Wrap_content"
android:padding= "10DP"
android:text= "Love development"/>
Here we use the TextView record tilt angle, with seekbar dynamic change angle
<com.aikaifa.leantextview
android:id= "@+id/lean"
android:layout_width= "Match_parent"
android: layout_height= "Wrap_content"
android:padding= "10DP"
android:text= "Love development"/>
<textview
Android:id= "@+id/degrees"
android:layout_width= "match_parent"
android:layout_height= "Wrap_content"
android:padding= "10DP"
android:gravity= "center"/>
<seekbar
android:id= "@+id/sb_lean" "
android:layout_width=" match_parent "
android:layout_margintop=" 20DP "
android:layout_height=" Wrap_content "
android:max="
android:progress= "the"/>
Java code
mtext= (Leantextview) Findviewbyid (R.id.lean);
Degrees= (TextView) Findviewbyid (r.id.degrees);
SeekBar Sblean = (SeekBar) Findviewbyid (R.id.sb_lean);
Sblean.setonseekbarchangelistener (New Seekbar.onseekbarchangelistener () {
@Override public
Void Onprogresschanged (SeekBar SeekBar, int progress, Boolean fromuser) {
mtext.setmdegrees (progress);
Degrees.settext ("Tilt:" +progress);
}
@Override public
void Onstarttrackingtouch (SeekBar SeekBar) {
}
@Override public
Void Onstoptrackingtouch (SeekBar SeekBar) {
}
});
This makes the custom control about TextView text italic even if it's basically done, it's easy.
Project Structure Chart:
TextView text skew Project structure chart
Summarize
This is the full content of this article, I hope the content of this article for your Android developers can help, if you have questions you can message exchange.