效果图
java代码RotateTextView
- import android.content.Context;
- import android.graphics.Canvas;
- import android.util.AttributeSet;
- import android.widget.TextView;
- public class RotateTextView extends TextView {
- private static final String NAMESPACE = "http://www.ywlx.net/apk/res/easymobi";
- private static final String ATTR_ROTATE = "rotate";
- private static final int DEFAULTVALUE_DEGREES = 0;
- private int degrees;
- public RotateTextView(Context context, AttributeSet attrs) {
- super(context, attrs);
- degrees = attrs.getAttributeIntValue(NAMESPACE, ATTR_ROTATE,
- DEFAULTVALUE_DEGREES);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- canvas.rotate(degrees, getMeasuredWidth() / 2, getMeasuredHeight() / 2);
- super.onDraw(canvas);
- }
- }
xml布局文件
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:easymobi="http://www.ywlx.net/apk/res/easymobi"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <com.example.test.RotateTextView
- android:id="@+id/tvBottom_color"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="#00FF00"
- android:layout_centerInParent="true"
- android:padding="15dip"
- android:textColor="#000"
- android:textSize="30dip"
- android:text="倾斜文字"
- easymobi:rotate = "10"/>
- </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在项目中的路径
很多博客用这种方法实现了文字旋转, 但是背景旋转呢
求高手赐教