关于testView的背景旋转

来源:互联网 发布:淘宝网店怎么找货源 编辑:程序博客网 时间:2024/06/05 00:46
 

Android之TextView实现倾斜文字

分类: android 138人阅读 评论(2) 收藏 举报
Androidtextview布局Canvas

效果图


java代码RotateTextView

[java] view plaincopy
  1. import android.content.Context;  
  2. import android.graphics.Canvas;  
  3. import android.util.AttributeSet;  
  4. import android.widget.TextView;  
  5.   
  6. public class RotateTextView extends TextView {  
  7. private static final String NAMESPACE = "http://www.ywlx.net/apk/res/easymobi";  
  8. private static final String ATTR_ROTATE = "rotate";  
  9. private static final int DEFAULTVALUE_DEGREES = 0;  
  10. private int degrees;  
  11.   
  12. public RotateTextView(Context context, AttributeSet attrs) {  
  13. super(context, attrs);  
  14. degrees = attrs.getAttributeIntValue(NAMESPACE, ATTR_ROTATE,  
  15. DEFAULTVALUE_DEGREES);  
  16.   
  17. }  
  18.   
  19. @Override  
  20. protected void onDraw(Canvas canvas) {  
  21.   
  22. canvas.rotate(degrees, getMeasuredWidth() / 2, getMeasuredHeight() / 2);  
  23. super.onDraw(canvas);  
  24. }  
  25.   
  26. }  

xml布局文件

[html] view plaincopy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:easymobi="http://www.ywlx.net/apk/res/easymobi"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <com.example.test.RotateTextView  
  7.         android:id="@+id/tvBottom_color"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:background="#00FF00"  
  11.         android:layout_centerInParent="true"  
  12.         android:padding="15dip"  
  13.         android:textColor="#000"  
  14.         android:textSize="30dip"  
  15.         android:text="倾斜文字"  
  16.         easymobi:rotate = "10"/>  
  17.   
  18. </RelativeLayout>  



注 : easymobi:rotate=”10″指定了旋转10度,但是要注意在头文件加上 xmlns:easymobi=”http://www.ywlx.net/apk/res/easymobi”,这个就是RotateTextView 中的命名空间的作用,其他属性与普通的TextView相同。还有一点要注意的就是加上合适的padding,因为这种方法旋转的是TextView里面的字,而不是TextView本身,如果 不加padding,有些字就会因为旋转而跑到了TextView外面而不能显示。

com.example.test.RotateTextView为RotateTextView在项目中的路径



很多博客用这种方法实现了文字旋转, 但是背景旋转呢

求高手赐教

0 0