Sometimes Android comes with controls that don't meet some of our requirements, and we need to customize the controls to implement these features. For example, the need for a textview in the word tilt a certain angle, you need to customize the TextView.
Package Com.leigo.ratatetextview;import Android.content.context;import Android.content.res.typedarray;import Android.graphics.canvas;import Android.util.attributeset;import Android.view.gravity;import android.widget.textview;/** * Created by Administrator on 2014/9/9. */public class Rotatetextview extends TextView {private static final int default_degrees = 0; private int mdegrees; Public Rotatetextview (Context context) {Super (context, NULL); } public Rotatetextview (context context, AttributeSet Attrs) {Super (context, Attrs, Android. R.attr.textviewstyle); This.setgravity (Gravity.center); TypedArray a = Context.obtainstyledattributes (Attrs, R.styleable.rotatetextview); Mdegrees = A.getdimensionpixelsize (R.styleable.rotatetextview_degree, default_degrees); A.recycle (); } @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeas Urespec, 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 (); } public void setdegrees (int degrees) {mdegrees = degrees; }}
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name= "Rotatetextview" > <attr name= "degree" format= "Dimension"/> </declare-styleable></resources>
Usage:
1.xml
<com.leigo.ratatetextview.rotatetextview xmlns:app= "Http://schemas.android.com/apk/res-auto" android:id= " @+id/text "android:text=" @string/hello_world "app:degree=" 10DP "android:padding=" 10DP "Android : layout_width= "wrap_content" android:layout_height= "Wrap_content"/></span>
2.java
Rotatetextview mText = (rotatetextview) Findviewbyid (r.id.text); Mtext.setdegrees;</span>
Project Address:
Https://coding.net/u/leigo/p/RotateTextView/git
Android text Tilt