Android滚动栏控件的优化
背景 由于普通TextView的跑马灯效果与焦点获取有关 所以不能直接使用 之前查找到的控件在数据设置方面存在问题
所以通过寻找github上的开源控件 并修改源码 得到一个目前感觉不错的效果
原理 滚动效果其实就是文字在屏幕上的移动 根据找到的控件 发现有两种方案
一种是使用scrollTo方法 使得文字移动到一个指定的位置 但是使用过程中发现 超过屏幕长度的文字会在最后显示省略号 在这个问题没解决前 不采用此方案
另外一种是使用drawText方法 不断绘制文字 最后修改源码得到的效果感觉不错 代码也比较精简 唯一的不方便是文字颜色大小需要在代码中设置 在属性中设置无效 不过感觉影响不太 看以后是否有更好的优化
目前采用这种方案
具体分析和相关的坑
1 drawText绘制文字居住的问题
canvas.drawText(text, x, y, paint),第一个参数是我们需要绘制的文本,第四个参数是我们的画笔,这两个不用多说
主要是第二和第三个参数的含义,这两个参数在不同的情况下的值还是不一样的,x默认是这个字符串的左边在屏幕的位置,
如果设置了paint.setTextAlign(Paint.Align.CENTER);那就是字符的中心,
y是指定这个字符baseline在屏幕上的位置,y不是这个字符中心在屏幕上的位置,而是baseline在屏幕上的位置
所以 要想让文字居中显示 正确的代码是这样的
paint.setTextAlign(Paint.Align.LEFT);
Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
int heigit = getMeasuredHeight();
y = (heigit - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top; //173
2 水平滚动x坐标的计算
当文字居中显示后 y坐标也就固定了 由于文字是在水平方向上滚动 所以主要是y坐标的计算
x值的变动 使得文字从头滚动到尾
最开始 文字起点在屏幕最右边不可见 所以初试值为屏幕宽度 比如1080
滚动后 x值慢慢变少 当变为0 在文字长度大于屏幕宽度的情况下 比如文字长度为4304 一个屏幕宽度的文字移出到了屏幕右侧
最后 在x值为负值的情况下 文字完全移出在屏幕左侧 具体的值为 -4304 其实就是文字长度的负值
于是循环开始 文字又在初试位置 即屏幕右侧 具体值为1080
3 其它
主要是相关属性的设置 另外 该控件还可以扩展 比如点击事件 暂停停止等
public class MainActivity extends Activity {
private AutoScrollTextView auto_tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auto_tv = (AutoScrollTextView) findViewById(R.id.auto_textview);
auto_tv.setText("特定的时间内"); //设置文字内容
auto_tv.setScrollTextColor(Color.parseColor("#AB82FF")); //设置文字颜色
auto_tv.setScrollTextSize(12); //设置字体大小 以sp为单位
auto_tv.setScrollSpeed(4); //设置文字滚动速度
auto_tv.init();
auto_tv.startScroll();
}
}
public class AutoScrollTextView extends TextView {
public boolean isStarting = false; //是否开始滚动
private float textLength = 0f; //文本长度
private float screenWidth = 0f; //屏幕宽度
private float x = 0f; //文字横坐标
private float y = 0f; //文字纵坐标
private String text = ""; //文本内容
private Paint paint = null; //绘图样式
private Context context;
private int color; //字体颜色
private float textSize; //字体大小 像素为单位
private float speed = 5; //文字滚动速度 实际是一个偏移像素值 越大速度越快
public AutoScrollTextView(Context context) {
super(context);
this.context = context;
}
public AutoScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
}
public void init() {
paint = getPaint();
paint.setColor(color);
paint.setTextSize(textSize);
screenWidth = getScreenWidth(context); //1080 屏幕宽度
x = screenWidth;
}
public void startScroll() {
isStarting = true;
invalidate();
}
public void stopScroll() {
isStarting = false;
invalidate();
}
public void setScrollTextColor(int color) {
this.color = color;
}
public void setScrollTextSize(int spValue) {
this.textSize = sp2px(context, spValue);
}
public void setScrollSpeed(float speed) {
this.speed = speed;
}
@Override
public void onDraw(Canvas canvas) {
text = getText().toString();
textLength = paint.measureText(text); //4310 文字长度 相当于四个屏幕宽度
//在这里计算y坐标
paint.setTextAlign(Paint.Align.LEFT);
Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
int heigit = getMeasuredHeight();
y = (heigit - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top; //
Log.i("TAG", "x-----------------x=" + x);
//Log.i("TAG", "y-----------------" + y);
canvas.drawText(text, x, y, paint);
if (!isStarting) {
return;
}
if (x < -textLength){
x = screenWidth;
}else {
x = x - speed;
}
invalidate();
}
private float sp2px(Context context, int spValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (spValue * fontScale + 0.5f);
}
private int getScreenWidth(Context context) {
return context.getResources().getDisplayMetrics().widthPixels;
}
}
Android滚动栏控件的优化的更多相关文章
- Android滚动选择控件
现在觉得github特别方便,我一般直接使用github中的内容, https://github.com/wangjiegulu/WheelView 这里面都有详细的介绍
- Android 性能优化——之控件的优化
Android 性能优化——之控件的优化 前面讲了图像的优化,接下来分享一下控件的性能优化,这里主要是面向自定义View的优化. 1.首先先说一下我们在自定义View中可能会犯的3个错误: 1)Use ...
- Android RecyclerView滚动类控件修改、去掉滑动边界的阴影效果
前言 滚动类控件,大家都用的很多,如 RecyclerView.NestedSrollView.... 下面以recyclerView为例讲解,其他滚动控件也同理. RecyclerView 滚动列表 ...
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
- 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用
Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...
- 【转】Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用
Android M新控件之FloatingActionButton,TextInputLayout,Snackbar,TabLayout的使用 分类: Android UI2015-06-15 16: ...
- 【风马一族_Android】第4章Android常用基本控件
第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...
- [置顶] Android常用适配器控件
Android常用适配器控件 列表控件用于显示数据集合,Android不是使用一种类型的控件管理显示和数据,而是将这两项功能分布用列表控件和适配器来实现.列表控件扩展了android.widget.A ...
随机推荐
- Python版本切换和Pip安装
Python版本切换 现在常用的linux系统中都会默认携带python运行环境,在ubuntu 16.04 和centos 7.3中携带有Python 2.7 和Python3.5两个版本, 默认使 ...
- 第三周的psp
PSP: 进度条: 累计进度图: 本周PSP饼状图:
- LintCode-379.将数组重新排序以构造最小值
将数组重新排序以构造最小值 给定一个整数数组,请将其重新排序,以构造最小值. 注意事项 The result may be very large, so you need to return a st ...
- iOS- 无处不在,详解iOS集成第三方登录(SSO授权登录<无需密码>)
1.前言 不多说,第三登录无处不在!必备技能,今天以新浪微博为例. 这是上次写的iOS第三方社交分享:http://www.cnblogs.com/qingche/p/3727559.html 可 ...
- 【week2】 构建之法 读后感及问题
上一次读后感涵盖前五章的内容包括个人技术,结对合作,小组项目等.本周作业的燃尽图以及站立会议是关于<构建之法>第六章的内容,所以关于这一章的读后感涵盖在上两篇博客中. 第七章 MSF 介绍 ...
- RPC架构-美团,京东面试题目
RPC(Remote Procedure Call) RPC服务 从三个角度来介绍RPC服务:分别是RPC架构,同步异步调用以及流行的RPC框架. RPC架构 先说说RPC服务的基本架构吧.允许我可耻 ...
- EasyUI 学习笔记
EasyUI常见错误 1 . 无论是用HMTL形式实现组件还是使用代码 + HTML 形式实现组件 , 在为组件设置属性时 , 要注意属性值的类型问题 string:必须加引号 number:不加任何 ...
- linux 装redmine
看第一篇 https://www.cnblogs.com/iluzhiyong/p/redmine.html 看第二篇 http://blog.51yip.com/cloud/1874.html 基本 ...
- MVC 枚举 转 SelectListItem
ViewBag.userlevel = new SelectList(Enum.GetNames(typeof(AdminLevels)),"", "", te ...
- 使用canvas控制gif图片的播放与暂停
if ('getContext' in document.createElement('canvas')) { HTMLImageElement.prototype.play = function() ...