自定义TextView解决事件冲突TextView不能获取焦点问题
2015-11-13 13:03
573 查看
import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; import com.bluezhang.zhufengfm.BuildConfig; /** * Author: blueZhang * DATE:2015/10/25 * Time: 17:35 * email:bluezhang521@163.com */ /** * 自定义TextView的跑马灯的效果 * 用于解决如果出现事件冲突TextView不能够获取焦点 * 如果可以获取焦点那么可以使用如下代码 * <pre> android:layout_width="100dip" android:layout_height="wrap_content" android:layout_gravity="center" android:text="走马灯效果的演示" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" <com.bluezhang.zhufengfm.widgets.NewTextView android:id="@+id/playtrackui_textview_titile_id" android:layout_width="160dp" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="30dp" android:layout_toRightOf="@id/playtrackui_imageview_back_id" android:ellipsize="marquee" android:singleLine="true" android:text="Title" android:textColor="@color/dimgrey" android:textSize="17sp" />
<pre/> * */
public class NewTextView extends TextView implements Runnable { private int currentScrollX;// 当前滚动的位置 private boolean isStop = false; private int textWidth; private boolean isMeasure = false; public NewTextView(Context context) { super(context); } public NewTextView(Context context, AttributeSet attrs) { super(context, attrs); } public NewTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); if (!isMeasure) {// 文字宽度只需获取一次就可以了 getTextWidth(); isMeasure = true; } } /** * 获取文字宽度 */ private void getTextWidth() { Paint paint = this.getPaint(); String str = this.getText().toString(); textWidth = (int) paint.measureText(str); } @Override public void run() { currentScrollX += 1;// 滚动速度 scrollTo(currentScrollX, 0); if (isStop) { return; } if (getScrollX() >= (this.getWidth() + textWidth)) { scrollTo(-(textWidth + 160), 0); currentScrollX = -textWidth - 160; if (BuildConfig.DEBUG) Log.d("NewTextView", "条件成立"); } postDelayed(this, 5); } // 开始滚动 public void startScroll() { isStop = false; this.removeCallbacks(this); post(this); } // 停止滚动 public void stopScroll() { isStop = true; } // 从头开始滚动 public void startFor0() { currentScrollX = -textWidth - 160; startScroll(); } }
相关文章推荐
- java压缩
- android 自定义shape
- request.getParameter(“参数名”) 中文乱码解决方法
- 偶然想法
- 在Unity中添加代码,会自动调用代码中的Reset函数,当修改代码中的变量的时候,会调用OnValidate函数
- 我的第一个csdn博客
- retain
- cocos2d-x textField多行输入
- Viewpager 中子view 持有焦点诱发的卡顿问题
- c基础知识
- 利用java类的思想和并查集的方法解决同一道算法题
- 机器学习(分类器)
- Sum---poj1844(数学题)
- 小马哥----高仿苹果6S A236 刷机拆机主板图与开机界面图 更新解锁界面 全网通4G 警惕
- java JDK 环境变量配置
- windows下生成固定大小文件
- Java中的向前引用
- 二维码的生成和扫描识别
- POSIX的pthread_join http://blog.csdn.net/ba_jie/article/details/6783205
- (转)java读取properties