PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
3 Posts
Electronics
68 Posts
Hardware
120 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
72 Posts
Symbian
2 Posts
Tubes
18 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


uRADMonitor - Online Radiation monitoring station | 14752 Views | Rate 70.58
uRADMonitor - Online Radiation monitoring station
NMEA GPS Library for AVR Microcontrollers | 4743 Views | Rate 57.84
NMEA GPS Library for AVR Microcontrollers
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 17547 Views | Rate 57.34
Bluetooth and iOS - Use Bluetooth in your iPhone apps
Repairing a Victoreen CDV-700 6B Dosimeter  | 107 Views | Rate 53.5
Repairing a Victoreen CDV-700 6B Dosimeter
Building a robot – Part 2 | 4487 Views | Rate 44.87
Building a robot – Part 2
Programmatically Injecting Events on Android - Part 2 | 4796 Views | Rate 44.41
Programmatically Injecting Events on Android - Part 2
Simple Switched power Supplies | 15732 Views | Rate 41.18
Simple Switched power Supplies
CDV-742 Dosimeter and CDV-750 Dosimeter Charger | 79 Views | Rate 39.5
CDV-742 Dosimeter and CDV-750 Dosimeter Charger

 
  

Android Vertical TextView / Custom Angle Text


By Radu Motisan Posted on December 15th, 2010 , 1955 Views (Rate 2.21)



  




It is easy to derive the View class and create a custom Textview that can support custom text angles, including vertical text!

  1.  
  2. class CustomTextView extends View {
  3. ...
  4. @Override
  5. protected void onDraw(Canvas canvas) {
  6. Paint paint = new Paint();
  7. paint.setTypeface(m_tTypeface);
  8. paint.setStyle(Paint.Style.FILL);
  9. paint.setColor(m_nColor);
  10. //paint.setShadowLayer(1, 0, 1, Color.parseColor("#000000"));
  11. paint.setTextSize(m_nSize);
  12. canvas.rotate(m_nRotationAngle,m_nRotationW,m_nRotationH);
  13. canvas.drawText(m_szText, 0, 0, paint);
  14. super.onDraw(canvas);
  15. }
  16. ...
  17.  

The resulting CustomTextView Class can be used as simple as a normal TextView class:

  1.  
  2. CustomTextView tv3 = new CustomTextView(this);
  3. //etc.
  4.  

See the left pic to understand the potential of this technique.

You need to be careful when selecting the rotation center, as it can put your text off-screen:





  1.  
  2. tv3.SetRotation(-90,120,90); //angle and rotation center
  3.  

The first argument is the angle, it is in degrees, it takes negative values as well ( -90 = 270 , if you imagine the trigonometric circle). The other two parameters are the rotation center (x,y) coordinates.

Here's the complete code:
CustomAngleText






  

More on PocketMagic:

Tube: USN-5J29 | 288 Views | Rate 28.8
Tube: USN-5J29
Programmatically Injecting Events on Android - Part 1 | 9898 Views | Rate 25.78
Programmatically Injecting Events on Android - Part 1
Tube: 1B40 | 227 Views | Rate 25.22
Tube: 1B40
Atmega8 and enc28J60 for ethernet support | 7061 Views | Rate 24.18
Atmega8 and enc28J60 for ethernet support
How to set the AVR Fusebits | 1626 Views | Rate 23.91
How to set the AVR Fusebits
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164 | 1988 Views | Rate 23.12
ATMega128 and HD44780 LCD using 3 Wires with the 74HC164

3 Responses to “Android Vertical TextView / Custom Angle Text”

  1. 1
    Anh Tuan:

    Thank u very much! Save me much time to create on my own.

  2. 2
    Dinesh:

    how to assign the resulted custom view in my already defined layout

  3. 3
    AgeOfAndroid:

    Hello

    Thanks for your golden posting…
    I am building textview that user can rotate text using his/her two figures.
    To solve, I used your code.

    But it is not resized according to rotated text size….
    So after rotating, text is croped by old textview size.

    How to solve that?
    I sent you my code.

    public class AngledTextView extends TextView {
    private short mAngle;

    public AngledTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mAngle = 0;
    }

    public void setAngle(short angle) {
    mAngle = (short) Math.max(0, Math.min(360, angle));
    this.invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setTypeface(this.getTypeface());
    paint.setStyle(Paint.Style.FILL);
    paint.setTextSize(this.getTextSize());
    canvas.rotate(mAngle, this.getWidth()/2, this.getHeight()/2);
    canvas.drawText(this.getText().toString(), 0, 0, paint);
    super.onDraw(canvas);
    }
    }

    What’s wrong?…..

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string mzZLtR to the field below: