通过TextSwitcher实现上下滚动的广告动画效果


1.布局xml


2.代码

//===========================================================
    @InjectView(R.id.profileSwitcher)
    TextSwitcher textSwitcher;
    private BitHandler bitHandler;
    private String[] strings={"text00001","text00002"};
    private int index = 0;
    private void test(){
        textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                TextView textView = new TextView(mContext);
                textView.setSingleLine();
                textView.setTextSize(15);
                textView.setTextColor(Color.parseColor("#ff0000"));
                textView.setEllipsize(TextUtils.TruncateAt.END);
                FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
                );
                lp.gravity = Gravity.CENTER;
                textView.setLayoutParams(lp);
                return textView;
            }
        });
        bitHandler = new BitHandler();
        new myThread().start();
    }

    class BitHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            textSwitcher.setText(strings[index]);
            index++;
            if (index == strings.length) {
                index = 0;
            }
        }
    }

    private class myThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (index < strings.length) {
                try {
                    synchronized (this) {
                        bitHandler.sendEmptyMessage(0);
                        this.sleep(2000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

3.anim

push_up_in



    

    


push_up_out



    

    

实现效果:





你可能感兴趣的:(通过TextSwitcher实现上下滚动的广告动画效果)