Android text Tilt

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.