textview 文字轮播 并实现动态修改文字大小和颜色

   日期:2020-08-08     浏览:81    评论:0    
核心提示:网上不少实现文字轮播的思路啊,有ViewFlipper思路这个要可以实现轮播效果,但是动态修改文字颜色,我修改完后,我感到不满意,寻找其他思路,如果你想要代码我给你,这个不完美的解决了动态设置文字颜色.,为啥呢,你看看标颜色的代码,我用两条路出发解决了这个问题,虽然效果看不出来,但是我总感觉怪怪的,有人说我直接修改textColor 不就行了,为啥要写for (int i =0 ;i < notices.size() ; i++){ Logger.e(xiugaiyasne,i

网上不少实现文字轮播的思路啊,

ViewFlipper思路

这个要可以实现轮播效果,但是动态修改文字颜色,我修改完后,我感到不满意,寻找其他思路,如果你想要代码我给你,这个不完美的解决了动态设置文字颜色.,为啥呢,你看看标颜色的代码,我用两条路出发解决了这个问题,虽然效果看不出来,但是我总感觉怪怪的,有人说我直接修改textColor 不就行了,为啥要写
 this.textColor = textColor;

for (int i =0 ;i < notices.size() ; i++){

    if(getChildAt(i)!= null){
        ((TextView) (((RelativeLayout)getChildAt(i)).findViewWithTag("lbnr"))).setTextColor(textColor);
    }
}

因为只写修改textColor .颜色不会立马变回来,太丑了.




public class MyView extends ViewFlipper {
    private int interval = 3000;
    private boolean singleLine = true;
    private int textSize = 20;
    private int TimetextSize = 20;
    private int textColor = Color.BLUE;
    private boolean ischangetextcolor= false;
    private int TimetextColor = Color.RED;
    private int animDuration = 1000;
    @AnimRes
    private int inAnimResId = R.anim.anim_notificationview_bottom_in;
    @AnimRes
    private int outAnimResId = R.anim.anim_notificationview_top_out;

    private List<String> notices = new ArrayList<>();
    private List<String> noticestime = new ArrayList<>();
    private int position = 0;
    private OnItemClickListener onItemClickListener;


    public MyNotificationView(Context context) {
        super(context);
    }

    public MyNotificationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);

    }
    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyNotificationView);
        interval = typedArray.getInteger(R.styleable.MyNotificationView_mvInterval, interval);
        singleLine = typedArray.getBoolean(R.styleable.MyNotificationView_mvSingleLine, false);
        if (typedArray.hasValue(R.styleable.MyNotificationView_mvTextSize)) {
            textSize = (int) typedArray.getDimension(R.styleable.MyNotificationView_mvTextSize, textSize);
            textSize = px2sp(context, textSize);
        }
        if (typedArray.hasValue(R.styleable.MyNotificationView_mvTimeTextSize)) {
            TimetextSize = (int) typedArray.getDimension(R.styleable.MyNotificationView_mvTimeTextSize, textSize);
            TimetextSize = px2sp(context, TimetextSize);
        }
        textColor = typedArray.getColor(R.styleable.MyNotificationView_mvTextColor, textColor);
        TimetextColor = typedArray.getColor(R.styleable.MyNotificationView_mvTimeTextColor, TimetextColor);
        typedArray.recycle();
        setFlipInterval(interval);
    }
    // 将px值转换为sp值
    private   int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);

    }

    public static <T> boolean isEmpty(List<T> list) {
        if (list == null || list.size() == 0) {
            return true;
        }
        return false;
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public int getPosition() {
        return (int) getCurrentView().getTag();
    }
    public void setMyTextColor(int textColor) {
        this.textColor = textColor;
//        ((TextView) (getCurrentView().findViewWithTag("lbnr"))).setTextColor(textColor);
        for (int i =0 ;i < notices.size() ; i++){
            Logger.e("xiugaiyasne",i+"fzwk");
            ((TextView) (getChildAt(i).findViewWithTag("lbnr"))).setTextColor(textColor);
        }

    }
    public void setNotices(List<String> notices) {
        this.notices = notices;
    }
    public void setNoticesTime(List<String> mnoticestime) {
        this.noticestime = mnoticestime;
    }

    public void setList(List<String> notices, List<String> noticestime) {
        if (isEmpty(notices)){
            addView(createLayout("",""));
        }else {
            setNotices(notices);
            setNoticesTime(noticestime);
            StartAnimation(inAnimResId, outAnimResId);
        }
    }

    private void StartAnimation(final int inAnimResId, final int outAnimResId) {
        post(new Runnable() {
            @Override
            public void run() {
                start(inAnimResId, outAnimResId);
            }
        });
    }
    private boolean isAnimStart = false;
    private boolean isAnimStart2= false;

    private void start(final @AnimRes int inAnimResId, final @AnimRes int outAnimResID) {
        removeAllViews();
        clearAnimation();
//        position = 0;
        addView(createLayout(notices.get(position),noticestime.get(position)));
       if (notices.size() > 1) {
            setInAndOutAnimation(inAnimResId, outAnimResID);
            startFlipping();
        }

        if (getInAnimation() != null) {
            getInAnimation().setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    if (isAnimStart) {
                        animation.cancel();
                    }
                    isAnimStart = true;
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    position++;
                    if (position >= notices.size()) {
                        position = 0;
                    }
                    View view = createLayout(notices.get(position),noticestime.get(position));
                    if (view.getParent() == null) {
                        addView(view);
                    }
                    isAnimStart = false;
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
        }
    }


    private View createLayout(CharSequence text, CharSequence texttime) {
        RelativeLayout relativeLayout = (RelativeLayout) getChildAt((getDisplayedChild() + 1) % 3);
        TextView textView = null;
        TextView textView02 = null;
        if (relativeLayout == null) {
            relativeLayout = new RelativeLayout(getContext());
            relativeLayout.setGravity(RelativeLayout.CENTER_IN_PARENT);
        }else {
            if (relativeLayout.getChildCount()>0)  relativeLayout.removeAllViews();
            relativeLayout.setGravity(RelativeLayout.CENTER_IN_PARENT);
        }
        RelativeLayout.LayoutParams mLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams mLayoutParams02 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
        textView = new TextView(getContext());
        textView02 = new TextView(getContext());

        textView.setTextColor(textColor);
        textView.setTextSize(textSize);
        textView.setSingleLine(singleLine);
        textView.setTag("lbnr");
        textView02.setTextColor(TimetextColor);
        textView02.setTextSize(TimetextSize);
        textView02.setSingleLine(singleLine);
        textView02.setTag("lbsj");
        textView.setMaxEms(16);
        textView.setEllipsize(TextUtils.TruncateAt.END);
        if (TextUtils.isEmpty(text)) {
            textView.setText("点击使用搜受功能");
        }else {
            textView.setText(text);
        }
        if (!TextUtils.isEmpty(texttime)) textView02.setText(texttime);
        relativeLayout.setTag(position);
        mLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        mLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        relativeLayout.addView(textView,mLayoutParams);
        mLayoutParams02.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        mLayoutParams02.addRule(RelativeLayout.CENTER_VERTICAL);
        relativeLayout.addView(textView02,mLayoutParams02);
        relativeLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(getPosition());
                }
            }
        });
        return relativeLayout;

    }
    
    private void setInAndOutAnimation(@AnimRes int inAnimResId, @AnimRes int outAnimResID) {
        Animation inAnim = AnimationUtils.loadAnimation(getContext(), inAnimResId);
        inAnim.setDuration(animDuration);
        setInAnimation(inAnim);

        Animation outAnim = AnimationUtils.loadAnimation(getContext(), outAnimResID);
        outAnim.setDuration(animDuration);
        setOutAnimation(outAnim);
    }
}

 

