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 does Android imitate the gift-giving function of live app?

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you "how Android imitates the gift-giving function of live app". The content is easy to understand and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how Android imitates the gift-giving function of live app".

Live broadcast interface

What is achieved is to play the local video file:

/ * LVB interface for interfacing LVB features * / public class LiveFrag extends Fragment {private ImageView img_thumb; private VideoView video_view; @ Nullable @ Override public View onCreateView (@ NonNull LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {View view = inflater.inflate (R.layout.frag_live, null); img_thumb = view.findViewById (R.id.img_thumb); img_thumb.setVisibility (View.GONE); video_view = view.findViewById (R.id.video_view) Video_view.setVisibility (View.VISIBLE); video_view.setVideoURI (Uri.parse ("android.resource://" + getActivity (). GetPackageName () + "/" + R.raw.video_1)); video_view.start (); video_view.setOnCompletionListener (new MediaPlayer.OnCompletionListener () {@ Override public void onCompletion (MediaPlayer mp) {video_view.setVideoURI (Uri.parse ("android.resource://" + getActivity () .getPackageName () + "/" + R.raw.video_1) / / or / mVideoView.setVideoPath (Uri.parse (_ filePath)); video_view.start ();}}); return view;}}

The layout file frag_live.xml is as follows:

Sliding hide effect

The effects that need to be achieved are as follows:

Custom DialogFragment, use ViewPager, the first is empty Fragment, the second is the Fragment we need, swipe left and right to switch between show and hide effects.

The interactive page InteractiveFrag of the audience function is as follows:

/ * interactive page with viewer function, slide hide effect * / public class InteractiveFrag extends DialogFragment {public View view; public Context myContext; private ViewPager vp_interactive; private LayerFrag layerFrag; @ Override public View onCreateView (LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {view = inflater.inflate (R.layout.frag_interactive, null); / / initialize initView (); initData (); return view } / * initialize View * / public void initView () {vp_interactive = view.findViewById (R.id.vp_interactive);} / * initialize data * / public void initData () {/ / EmptyFrag: nothing / / LayerFrag: interface / / this meets the need for sliding hidden interaction vp_interactive.setAdapter (new FragmentPagerAdapter (getChildFragmentManager ()) {@ Override public int getCount () {return 2) } @ Override public Fragment getItem (int position) {if (position = = 0) {return new EmptyFrag (); / return the empty interface fragment} else if (position = = 1) {return layerFrag = new LayerFrag (); / / return the frag} else of the interface {/ / set the default return new EmptyFrag (); / / set the default display interface vp_interactive.setCurrentItem (1) / / at the same time, change the interface to resize when the soft keyboard pops up, Fragment will not follow the mobile getDialog (). GetWindow (). SetSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);} @ Override public Dialog onCreateDialog (Bundle savedInstanceState) {/ / set the style of DialogFragment, the code here had better use me, do not change Dialog dialog = new Dialog (getActivity (), R.style.MainDialog) {@ Override public void onBackPressed () {super.onBackPressed () GetActivity (). Finish ();}; return dialog;}

The frag_interactive.xml file is as follows:

User interaction page LayerFrag:

Public class LayerFrag extends Fragment {@ Nullable @ Override public View onCreateView (@ NonNull LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {return inflater.inflate (R.layout.frag_layer, null);}}

Frag_layer:

EmptyFrag:

/ * * empty fragment * / public class EmptyFrag extends Fragment {@ Nullable @ Override public View onCreateView (@ NonNull LayoutInflater inflater, @ Nullable ViewGroup container, @ Nullable Bundle savedInstanceState) {return inflater.inflate (R.layout.frag_empty, null);}}

Frag_empty.xml:

Use FrameLayout layout in MainActivity to overlay the audience interaction page InteractiveFrag on the LVB page LiveFrag.

MainActivity:

Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); / / load LVB fragment LiveFrag liveFrag = new LiveFrag (); getSupportFragmentManager (). BeginTransaction (). Add (R.id.fl_root, liveFrag). Commit (); / load new InteractiveFrag (). Show (getSupportFragmentManager (), "InteractiveFrag");}}

Activity_main.xml:

User interaction page implementation

MagicTextView animation effect

The MagicTextView code is shown at the end of the article.

Let's first achieve the following animation effect

Animation:

Public class NumberAnim {private Animator lastAnimator; public void showAnimator (View v) {if (lastAnimator! = null) {lastAnimator.removeAllListeners (); lastAnimator.cancel (); lastAnimator.end ();} ObjectAnimator animScaleX = ObjectAnimator.ofFloat (v, "scaleX", 1.3f, 1.0f); ObjectAnimator animScaleY = ObjectAnimator.ofFloat (v, "scaleY", 1.3f, 1.0f); AnimatorSet animSet = new AnimatorSet (); animSet.playTogether (animScaleX, animScaleY); animSet.setDuration (200); lastAnimator = animSet; animSet.start () }} mtv_giftNum.setText ("x" + count); giftNumberAnim = new NumberAnim (); / / initialize digital animation mtv_giftNum.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View v) {count++; mtv_giftNum.setText ("x" + count); giftNumberAnim.showAnimator (mtv_giftNum);}})

Animation when the gift comes in

Enter the animation set to decelerate_interpolator deceleration interpolator:

/ * method for brushing gifts * / private void showGift (String tag) {View newGiftView = ll_gift_group.findViewWithTag (tag); / / whether there is a gift of this tag type if (newGiftView = = null) {/ / get gift newGiftView = getNewGiftView (tag); ll_gift_group.addView (newGiftView); / / play newGiftView.startAnimation (inAnim); final MagicTextView mtv_giftNum = newGiftView.findViewById (R.id.mtv_giftNum) InAnim.setAnimationListener (new Animation.AnimationListener () {@ Override public void onAnimationStart (Animation animation) {} @ Override public void onAnimationRepeat (Animation animation) {} @ Override public void onAnimationEnd (Animation animation) {giftNumberAnim.showAnimator (mtv_giftNum);}}) } else {/ / if there is already a gift of this type in the list, it will no longer be created, but directly take out / / update the logo and record the time of the latest modification, which is used to recycle and judge ImageView iv_gift = newGiftView.findViewById (R.id.iv_gift); iv_gift.setTag (System.currentTimeMillis ()); / / update the logo and update the number of gifts MagicTextView mtv_giftNum = newGiftView.findViewById (R.id.mtv_giftNum) Int giftCount = (int) mtv_giftNum.getTag () + 1; / / increasing mtv_giftNum.setText ("x" + giftCount); mtv_giftNum.setTag (giftCount); giftNumberAnim.showAnimator (mtv_giftNum) }} / * get a gift * / private View getNewGiftView (String tag) {/ / add a logo. If the view exists in layout, it will not be generated (used for findViewWithTag to determine whether it exists) View giftView = LayoutInflater.from (myContext). Evaluate (R.layout.item_gift, null); giftView.setTag (tag) / / add a logo, record the generation time, and be used to determine whether it is up-to-date during collection. The oldest ImageView iv_gift = giftView.findViewById (R.id.iv_gift); iv_gift.setTag (System.currentTimeMillis ()); / / add a logo to record the number of gifts MagicTextView mtv_giftNum = giftView.findViewById (R.id.mtv_giftNum); mtv_giftNum.setTag (1); mtv_giftNum.setText ("x1") Switch (tag) {case "gift01": iv_gift.setImageResource (GiftIcon [0]); break; case "gift02": iv_gift.setImageResource (GiftIcon [1]); break; case "gift03": iv_gift.setImageResource (GiftIcon [2]); break; case "gift04": iv_gift.setImageResource (GiftIcon [3]); break;} LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp.topMargin = 10; giftView.setLayoutParams (lp); return giftView } @ Override public void onClick (View v) {switch (v.getId ()) {case R.id.btn_gift01: / / Gift 1, soap showGift ("gift01"); break; case R.id.btn_gift02: / / Gift 2, Rose showGift ("gift02"); break; case R.id.btn_gift03: / / Gift 3, Love showGift ("gift03"); break Case R.id.btn_gift04: / / Gift 4, cake showGift ("gift04"); break;}}

Gift removal animation

The results are as follows:

Use the accelerate_interpolator acceleration differentiator when the gift is removed

/ * * remove giftView * / private void removeGiftView (final int index) {/ / remove the list from the gift list, plus exit the animation final View removeGiftView = ll_gift_group.getChildAt (index); outAnim.setAnimationListener (new Animation.AnimationListener () {@ Override public void onAnimationStart (Animation animation) {} @ Override public void onAnimationRepeat (Animation animation) {} @ Override public void onAnimationEnd (Animation animation) {ll_gift_group.removeViewAt (index);}})) / / enable the animation, because of timing reasons, it may be in the child thread getActivity () .runOnUiThread (new Runnable () {@ Override public void run () {removeGiftView.startAnimation (outAnim);}});}

If more than 3 gifts are displayed, remove the earliest gift:

/ / whether there is a gift of this tag type if (newGiftView = = null) {/ / determine whether there are already three gifts on the gift list. If so, delete one that has not been updated, and then add the new gift. Always keep only 3 if (ll_gift_group.getChildCount () > = 3) {/ / get the last update time of the first two elements View giftView01 = ll_gift_group.getChildAt (0) ImageView iv_gift01 = giftView01.findViewById (R.id.iv_gift); long lastTime1 = (long) iv_gift01.getTag (); View giftView02 = ll_gift_group.getChildAt (1); ImageView iv_gift02 = giftView02.findViewById (R.id.iv_gift); long lastTime2 = (long) iv_gift02.getTag (); if (lastTime1 > lastTime2) {/ / if the second View is displayed for a longer time removeGiftView (1) } else {/ / if the first View is displayed for a long time removeGiftView (0);}}.

Turn on the regular cleaning gift list

If the gift is displayed for more than a certain period of time, the gift will be automatically removed from the gift list:

/ * regularly clean gift list information * / private void clearTiming () {Timer timer = new Timer (); timer.schedule (new TimerTask () {@ Override public void run () {int childCount = ll_gift_group.getChildCount (); long nowTime = System.currentTimeMillis (); for (int I = 0; I)

< childCount; i++) { View childView = ll_gift_group.getChildAt(i); ImageView iv_gift = (ImageView) childView.findViewById(R.id.iv_gift); long lastUpdateTime = (long) iv_gift.getTag(); // 更新超过3秒就刷新 if (nowTime - lastUpdateTime >

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