天天看點

android 自定義TextView,内容指定位置滾動動畫效果

public class MyTextView extends TextView {

    private int mTextStartIndex;

    private int mEverySpaceTime;

    private String mTextContent;

    private String mSubTextContent;

    private boolean mIsStartAnimation;

    private ShowTextThread mShowTextThread;

    private int mViewVisiblity;

    private int mWindowVisiblity;

    private volatile boolean mViewVisible;

    public MyTextView(Context context, AttributeSet attrs) {

        super(context, attrs);

        init(attrs);

        setTextStyle(context);

        mTextContent = (String)getText();

        if (mIsStartAnimation) {

            mSubTextContent = mTextContent;

        }else{

            mSubTextContent = mTextContent.substring(0, mTextStartIndex);

        }

    }

    public boolean ismIsStartAnimation() {

        return mIsStartAnimation;

    }

    public void setmIsStartAnimation(boolean mIsStartAnimation) {

        this.mIsStartAnimation = mIsStartAnimation;

    }

    public int getmTextStartIndex() {

        return mTextStartIndex;

    }

    public void setmTextStartIndex(int mTextStartIndex) {

        this.mTextStartIndex = mTextStartIndex;

    }

    public int getmEverySpaceTime() {

        return mEverySpaceTime;

    }

    public void setmEverySpaceTime(int mEverySpaceTime) {

        this.mEverySpaceTime = mEverySpaceTime;

    }

    private void init(AttributeSet attrs) {

        TypedArray t = getContext().obtainStyledAttributes(attrs, R.styleable.NewTextViewElement);

        mEverySpaceTime = t.getInteger(R.styleable.NewTextViewElement_spaceTime, 300);

        mTextStartIndex = t.getInteger(R.styleable.NewTextViewElement_animationStartIndex, 1);

        mIsStartAnimation = t.getBoolean(R.styleable.NewTextViewElement_isStartAnimation, false);

        t.recycle();

    }

    @Override

    protected void onDraw(Canvas canvas) {

        setText(mSubTextContent);

        super.onDraw(canvas);

    }

    class ShowTextThread extends Thread {

        public void run() {

            changeText();

        }

        private void changeText() {

            final int text_length = getText().length();

            if (mTextStartIndex > (text_length - 1)) {

                return;

            }

            int currentIndex = mTextStartIndex;

            while (true) {

                if (!mViewVisible) {

                    break;

                }

                mSubTextContent = mTextContent.substring(0, currentIndex);

                postInvalidate();

                if (currentIndex == text_length) {

                    currentIndex = mTextStartIndex;

                } else {

                    currentIndex++;

                }

                try {

                    sleep(mEverySpaceTime);

                } catch (InterruptedException e) {

                    e.printStackTrace();

                }

            }

        }

    }

    public void setAnimationTextStartIndex(int index) {

        mTextStartIndex = index;

    }

    private void setTextStyle(Context context) {

        AssetManager mgr = context.getAssets();

        Typeface createFromAsset = Typeface.createFromAsset(mgr, "yf.ttf");

        setTypeface(createFromAsset);

    }

    @Override

    protected void onWindowVisibilityChanged(int visibility) {

        super.onWindowVisibilityChanged(visibility);

        mWindowVisiblity = visibility;

        viewVisiblityChanged();

    }

    @Override

    protected void onVisibilityChanged(View changedView, int visibility) {

        super.onVisibilityChanged(changedView, visibility);

        mViewVisiblity = visibility;

        viewVisiblityChanged();

    }

    private void viewVisiblityChanged() {

        boolean visible = (mViewVisiblity | mWindowVisiblity) == View.VISIBLE;

        if (visible == mViewVisible) {

            return;

        }

        mViewVisible = visible;

        if (mViewVisible&&mIsStartAnimation) {

//            if (mShowTextThread == null) {

//                mShowTextThread = new ShowTextThread();

//                mShowTextThread.start();

//            }

//        } else {

//            if (mShowTextThread != null) {

//                mShowTextThread.interrupt();

//            }

            new Thread(){

                public void run() {

                    changeText();

                }

                private void changeText() {

                    final int text_length = getText().length();

                    if (mTextStartIndex > (text_length - 1)) {

                        return;

                    }

                    int currentIndex = mTextStartIndex;

                    while (true) {

                        if (!mViewVisible) {

                            break;

                        }

                        mSubTextContent = mTextContent.substring(0, currentIndex);

                        postInvalidate();

                        if (currentIndex == text_length) {

                            currentIndex = mTextStartIndex;

                        } else {

                            currentIndex++;

                        }

                        try {

                            sleep(mEverySpaceTime);

                        } catch (InterruptedException e) {

                            e.printStackTrace();

                        }

                    }

                }

            }.start();

        }

    }

}

xmlns:xsetting="http://schemas.android.com/apk/res/com.itshow"

//xmlns:xsetting="http://schemas.android.com/apk/res/包名"

        <com.itshow.view.MyTextView

            android:layout_width="170px"

            android:layout_height="wrap_content"

            android:text="@string/xxx"       如:123456789

            xsetting:animationStartIndex="3"

            xsetting:isStartAnimation="true"

            xsetting:spaceTime="500" />

繼續閱讀