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

package com.leigo.ratatetextview;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.util.AttributeSet;import android.view.Gravity;import android.widget.TextView;/** * Created by Administrator on 2014/9/9. */public class RotateTextView extends TextView {    private static final int DEFAULT_DEGREES = 0;    private int mDegrees;    public RotateTextView(Context context) {        super(context, null);    }    public RotateTextView(Context context, AttributeSet attrs) {        super(context, attrs, android.R.attr.textViewStyle);        this.setGravity(Gravity.CENTER);        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RotateTextView);        mDegrees = a.getDimensionPixelSize(R.styleable.RotateTextView_degree, DEFAULT_DEGREES);        a.recycle();    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth());    }    @Override    protected void onDraw(Canvas canvas) {        canvas.save();        canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop());        canvas.rotate(mDegrees, this.getWidth() / 2f, this.getHeight() / 2f);        super.onDraw(canvas);        canvas.restore();    }    public void setDegrees(int degrees) {        mDegrees = degrees;    }}

<?xml version="1.0" encoding="utf-8"?>                

用法:

1.xml


2.java

RotateTextView  mText = (RotateTextView) findViewById (R.id.text);mText.setDegrees(10);

项目地址:

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



Android 文字倾斜_第1张图片


更多相关文章

  1. Android控件隐藏方式 .
  2. Android 菜单(Menu)控件的使用
  3. android中ListView控件&&onItemClick事件中获取listView传递的数
  4. 怎么给Android(安卓)控件添加边框(样式)?
  5. android 自定义控件(邮箱边写边出格式邮箱)
  6. 自定义控件--虚线
  7. android之4.0控件switch自定义开关滑块、滑块轨道和宽度
  8. Android中WebView控件的使用
  9. Android Studio 新建编辑条 点击按钮显示控件中的内容

随机推荐

  1. Android(安卓)仿网易云鲸云音效动效
  2. Android关于Dex拆分 MultiDex 技术详解
  3. 音视频播放之从Android(安卓)framework到
  4. [Android(安卓)Studio] Gradle fails to
  5. Android 通用标题栏之组合控件
  6. android实现好看的自定义提示框
  7. android 带阻尼的周期运动
  8. android加载效果,带百分比的
  9. 【Android】adb 查看所有程序包名
  10. android 拨号