美文网首页
Android 文字上下滚动显示

Android 文字上下滚动显示

作者: 因为我的心 | 来源:发表于2023-03-12 11:49 被阅读0次

    一、前言:

    我们APP的通知要在类似输入框中,文字上下滚动显示,效果如下:

    图片.png
    gitee地址:https://gitee.com/lyyon/MarqueeTextViewDemo

    二、代码:

    1、调用:

      //组装数据
            var announcementList = mutableListOf<Announce>()
            announcementList.add(Announce("标题1"))
            announcementList.add(Announce("标题2"))
            announcementList.add(Announce("标题3"))
            //调用方法
            binding.tvContext.setTextArraysAndClickListener(announcementList,
                MarqueeTextViewClickListener { view, position ->
                    val title = announcementList.getOrNull(position)?.title
                    Log.d("lyy","----title:${title}")
                    //执行对应的跳转逻辑
                })
    

    2、XML

      <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="40dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/btn_click"
                android:layout_marginTop="35dp"
                android:background="@drawable/btn_shap_13"
                android:layout_marginHorizontal="20dp"
                >
    
                <ImageView
                    android:id="@+id/iv_horn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@mipmap/bookshelf_horn"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    android:layout_marginStart="9dp"
                    />
                <com.youjiakeji.helloaa.tools.MarqueeTextView
                    android:id="@+id/tv_context"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="7dp"
                    tools:text="新用户更享好礼活动 "
                    tools:ignore="MissingConstraints"
                    app:layout_constraintStart_toEndOf="@+id/iv_horn"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintBottom_toBottomOf="parent"
                    />
    
            </androidx.constraintlayout.widget.ConstraintLayout>
    

    3、Announce对象

    public class Announce {
    
        /**
         * title : 每日更新好书,等你来读
         * content : 每日更新好书,等你来读。
         */
        private String title;
        private String content;
    
        public Announce(String title) {
            this.title = title;
        }
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
        @Override
        public String toString() {
            return "Announce{" +
                    "title='" + title + '\'' +
                    ", content='" + content + '\'' +
                    '}';
        }
    }
    

    4、MarqueeTextView

    package com.youjiakeji.helloaa.tools;
    
    import android.content.Context;
    import android.graphics.Color;
    import android.os.Handler;
    import android.os.Looper;
    import android.util.AttributeSet;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.animation.AnimationUtils;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.ViewFlipper;
    
    import com.youjiakeji.helloaa.R;
    
    import java.util.List;
    
    /**
     * 文字上下滚动
     */
    public class MarqueeTextView extends LinearLayout {
    
        private Context mContext;
        private ViewFlipper viewFlipper;
        private View marqueeTextView;
        private List<Announce> textArrays;
        private MarqueeTextViewClickListener marqueeTextViewClickListener;
        int mWidth;
        int mHeight;
        private Handler mHandler;
    
        public MarqueeTextView(Context context) {
            super(context);
            mContext = context;
            initBasicView();
        }
    
        public MarqueeTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mContext = context;
            initBasicView();
        }
    
        public void setTextArraysAndClickListener(List<Announce> textArrays, MarqueeTextViewClickListener marqueeTextViewClickListener) {
            this.textArrays = textArrays;
            this.marqueeTextViewClickListener = marqueeTextViewClickListener;
            initMarqueeTextView(textArrays, marqueeTextViewClickListener);
        }
    
        public void initBasicView() {
            mHandler = new Handler(Looper.getMainLooper());
            marqueeTextView = LayoutInflater.from(mContext).inflate(R.layout.marquee_textview_layout, null);
            LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            addView(marqueeTextView, layoutParams);
            viewFlipper = marqueeTextView.findViewById(R.id.viewFlipper);
            viewFlipper.setInAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_in_bottom));
            viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_top));
            viewFlipper.startFlipping();
            viewFlipper.setFlipInterval(5000);
        }
    
        public void initMarqueeTextView(List<Announce> textArrays, final MarqueeTextViewClickListener marqueeTextViewClickListener) {
            if (textArrays.size() == 0) {
                return;
            }
            viewFlipper.removeAllViews();
            checkInit();
        }
    
        /**
         * 检测控件是否初始化完毕
         */
        public void checkInit() {
    
            Runnable checkRunnable = new Runnable() {
                @Override
                public void run() {
                    if (mHeight > 0) {
                        int i = 0;
                        while (i < textArrays.size()) {
                            final int j = i;
                            TextView textView = new TextView(mContext);
                            textView.setLines(1);
                            textView.setTextColor(Color.WHITE);
                            textView.setText(textArrays.get(i).getTitle());
                            textView.setTextSize(15);
                            textView.setGravity(Gravity.CENTER_VERTICAL);
                            textView.setOnClickListener(new OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    marqueeTextViewClickListener.onClick(v, j);
                                }
                            });
                            ViewFlipper.LayoutParams lp = new ViewFlipper.LayoutParams(ViewFlipper.LayoutParams.MATCH_PARENT, mHeight);
                            viewFlipper.addView(textView, lp);
                            i++;
                        }
                        mHandler.removeCallbacks(this);
                    } else {
                        mHandler.postDelayed(this, 5);
                    }
                }
            };
            // 开始检测
            mHandler.post(checkRunnable);
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
        }
    
        public void releaseResources() {
            if (marqueeTextView != null) {
                if (viewFlipper != null) {
                    viewFlipper.stopFlipping();
                    viewFlipper.removeAllViews();
                    viewFlipper = null;
                }
                marqueeTextView = null;
            }
        }
    }
    

    5、MarqueeTextViewClickListener

    package com.youjiakeji.helloaa.tools;
    
    import android.view.View;
    
    /**
     * Created by administrator on 2016/11/28.
     */
    
    public interface MarqueeTextViewClickListener {
        void onClick(View view, int position);
    }
    

    6、slide_in_bottom

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromYDelta="50%p"
            android:toYDelta="0" />
        <alpha
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromAlpha="0.0"
            android:toAlpha="1.0" />
    </set>
    

    7、slide_out_top

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <translate
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromYDelta="0"
            android:toYDelta="-50%p" />
        <alpha
            android:duration="@android:integer/config_mediumAnimTime"
            android:fromAlpha="1.0"
            android:toAlpha="0.0" />
    </set>
    

    8、marquee_textview_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
        >
    
        <ViewFlipper
            android:id="@+id/viewFlipper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            >
        </ViewFlipper>
    </LinearLayout>
    

    相关文章

      网友评论

          本文标题:Android 文字上下滚动显示

          本文链接:https://www.haomeiwen.com/subject/cqgfrdtx.html