在android中怎么定义垂直,Android中的垂直(旋转)标签
我为我的ChartDroid项目。创造VerticalLabelView.java:public class VerticalLabelView extends View {
private TextPaint mTextPaint;
private String mText;
private int mAscent;
private Rect text_bounds = new Rect();
final static int DEFAULT_TEXT_SIZE = 15;
public VerticalLabelView(Context context) {
super(context);
initLabelView();
}
public VerticalLabelView(Context context, AttributeSet attrs) {
super(context, attrs);
initLabelView();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.VerticalLabelView);
CharSequence s = a.getString(R.styleable.VerticalLabelView_text);
if (s != null) setText(s.toString());
setTextColor(a.getColor(R.styleable.VerticalLabelView_textColor, 0xFF000000));
int textSize = a.getDimensionPixelOffset(R.styleable.VerticalLabelView_textSize, 0);
if (textSize > 0) setTextSize(textSize);
a.recycle();
}
private final void initLabelView() {
mTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(DEFAULT_TEXT_SIZE);
mTextPaint.setColor(0xFF000000);
mTextPaint.setTextAlign(Align.CENTER);
setPadding(3, 3, 3, 3);
}
public void setText(String text) {
mText = text;
requestLayout();
invalidate();
}
public void setTextSize(int size) {
mTextPaint.setTextSize(size);
requestLayout();
invalidate();
}
public void setTextColor(int color) {
mTextPaint.setColor(color);
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mTextPaint.getTextBounds(mText, 0, mText.length(), text_bounds);
setMeasuredDimension(
measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}
private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text
result = text_bounds.height() + getPaddingLeft() + getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);
}
}
return result;
}
private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
mAscent = (int) mTextPaint.ascent();
if (specMode == MeasureSpec.EXACTLY) {
// We were told how big to be
result = specSize;
} else {
// Measure the text
result = text_bounds.width() + getPaddingTop() + getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
// Respect AT_MOST value if that was what is called for by measureSpec
result = Math.min(result, specSize);
}
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float text_horizontally_centered_origin_x = getPaddingLeft() + text_bounds.width()/2f;
float text_horizontally_centered_origin_y = getPaddingTop() - mAscent;
canvas.translate(text_horizontally_centered_origin_y, text_horizontally_centered_origin_x);
canvas.rotate(-90);
canvas.drawText(mText, 0, 0, mTextPaint);
}}
和在attrs.xml:
在android中怎么定义垂直,Android中的垂直(旋转)标签相关推荐
- Android弹出自定义Dialog,android自定义Dialog实现底部弹窗
android自定义Dialog实现底部弹窗 拿到这个需求,很多人都是直接想用popWindow 实现,但是这样的效果我们完全可以根据系统的Dialog 自定义一个. AlertDialog.Buil ...
- java中如何定义接口_java中如何定义一个接口
使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,其中接口体由常量定义和方法定义两部分组成.定义接口的基本格式如下:[修饰符] interface 接口名 [e ...
- java中如何定义一个_java中如何定义一个方法
java中如何定义一个方法 发布时间:2020-06-20 16:39:15 来源:亿速云 阅读:88 作者:Leah java中如何定义一个方法?相信很多新手小白还没学会这个技能,通过这篇文章的总结 ...
- Android接口一般定义格式,Android开发规范
原标题:Android开发规范 一.书写规范 1. 编码方式统一用UTF-8. 2. 花括号不要单独一行,和它前面的代码同一行.而且,花括号与前面的代码之间用一个空格隔开. 3. 空格的使用 if.e ...
- android数据线接口定义,制作Android手机可连接普通键盘的数据转接线
在上一篇" 现在市面上的Android手机和Android平板电脑基本上都支持USB OTG技术.这种技术将允许用户将手机或平板电脑通过usb2.0数据线连接到标准USB键盘和鼠标,甚至普通 ...
- python中如何定义数组_python中数组是如何使用的?
今天马哥教育要跟大家分享的文章是Python数组是如何使用的?熟悉Python的小伙伴们都知道在学习Python过程中数组是个逃不过去的一个关,既然逃不过去咱就勇敢面对它,Python入门新手和正在 ...
- python中plt定义,对Python中plt的画图函数详解
1.plt.legend plt.legend(loc=0)#显示图例的位置,自适应方式 说明: 'best' : 0, (only implemented for axes legends)(自适应 ...
- matlab 报错 保留变量,matlab中明明定义了函数变量总是报错
matlab定义符号函数 functiony=ff(t)t=input('t=');ift>=0y=sym('1');elsey=sym('0');end matlab中如何定义函数 matla ...
- java mysql tinyint_Msq 中tinyint字段对应 java中哪个类型?
展开全部 MySql 中的tinyint(1)的使用 在MySql中如何定义像Java中类型的Boolean类型数据..其实,mysql中 是没有直接定义成Boolean这种数据类型. 它只能定义成 ...
- android中各属性布局,Android里面各布局的属性定义
LinearLayout(线性布局) 提供了控件水平垂直排列的模型,同时可以通过设置子控件的weight布局参数控制各个控件在布局中的相对大小. 水平(vertical) 垂直(horizontal) ...
最新文章
- Android 使用java 代码获取res 里面的value 定义的数组
- web端ios布局fixed元素软键盘唤起时fixed失效
- 深度学习(十二)稀疏自编码
- python怎么查看列表_Python 小白,关于对于列表的+=操作不明白,查了半天也不知道怎么查,并且查询无果,请人指点?...
- 远程监督关系抽取漫谈:结合知识图谱和图神经网络
- OpenCASCADE绘制测试线束:几何命令之转换
- Python删除Delphi临时文件的小脚本
- poj Going from u to v or from v to u? 强联通缩点+拓扑排序(或搜索)
- 仿iphone触屏手机界面
- hdu5751 Eades
- 蓝点中文_linux2.0 实验二 简单shell命令
- Spring 中注入什么意思?
- JSP中Session的使用
- html5直播api,HTML5 VideoAPI,打造自己的Web视频播放器
- C++ 判断进程是否存在
- 新城建产品 DTCIM
- ERP进销存系统源码
- Win10 添加传统照片查看器
- Win10将绿色软件添加到开始菜单
- mysql怎样添加唯一标识_MySql设置唯一标识,主键等设置
热门文章
- android ndk配置环境,在Windows中的安装Android NDK开发环境
- 麦克纳姆轮全向移动机器人斜向直线运动分析
- java super实例_Java 的 super 学习的一个实例
- oracle 数据库基础配置,Oracle数据库网络服务配置基础、SQL编程详解-Oracle
- e.target+addEventListener事件委托
- 浮动元素具有行内块元素特点(HTML、CSS)
- vue项目中使用sass的方法
- Html5 h5页面输入框失去焦点页面底部白板问题
- Eigen教程(6)之高级初始化方法
- 在Ubuntu16.04下安装cmake 和cmake-gui