Android 文字倾斜

来源:互联网 发布:opengl 纹理变形算法 编辑:程序博客网 时间:2024/04/30 17:56

http://blog.csdn.net/le_go/article/details/39178563

[java] view plain copy
  1.   

有时候Android自带的控件无法满足我们的某些要求,这时就需要我们自定义控件来实现这些功能。比如需要一个TextView里的字倾斜一定的角度,就需要自定义TextView。

[html] view plain copy
  1. package com.leigo.ratatetextview;  
  2.   
  3. import android.content.Context;  
  4. import android.content.res.TypedArray;  
  5. import android.graphics.Canvas;  
  6. import android.util.AttributeSet;  
  7. import android.view.Gravity;  
  8. import android.widget.TextView;  
  9.   
  10. /**  
  11.  * Created by Administrator on 2014/9/9.  
  12.  */  
  13. public class RotateTextView extends TextView {  
  14.   
  15.     private static final int DEFAULT_DEGREES = 0;  
  16.   
  17.     private int mDegrees;  
  18.   
  19.     public RotateTextView(Context context) {  
  20.         super(context, null);  
  21.     }  
  22.   
  23.     public RotateTextView(Context context, AttributeSet attrs) {  
  24.         super(context, attrs, android.R.attr.textViewStyle);  
  25.   
  26.         this.setGravity(Gravity.CENTER);  
  27.   
  28.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RotateTextView);  
  29.   
  30.         mDegrees = a.getDimensionPixelSize(R.styleable.RotateTextView_degree, DEFAULT_DEGREES);  
  31.   
  32.         a.recycle();  
  33.     }  
  34.   
  35.     @Override  
  36.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  37.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  38.         setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());  
  39.     }  
  40.   
  41.     @Override  
  42.     protected void onDraw(Canvas canvas) {  
  43.         canvas.save();  
  44.         canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());  
  45.         canvas.rotate(mDegrees, this.getWidth() / 2f, this.getHeight() / 2f);  
  46.         super.onDraw(canvas);  
  47.         canvas.restore();  
  48.     }  
  49.   
  50.     public void setDegrees(int degrees) {  
  51.         mDegrees = degrees;  
  52.     }  
  53.   
  54. }  

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <declare-styleable name="RotateTextView">  
  4.         <attr name="degree" format="dimension" />  
  5.     </declare-styleable>  
  6. </resources>  

用法:

1.xml

[html] view plain copy
  1. <com.leigo.ratatetextview.RotateTextView  
  2.         xmlns:app="http://schemas.android.com/apk/res-auto"  
  3.         android:id="@+id/text"  
  4.         android:text="@string/hello_world"  
  5.         app:degree="10dp"  
  6.         android:padding="10dp"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content" /></span>  

2.java

[java] view plain copy
  1. RotateTextView  mText = (RotateTextView) findViewById (R.id.text);  
  2. mText.setDegrees(10);</span>  

项目地址:

https://coding.net/u/leigo/p/RotateTextView/git




0 0