垂直、水平滚动的TextView

 

源码来自于:https://github.com/Glgo/AutoVerticalScrollTextView

 

 

 public View makeView() {
        TextView textView = new TextView(mContext);
        textView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        textView.setTextSize(14);
        textView.setSingleLine(true);
        textView.setGravity(Gravity.CENTER_VERTICAL);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        textView.setTextColor(getResources().getColor(R.color.mainbbscolor));
        return textView;

    }

 

 

 

其中这个方法就是对TextView进行设置,做出自己想要的效果。下面是直接使用方法:

 

 

  /**
     * 首页广告
     */
    private void initAD(){
        //上层滚动
        verticalUpScrollTV = (AutoVerticalScrollTextView)inflate.findViewById(R.id.textview_auto_roll_up);
        verticalUpScrollTV.setText(testAD.get(0));

        //滚动广告点击事件
        verticalUpScrollTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), testAD.get(upNumber % testAD.size()), Toast.LENGTH_SHORT).show();
            }
        });

        new Thread(){
            @Override
            public void run() {
                while (isRunning){
                    SystemClock.sleep(5000);
                    handler.sendEmptyMessage(199);
                }
            }
        }.start();
    }

    /**
     * 线程
     */
    private Handler handler = new Handler() {

        public void handleMessage(Message msg) {
            if (msg.what == 199) {
                verticalUpScrollTV.next();
                upNumber++;
                verticalUpScrollTV.setText(testAD.get(upNumber%testAD.size()));
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        //关闭界面时关闭广告轮播线程
        isRunning=false;
    }

 

 

其中开了一个线程,记得关闭。
 

你可能感兴趣的:(安卓)