文字跑马灯效果

自定义AutoScrollTextView.java :

package com.example.datamodule.terminal;

/**
 * Created by scq on 16-8-12.
 */
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.Display;
import android.view.WindowManager;
import android.widget.TextView;

public class AutoScrollTextView extends TextView {

    private float textLength = 0f;//文本长度
    private float viewWidth = 0f;
    private float step = 0f;//文字的横坐标
    private float y = 0f;//文字的纵坐标
    private float temp_view_plus_text_length = 0.0f;//用于计算的临时变量
    private float temp_view_plus_two_text_length = 0.0f;//用于计算的临时变量
    public boolean isStarting = false;//是否开始滚动
    private Paint paint = null;//绘图样式
    private String text = "";//文本内容
    private int mEffectNumber = 0;

    private boolean isChange = false;
    private boolean isOneRun = false;
    private boolean isTwoRun = false;
    private float stepTow = 0f;

    private SharedPreferenceTerminal mSharedPreferenceString;
    private String mTextContent;
    private int mTextSize;
    private float mSpeed;
    private int mEffect;
    private int mTextColor;
    private int mAlpha;
    private WindowManager mWindowManager;
    private Context mContext;

    public AutoScrollTextView(Context context) {
        this(context, null, 0);
    }

    public AutoScrollTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        mContext = context.getApplicationContext();
        mWindowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        mSharedPreferenceString = new SharedPreferenceTerminal(context);

        mTextContent = mSharedPreferenceString.getTerminalScrollingContent();
        mTextSize = mSharedPreferenceString.getTerminalScrollingFontSizeContent();
        mSpeed = mSharedPreferenceString.getTerminalScrollingSpeedContent();
        mEffect = mSharedPreferenceString.getTerminalScrollingEffectContent();
        mTextColor = mSharedPreferenceString.getTerminalScrollingTextColorContent();
        mAlpha = mSharedPreferenceString.getTerminalScrollingTransparency();

        init(mWindowManager);
        if(mEffect == 0){
            stopScroll();
        }else if(mEffect == 9){
            startScroll();
        }else {
            startScroll();
        }
    }

    public void init(WindowManager windowManager) {
        paint = getPaint();
        text = mTextContent;
        setTextSize(mTextSize);
        if(mEffectNumber != 0){
            mEffectNumber = 0;
        }
        paint.setColor(mTextColor);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        getBackground().setAlpha(mAlpha);
        textLength = paint.measureText(text);
        viewWidth = getWidth();
        if(viewWidth == 0) {
            if(windowManager != null) {
                Display display = windowManager.getDefaultDisplay();
                viewWidth = display.getWidth();
            }
        }
        step = textLength;
        stepTow = textLength;
        temp_view_plus_text_length = viewWidth + textLength;
        temp_view_plus_two_text_length = viewWidth + textLength * 2;
        y = getTextSize() + getPaddingTop();
    }

    @Override
    public Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);
        ss.step = step;
        ss.isStarting = isStarting;
        return ss;
    }

    @Override
    public void onRestoreInstanceState(Parcelable state) {
        if (!(state instanceof SavedState)) {
            super.onRestoreInstanceState(state);
            return;
        }
        SavedState ss = (SavedState)state;
        super.onRestoreInstanceState(ss.getSuperState());
        step = ss.step;
        isStarting = ss.isStarting;
    }

    public static class SavedState extends BaseSavedState {
        public boolean isStarting = false;
        public float step = 0.0f;
        SavedState(Parcelable superState) {
            super(superState);
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeBooleanArray(new boolean[]{isStarting});
            out.writeFloat(step);
        }

        public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }

            @Override
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }
        };

        private SavedState(Parcel in) {
            super(in);
            boolean[] b = null;
            in.readBooleanArray(b);
            if(b != null && b.length > 0)
                isStarting = b[0];
            step = in.readFloat();
        }
    }

    public void startScroll() {
        isStarting = true;
        if(mEffectNumber != 0){
            mEffectNumber = 0;
        }
        invalidate();
    }

    public void stopScroll() {
        isStarting = false;
        if(mEffectNumber != 0){
            mEffectNumber = 0;
        }
        invalidate();
    }

    @Override
    public void onDraw(Canvas canvas) {
        if(!isStarting) {
            canvas.drawText(text, viewWidth/2 - textLength/2, y, paint);
        } else {
            canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
            canvas.drawText(text, temp_view_plus_text_length - stepTow, y, paint);
            if(!isChange){
                if(isTwoRun){
                    stepTow += mSpeed;
                    if(stepTow > temp_view_plus_two_text_length){
                        stepTow = textLength;
                        isTwoRun = false;
                        isStopScroll();
                    }
                }
                step += mSpeed;
                if((textLength - getLeft() + (temp_view_plus_text_length - step)) <= viewWidth/3){
                    isChange = true;
                    stepTow = textLength;
                    isOneRun = true;
                }
            } else {
                if(isOneRun){
                    step += mSpeed;
                    if(step > temp_view_plus_two_text_length){
                        step = textLength;
                        isOneRun = false;
                        isStopScroll();
                    }
                }
                stepTow += mSpeed;
                if((textLength - getLeft() + (temp_view_plus_text_length - stepTow)) <= viewWidth/3){
                    isChange = false;
                    step = textLength;
                    isTwoRun = true;
                }
            }
        }
        invalidate();
    }
    public void isStopScroll(){
        if(mEffect != 9 && mEffect != 0){
            mEffectNumber ++;
            if(mEffectNumber >= mEffect){
                stopScroll();
            }
        }
    }
}