至于动画的xml你自己随便搜一个就行,我知道你也不会写,能看到我这博客.....我也不想让你太方便哈哈哈.

TextSwitcher思路
这个思路我喜欢,因为这个就是一个简单的思路,是android官方的给实现轮播最简单的方案吧,看代码
修改颜色,直接找到工厂中的两个来回轮换的textview,直接设置颜色,完美解决了,(这个局限性就是只能写文字轮播啊,android还给了一个图片轮播的父类imageSwitcher.但是上一个方案啥都能轮播,多复杂都行,你权衡下)

**
 * @author:lkk on2020-08-05 20:23:05
 */
public class TextSwitchView extends TextSwitcher implements ViewSwitcher.ViewFactory{

    private MyNotificationView.OnItemClickListener onItemClickListener;
    private int index= -1;
    private int  flag= 0;
    private Context context;
    private int textColor = Color.WHITE;
    private int textSize = 14;
    private String[] resources = {"000","111","222","333"};
    private Timer timer;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
                case 1:
                    index = next();
                    updateText();
                    break;
            }
            super.handleMessage(msg);
        }
    };


    //自定义View的构造方法
    public TextSwitchView(Context context) {
        super(context);
        this.context = context;
        init();
    }

    public TextSwitchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        if (timer == null) {
            timer = new Timer();
        }
        //实现ViewSwitcher.ViewFactory接口方法,创建出TextView并启动动画
        setFactory(this);
        setInAnimation(AnimationUtils.loadAnimation(context, R.anim.anim_notificationview_bottom_in));
        setOutAnimation(AnimationUtils.loadAnimation(context, R.anim.anim_notificationview_top_out));
    }
    public void setResources(String[] res) {
        resources = res;
    }
    public void setTextColor(int color) {
        textColor = color;
        ( (TextView) getCurrentView()).setTextColor(textColor);
        ( (TextView) getNextView()).setTextColor(textColor);
//        ( (TextView) getChildAt(0)).setTextColor(textColor);
//        ( (TextView) getChildAt(1)).setTextColor(textColor);
    }
    public void setTextSize(int size) {
        textSize = size;
        ((TextView) getCurrentView()).setTextSize(size);
        ((TextView) getNextView()).setTextSize(size);
    }
    //这个是自定义View的启动点,从外面传进来的间隔时间,并以此来开启这个定时任务器
    public void setTextStillTime(long time) {
        if (timer == null) {
            timer = new Timer();
        } else {
            timer.scheduleAtFixedRate(new MyTask(), 1, time);
        }
    }

    //启动任务,每间隔time时间发送一个消息给handler更新文字
    private class MyTask extends TimerTask {
        @Override
        public void run() {
            mHandler.sendEmptyMessage(1);
        }
    }

    private int next() {
        flag = index + 1;
        if (flag > resources.length - 1) {
            flag = flag - resources.length;
        }
        return flag;
    }

    private void updateText() {
        setText(resources[index]);
        ((TextView) getCurrentView()).setTag(index);
    }

    @Override
    public View makeView() {
        TextView tv = new TextView(context);
        tv.setTextColor(textColor);
        tv.setTextSize(textSize);
        tv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(getPosition());
                }
            }
        });
        return tv;
    }

    private int getPosition() {
        return (int) getCurrentView().getTag();
    }

    public void setOnItemClickListener(MyNotificationView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemClick(int position);
    }

}


不知道为啥,大家都没提供动态修改颜色的方法,是因为太简单了?还是太复杂了?还是根本没人会
有这样变态的需求啊.我想信随着浸入式的普及,越来越多人会有这个疑问的
 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服