Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Android to imitate the infinite rotation effect of JD.com and KuaiBao

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use Android to imitate JD.com KuaiBao unlimited rotation effect, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

The content of the announcement slides up from the middle about every 3 seconds, while the next item slides in from the bottom up. The whole process is accompanied by the gradual disappearance of the content, and the animation effect is very smooth.

It is easier to implement with ViewFlipper. Take a look at the official comments for the ViewFlipper class:

Simple {@ link ViewAnimator} that will animate between two or more views that have been added to it. Only one child is shown at a time. If requested, can automatically flip between each child at a regular interval.

ViewFlipper is a container that can switch between two or more sub-View animations added to it, and only one child is displayed at a point in time. And can automatically switch to a child every other time period. To achieve the switching effect of JD.com and KuaiBao, we only need to set it to TextView and add it to ViewFlipper according to the announcement content of the rotation, and animate the switch between them.

To facilitate direct reuse in the project, we can customize it as a control NoticeView that inherits from ViewFlipper.

Public class NoticeView extends ViewFlipper implements View.OnClickListener {private Context mContext; private List mNotices; private OnNoticeClickListener mOnNoticeClickListener; public NoticeView (Context context) {super (context);} public NoticeView (Context context, AttributeSet attrs) {super (context, attrs); init (context);} private void init (Context context) {mContext = context; / / 3s setFlipInterval (3000); / / Inner margin 5dp setPadding (dp2px (5f), dp2px (5f)) / / animate enter and leave setInAnimation (AnimationUtils.loadAnimation (mContext, R.anim.notice_in)); setOutAnimation (AnimationUtils.loadAnimation (mContext, R.anim.notice_out));} / * add announcements that need to be displayed in rotation * * @ param notices * / public void addNotice (List notices) {mNotices = notices; removeAllViews (); for (int I = 0; I < mNotices.size () Build a TextView String notice = notices.get (I) based on the announcement content; TextView textView = new TextView (mContext); textView.setSingleLine (); textView.setText (notice); textView.setTextSize (20f); textView.setEllipsize (TextUtils.TruncateAt.END); textView.setTextColor (Color.parseColor ("# 666666")); textView.setGravity (Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL); / / the tag that sets the location of the announcement to textView is called back to user textView.setTag (I) TextView.setOnClickListener (this); / / add to ViewFlipper NoticeView.this.addView (textView, new LayoutParams (LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);}} @ Override public void onClick (View v) {int position = (int) v.getTag (); String notice = (String) mNotices.get (position); if (mOnNoticeClickListener! = null) {mOnNoticeClickListener.onNotieClick (position, notice) }} / * Notification Click listener Interface * / public interface OnNoticeClickListener {void onNotieClick (int position, String notice);} / * set Notification Click listener * * @ param onNoticeClickListener Notification Click listener * / public void setOnNoticeClickListener (OnNoticeClickListener onNoticeClickListener) {mOnNoticeClickListener = onNoticeClickListener;} private int dp2px (float dpValue) {return (int) TypedValue.applyDimension (TypedValue.COMPLEX_UNIT_DIP, dpValue, mContext.getResources (). GetDisplayMetrics ());}}

Layout file

Style of layout

The announcement content enters the animation notice_in.xml.

The announcement content slides out of the animation notice_out.xml

Just use it directly in Activity or Fragment.

/ / define it as a method, just call private void init () {NoticeView noticeView = (NoticeView) getActivity (). FindViewById (R.id.notice_view); List notices = new ArrayList (); notices.add ("unpack your blessings and take hundreds of millions of red envelopes for the New year"); notices.add ("Home Appliances 50% discount Troupe, grab billions of unthreshold cash red envelopes"); notices.add ("Star Wars razors first send 200 yuan vouchers") NoticeView.addNotice (notices); noticeView.startFlipping ();}

The above is all the content of this article "how to use Android to imitate the unlimited rotation effect of JD.com and KuaiBao". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report