Android custom TextView to achieve text tilt effect _android

Source: Internet
Author: User

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.

Related Article

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.