2

I want to create a rotated be X degrees title in android.

Something like that:

enter image description here

I have tried this class

public class AngledTextView extends Button {
    public AngledTextView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // Save the current matrix
        canvas.save();
        // Rotate this View at its center
        canvas.rotate(-45, this.getWidth() / 2, this.getHeight() / 2);
        // Draw it
        super.onDraw(canvas);
        // Restore to the previous matrix
        canvas.restore();
    }
}

in this layout:

but got chopped text.

enter image description here

How can i fix this?

Loktar
  • 34,764
  • 7
  • 90
  • 104
Elad Benda2
  • 13,852
  • 29
  • 82
  • 157

3 Answers3

0

Instead of rotating the canvas at its center, rotate it at the bottom left and rotate it the number of degrees of the angle from the bottom left to the top right.

float angle = Math.tan((float)this.getHeight() / (float)this.getWidth());

// Rotate this View at its center
canvas.rotate(angle, 0, this.getHeight());
ImmortalDev
  • 476
  • 3
  • 5
0

According to this question this functionality is not possible in Android natively. But as the last comment of the second answer suggests you can rotate your view by overriding the draw() method (not onDraw() as you did) and forwarding events to your button when the screen is touched at the specific area. So you have to take care of events yourself (because button doesn't work rotated) and then it could work. Please note that in the example provided in the other question and its answers TextView is extended, not Button.

Community
  • 1
  • 1
Robin Ellerkmann
  • 2,083
  • 4
  • 29
  • 34
0

u can use view.setRotation(rotationAngle) or checkout the RotateAnimation in android

eldjon
  • 2,800
  • 2
  • 20
  • 23