保存数据 SharedPreferenceTerminal.java :
package com.example.datamodule.terminal;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by scq on 16-8-9.
 */
public class SharedPreferenceTerminal {

    private Context mContext;
    private SharedPreferences mySharedPreferences;
    private SharedPreferences.Editor mEditor;
    private String mTerminalNumber = "TerminalName";

    private String mScrollingContent = "ScrollingContent";
    private String mScrollingFontSize = "ScrollingFontSize";
    private String mScrollingFontSizeNumber = "ScrollingFontSizeNumber";
    private String mScrollingFontSizeContent = "ScrollingFontSizeContent";
    private String mScrollingSpeed = "ScrollingSpeed";
    private String mScrollingSpeedNumber = "ScrollingSpeedNumber";
    private String mScrollingSpeedContent = "ScrollingSpeedContent";
    private String mScrollingEffect = "ScrollingEffect";
    private String mScrollingEffectNumber = "ScrollingEffectNumber";
    private String mScrollingEffectContent = "ScrollingEffectContent";
    private String mScrollingTextColor = "ScrollingTextColor";
    private String mScrollingTextColorNumber = "ScrollingTextColorNumber";
    private String mScrollingTextColorContent = "ScrollingTextColorContent";
    private String mScrollingTransparency = "ScrollingTransparency";
    private String mScrollingTransparencyContent = "ScrollingTransparencyContent";
    /**创建SharedPreference**/
    public SharedPreferenceTerminal(Context context){
        mContext = context;
        mySharedPreferences = context.getSharedPreferences(mTerminalNumber, Activity.MODE_PRIVATE);
    }

    /**保存【滚动内容】**/
    public void setEditorScrollingContent(String mName){//滚动内容
        mEditor = mySharedPreferences.edit();
        mEditor.putString(mScrollingContent, mName);
        mEditor.commit();
    }
    /**读取【滚动内容】:name内容**/
    public String getTerminalScrollingContent(){
        return mySharedPreferences.getString(mScrollingContent, mContext.getResources().getString(R.string.action_text_content));
    }

    /**保存【文字大小】**/
    public void setEditorScrollingFontSize(String mName, int mNumber, int mSize){//文字大小
        mEditor = mySharedPreferences.edit();
        mEditor.putString(mScrollingFontSize, mName);
        mEditor.putInt(mScrollingFontSizeNumber, mNumber);
        mEditor.putInt(mScrollingFontSizeContent, mSize);
        mEditor.commit();
    }
    /**读取【文字大小】:name内容**/
    public String getTerminalScrollingFontSize(){
        return mySharedPreferences.getString(mScrollingFontSize, mContext.getResources().getString(R.string.scrolling_fontsize_small));
    }
    /**读取【文字大小】:列表默认位置**/
    public int getTerminalScrollingFontSizeNumber(){
        return mySharedPreferences.getInt(mScrollingFontSizeNumber, 0);
    }
    /**读取【文字大小】:设置的字体大小**/
    public int getTerminalScrollingFontSizeContent(){
        return mySharedPreferences.getInt(mScrollingFontSizeContent, 16);
    }

