傾斜的TextView 自定義TextView

想作一個傾斜的TextView,想海報上顯示的那樣 ,在網上找例子一直不能實現,看了看TextView源碼,發現很簡單,爲方便像我同樣糊塗的孩紙,貼出來了

首先須要先自定義一個TextView java

public class MyTextView extends TextView{

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //傾斜度45,上下左右居中
        canvas.rotate(-45, getMeasuredWidth()/2, getMeasuredHeight()/2);
        super.onDraw(canvas);
    }
    
}
xml文件中添加上這個MyTextView 
<com.peace.test.incline_textview.MyTextView
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:gravity="center"
        android:text="@string/hello_world"/>
相關文章
相關標籤/搜索