TextView文字旋转

来源:互联网 发布:云计算工程师做什么 编辑:程序博客网 时间:2024/06/08 18:58

Android TextView文字旋转45°效果图如图:

图片资源:    


RotateTextView:

[java] view plain copy
  1. import android.content.Context;  
  2. import android.graphics.Canvas;  
  3. import android.util.AttributeSet;  
  4. import android.widget.TextView;  
  5.   
  6. /** 
  7.  *  
  8.  * @author Tyler 
  9.  * @time 2015-11-25 下午1:45:07 
  10.  */  
  11. public class RotateTextView extends TextView{  
  12.   
  13.       
  14.     public RotateTextView(Context context) {  
  15.         super(context);  
  16.     }  
  17.       
  18.     public RotateTextView(Context context, AttributeSet attrs) {  
  19.         super(context, attrs);  
  20.     }  
  21.   
  22.     @Override  
  23.     protected void onDraw(Canvas canvas) {  
  24.         //倾斜度45,上下左右居中  
  25.         canvas.rotate(45, getMeasuredWidth()/2, getMeasuredHeight()/2);  
  26.         super.onDraw(canvas);  
  27.     }  
  28.       
  29. }  


XML:

[html] view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="match_parent"  
  4.     android:orientation="vertical"  
  5.     android:background="@color/white" >  
  6.   
  7.     <包名.RotateTextView  
  8.         android:layout_width="54dip"  
  9.         android:layout_height="54dip"  
  10.         android:layout_alignParentRight="true"  
  11.         android:background="@drawable/e_rotate_bg2"  
  12.         android:gravity="center"  
  13.         android:paddingBottom="17dp"  
  14.         android:text="进行中"  
  15.         android:textColor="#fff"  
  16.         android:textSize="12sp" />  
  17.   
  18. </RelativeLayout>  
1 0
原创粉丝点击