    /**保存【滚动速度】**/
    public void setEditorScrollingSpeed(String mName, int mNumber, float mSpeed){//滚动速度
        mEditor = mySharedPreferences.edit();
        mEditor.putString(mScrollingSpeed, mName);
        mEditor.putInt(mScrollingSpeedNumber, mNumber);
        mEditor.putFloat(mScrollingSpeedContent, mSpeed);
        mEditor.commit();
    }
    /**读取【滚动速度】:name内容**/
    public String getTerminalScrollingSpeed(){
        return mySharedPreferences.getString(mScrollingSpeed, mContext.getResources().getString(R.string.scrolling_speed_normal));
    }
    /**读取【滚动速度】:列表默认位置**/
    public int getTerminalScrollingSpeedNumber(){
        return mySharedPreferences.getInt(mScrollingSpeedNumber, 1);
    }
    /**读取【滚动速度】:设置的字体滚动速度**/
    public float getTerminalScrollingSpeedContent(){
        return mySharedPreferences.getFloat(mScrollingSpeedContent, 2f);
    }

    /**保存【滚动效果】**/
    public void setEditorScrollingEffect(String mName, int mNumber, int mEffect){//滚动效果
        mEditor = mySharedPreferences.edit();
        mEditor.putString(mScrollingEffect, mName);
        mEditor.putInt(mScrollingEffectNumber, mNumber);
        mEditor.putInt(mScrollingEffectContent, mEffect);
        mEditor.commit();
    }
    /**读取【滚动效果】:name内容**/
    public String getTerminalScrollingEffect(){
        return mySharedPreferences.getString(mScrollingEffect, mContext.getResources().getString(R.string.scrolling_effect_continued));
    }
    /**读取【滚动效果】:列表默认位置**/
    public int getTerminalScrollingEffectNumber(){
        return mySharedPreferences.getInt(mScrollingEffectNumber, 3);
    }
    /**读取【滚动效果】:设置的滚动效果**/
    public int getTerminalScrollingEffectContent(){
        return mySharedPreferences.getInt(mScrollingEffectContent, 9);
    }

    /**保存【文字颜色】**/
    public void setScrollingTextColor(String mName, int mNumber, int mTextColor){//文字颜色
        mEditor = mySharedPreferences.edit();
        mEditor.putString(mScrollingTextColor, mName);
        mEditor.putInt(mScrollingTextColorNumber, mNumber);
        mEditor.putInt(mScrollingTextColorContent, mTextColor);
        mEditor.commit();
    }
    /**读取【文字颜色】:name内容**/
    public String getTerminalScrollingTextColor(){
        return mySharedPreferences.getString(mScrollingTextColor, mContext.getResources().getString(R.string.scrolling_textcolor_black));
    }
    /**读取【文字颜色】:列表默认位置**/
    public int getTerminalScrollingTextColorNumber(){
        return mySharedPreferences.getInt(mScrollingTextColorNumber, 4);
    }
    /**读取【文字颜色】:设置的字体颜色**/
    public int getTerminalScrollingTextColorContent(){
        return mySharedPreferences.getInt(mScrollingTextColorContent, 0xff000000);
    }

    /**保存【滚动背景透明度】**/
    public void setScrollingTransparency(int mNumber, int mAlpha){//滚动背景透明度
        mEditor = mySharedPreferences.edit();
        mEditor.putInt(mScrollingTransparency, mNumber);
        mEditor.putInt(mScrollingTransparencyContent, mAlpha);
        mEditor.commit();
    }
    /**读取【滚动背景透明度】:name内容**/
    /**读取【滚动背景透明度】:列表默认位置**/
    public int getTerminalScrollingTransparency(){
        return mySharedPreferences.getInt(mScrollingTransparency, 255);
    }
    /**读取【滚动背景透明度】:设置的滚动背景透明度**/
    public int getTerminalScrollingTransparencyContent(){
        return mySharedPreferences.getInt(mScrollingTransparencyContent, 255);
    }

    /**清空数据**/
    public void clearData(){
        mEditor = mySharedPreferences.edit();
        mEditor.clear();
        mEditor.commit();
    }
}


猜你喜欢

转载自blog.csdn.net/alin693/article/details/60140793