Android realizes the effect of vertical horse race

  • 2021-09-05 00:50:21
  • OfStack

In our development process, The function of horse racing lantern is very practical. In the implementation of this function, this time we usually need to find demo to achieve this method, I found from github above this demo feel very good, so it is necessary to achieve this function MarqueeView, look at this tool class, because I find this class when there is no click event, so I added a click event to it, look at this tool class


public class MarqueeView extends ViewFlipper {

 private Context mContext;
 private List<String> notices;
 private boolean isSetAnimDuration = false;
 private int contentSize;
 private int interval = 1000;
 private int animDuration = 500;
 private int textSize = 14;
 private int textColor = 0xffffffff;
 private int gravity = Gravity.LEFT | Gravity.CENTER_VERTICAL;
 // Click event 
 private OnItemClickListener onItemClickListener;

 public MarqueeView(Context context, AttributeSet attrs) {
 super(context, attrs);
 init(context, attrs, 0);
 }

 private void init(Context context, AttributeSet attrs, int defStyleAttr) {
 this.mContext = context;
 if (notices == null) {
 notices = new ArrayList<>();
 }

 TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeViewStyle, defStyleAttr, 0);
 interval = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvInterval, interval);
 isSetAnimDuration = typedArray.hasValue(R.styleable.MarqueeViewStyle_mvAnimDuration);
 animDuration = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvAnimDuration, animDuration);
 if (typedArray.hasValue(R.styleable.MarqueeViewStyle_mvTextSize)) {
 textSize = (int) typedArray.getDimension(R.styleable.MarqueeViewStyle_mvTextSize, textSize);
 textSize = DisplayUtil.px2sp(mContext, textSize);
 }
 textColor = typedArray.getColor(R.styleable.MarqueeViewStyle_mvTextColor, textColor);
 typedArray.recycle();

 setFlipInterval(interval);

 Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
 if (isSetAnimDuration) animIn.setDuration(animDuration);
 setInAnimation(animIn);

 Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
 if (isSetAnimDuration) animOut.setDuration(animDuration);
 setOutAnimation(animOut);
 }

 //  Start carousel according to announcement string 
 public void startWithText(final String notice) {
 if (TextUtils.isEmpty(notice)) return;
 getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
 @Override
 public void onGlobalLayout() {
 getViewTreeObserver().removeGlobalOnLayoutListener(this);
 startWithFixedWidth(notice, getWidth());
 }
 });
 }

 //  Start carousel according to the announcement string list 
 public void startWithList(List<String> notices) {
 setNotices(notices);
 start();
 }

 //  Start carousel based on width and announcement string 
 private void startWithFixedWidth(String notice, int width) {
 int noticeLength = notice.length();
 int dpW = DisplayUtil.px2dip(mContext, width);
 int limit = dpW / textSize;
 if (dpW == 0) {
 throw new RuntimeException("Please set MarqueeView width !");
 }

 if (noticeLength <= limit) {
 notices.add(notice);
 } else {
 int size = noticeLength / limit + (noticeLength % limit != 0 ? 1 : 0);
 for (int i = 0; i < size; i++) {
 int startIndex = i * limit;
 int endIndex = ((i + 1) * limit >= noticeLength ? noticeLength : (i + 1) * limit);
 notices.add(notice.substring(startIndex, endIndex));
 }
 }
 start();
 }

 //  Start carousel 
 public boolean start() {
 if (notices == null || notices.size() == 0) return false;
 removeAllViews();

 for (int i = 0; i < notices.size(); i++) {
 final TextView textView = createTextView(notices.get(i), i);
 final int finalI = i;
 textView.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
  if (onItemClickListener != null) {
  onItemClickListener.onItemClick(finalI, textView);
  }
 }
 });
 addView(textView);
 }

 if (notices.size() > 1) {
 startFlipping();
 }
 return true;
 }

 //  Create ViewFlipper Under TextView
 private TextView createTextView(String text, int position) {
 TextView tv = new TextView(mContext);
 tv.setGravity(gravity);
 tv.setText(text);
 tv.setTextColor(textColor);
 tv.setTextSize(textSize);
 tv.setTag(position);
 return tv;
 }

 public List<String> getNotices() {
 return notices;
 }

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

 public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
 this.onItemClickListener = onItemClickListener;
 }

 public interface OnItemClickListener {
 void onItemClick(int position, TextView textView);
 }

}

This is how it is implemented. I added click events to it, so its usage is like this


<com.redsun.property.views.MarqueeView
 android:id="@+id/vertical_switch_textview1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerInParent="true"
 android:layout_toLeftOf="@+id/total_quantity"
 android:layout_toRightOf="@+id/news_image"
 android:background="@color/white"
 android:ellipsize="end"
 android:maxEms="10"
 android:maxLength="10"
 android:textColor="@color/gray_dark"
 android:textSize="@dimen/font_normal"
 app:mvAnimDuration="1000"
 app:mvInterval="3000"
 app:mvTextColor="@color/black"
 app:mvTextSize="14sp"
 tools:text=" Hiro Life APP Revised "
 />

verticalSwitchTextView1 = (MarqueeView) rootView.findViewById(R.id.vertical_switch_textview1);
List<String> info = new ArrayList<>();
info.add("1. Able to adapt to multiple long texts Android TextView Examples of ");
info.add("2.\" Kobe Bryant! ");
info.add("3. GitHub Account number: zhangyuanchong");
info.add("4.\" The understanding is also very simple, ");
info.add("5.  Cracking key ");
info.add("6.  Two ways are implemented ");
verticalSwitchTextView1.startWithList(info);
verticalSwitchTextView1.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
 @Override
 public void onItemClick(int position, TextView textView) {
 position = position + 1;
 Toast.makeText(getActivity(), " Clicked " + position, Toast.LENGTH_SHORT).show();
 }
});

In this way, it can be realized directly, but it is actually quite simple.


Related articles: