自定义TextView解决事件冲突TextView不能获取焦点问题


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:[email protected]
 */

/**
 * 自定义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();
    }
}



你可能感兴趣的:(textview)