На платформі Android є можливість повернути View на вказаний градус. У цій статті розглядається приклад, в якому реалізований вказаний поворот.
Створіть власний View під назвою RotatedTextView. Вам необхідно успадкувати його від TextView (або будь-якого іншого View) і перевизначити 3 конструктора:
package com.seostella.rotatesample;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;
public class RotatedTextView extends TextView {
public RotatedTextView(Context context) {
super(context);
}
public RotatedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public RotatedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
canvas.rotate(5, 0, 0);
super.onDraw(canvas);
canvas.restore();
}
}
Для повороту використовується перевизначенний метод onDraw(). Поворот відбувається за допомогою функції canvas.rotate(). В даному випадку View повернеться на 5 градусів.
Щоб додати RotatedTextView в xml-розмітку скористайтеся наступним кодом:
<com.seostella.rotatesample.RotatedTextView
android:layout_height="40dip"
android:layout_width="fill_parent"
android:text="Rotate Canvas Sample" />
В результаті Ви побачете на екрані емулятора (мобільного телефону) наступне вікно: