小视频源码,可控的跑马灯,无需焦点

小视频源码,可控的跑马灯,无需焦点实现的相关代码

package com.android.view;

import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.text.Layout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.widget.TextView;

public class MarqueeTextView extends TextView {
    /** 是否停止滚动 */
    private boolean mStopMarquee;
    private String mText;//文本内容
    private float mCoordinateX = 1280;//当前滚动位置
    private float mTextWidth;//文本宽度
    private int mScrollWidth = 1280;//滚动区域宽度
    private int speed = 1;//滚动速度
    private float currentPosition = 0.0f;
    public float getCurrentPosition() {
        return mCoordinateX;
    }

    public boolean ismStopMarquee() {
        return mStopMarquee;
    }

    public void setmStopMarquee(boolean mStopMarquee) {
        this.mStopMarquee = mStopMarquee;
    }

    public void setCurrentPosition(float mCoordinateX) {
        this.mCoordinateX = mCoordinateX;
    }

    public int getScrollWidth() {
        return mScrollWidth;
    }

    public void setScrollWidth(int mScrollWidth) {
        this.mScrollWidth = mScrollWidth;
        currentPosition = mScrollWidth;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void setText(String text) {
        this.mText = text;
        mTextWidth = getPaint().measureText(mText);
        //mTextWidth = 1280;
        if (mHandler.hasMessages(0))
            mHandler.removeMessages(0);
        mHandler.sendEmptyMessageDelayed(0, 10);
    }

    @Override
    protected void onAttachedToWindow() {
        //mStopMarquee = false;
        if (!isEmpty(mText))
            mHandler.sendEmptyMessageDelayed(0, 500);
        super.onAttachedToWindow();
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    @Override
    protected void onDetachedFromWindow() {
        //mStopMarquee = true;
        if (mHandler.hasMessages(0))
            mHandler.removeMessages(0);
        super.onDetachedFromWindow();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        TextPaint paint = getPaint();
        int width = (int) Layout.getDesiredWidth(mText, 0,
                mText.length(), paint);
        if(width < getWidth()){
            canvas.drawText(mText, 10, 40, getPaint());
            return;
        }
        if (!isEmpty(mText)) {
            //System.err.println("zzx===>mCoordinateX: "+mCoordinateX);
            canvas.drawText(mText, mCoordinateX+10, 40, getPaint());
            currentPosition = width+mCoordinateX+200;
            canvas.drawText(mText, currentPosition, 40, getPaint());
        }
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    if (mCoordinateX < (-mTextWidth)) {//文字滚动完了,从滚动区域的右边出来
                        mCoordinateX = currentPosition-10;
                        if (!mStopMarquee) {
                            sendEmptyMessageDelayed(0, 500);
                        }else{
                            mCoordinateX = 0;
                        }
                        invalidate();
                    } else {
                        mCoordinateX -= speed;
                        if (!mStopMarquee) {
                            sendEmptyMessageDelayed(0, 30);
                        }else{
                            mCoordinateX = 0;
                        }
                        invalidate();
                    }

                    break;
            }
            super.handleMessage(msg);
        }
    };

}

以上就是 小视频源码,可控的跑马灯,无需焦点实现的相关代码,更多内容欢迎关注之后的文章

上一篇:《Android多媒体应用开发实战详解:图像、音频、视频、2D和3D》——2.3节简析Android安装文件


下一篇:JavaScript == 和 ===