package com.datarecovery.master.widget; import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Handler; import android.text.TextUtils; import android.util.Pair; import android.view.LayoutInflater; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.TranslateAnimation; import android.widget.ViewSwitcher; import com.datarecovery.master.databinding.ViewHomePageSwitchTextBinding; import java.util.List; public class InformationSwitchBanner implements ViewSwitcher.ViewFactory, View.OnClickListener { private static final int DURATION = 1500; private final Context context; private final ViewSwitcher viewSwitcher; private final Handler handler = new Handler(); LayoutInflater inflater; private List> texts; private int marker; private AnimationSet InAnimationSet; private AnimationSet OutAnimationSet; private int delayTime = 1500; private OnSwitchListener onSwitchListener; public InformationSwitchBanner(Context context, ViewSwitcher textSwitcher) { this.context = context; this.viewSwitcher = textSwitcher; inflater = LayoutInflater.from(context); init(); } private void init() { viewSwitcher.setFactory(this); setText("", ""); createAnimation(); viewSwitcher.setInAnimation(InAnimationSet); viewSwitcher.setOutAnimation(OutAnimationSet); viewSwitcher.setOnClickListener(this); } private void setText(CharSequence left, CharSequence right) { ViewHomePageSwitchTextBinding binding = ViewHomePageSwitchTextBinding.bind(viewSwitcher.getNextView()); binding.tvSwitchText.setText(left); binding.tvSwitchDate.setText(right); binding.tvSwitchText.setVisibility(TextUtils.isEmpty(left) ? View.GONE : View.VISIBLE); binding.tvSwitchDate.setVisibility(TextUtils.isEmpty(right) ? View.GONE : View.VISIBLE); viewSwitcher.showNext(); } public void setTextColor(Drawable leftDrawable, int padding, int prefixColor, int suffixColor) { ViewHomePageSwitchTextBinding currentView = ViewHomePageSwitchTextBinding.bind(viewSwitcher.getCurrentView()); ViewHomePageSwitchTextBinding nextView = ViewHomePageSwitchTextBinding.bind(viewSwitcher.getNextView()); currentView.tvSwitchText.setTextColor(prefixColor); nextView.tvSwitchText.setTextColor(prefixColor); currentView.tvSwitchDate.setTextColor(suffixColor); nextView.tvSwitchDate.setTextColor(suffixColor); leftDrawable.setBounds(0, 0, leftDrawable.getMinimumWidth(), leftDrawable.getMinimumHeight()); currentView.tvSwitchText.setCompoundDrawablePadding(padding); nextView.tvSwitchText.setCompoundDrawablePadding(padding); currentView.tvSwitchText.setCompoundDrawables(leftDrawable, null, null, null); nextView.tvSwitchText.setCompoundDrawables(leftDrawable, null, null, null); } @Override public View makeView() { ViewHomePageSwitchTextBinding viewHomePageSwitchTextBinding = ViewHomePageSwitchTextBinding.inflate(inflater); return viewHomePageSwitchTextBinding.getRoot(); } @Override public void onClick(View v) { } public void update(List> list) { if (list == null || list.size() == 0) { return; } stop(); texts = list; marker = 0; Pair firstTxt = texts.get(0); setText(firstTxt.first, firstTxt.second); start(); } public void start() { stop(); handler.postDelayed(task, delayTime); } public void stop() { handler.removeCallbacks(task); } public int getMarker() { return marker; } public InformationSwitchBanner setTexts(List> texts) { this.texts = texts; return this; } public void setDelayTime(int delayTime) { this.delayTime = delayTime; } private void createAnimation() { AlphaAnimation alphaAnimation; TranslateAnimation translateAnimation; int h = viewSwitcher.getHeight(); if (h <= 0) { viewSwitcher.measure(0, 0); h = viewSwitcher.getMeasuredHeight(); } InAnimationSet = new AnimationSet(true); OutAnimationSet = new AnimationSet(true); alphaAnimation = new AlphaAnimation(0, 1); translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, h, Animation.ABSOLUTE, 0); InAnimationSet.addAnimation(alphaAnimation); InAnimationSet.addAnimation(translateAnimation); InAnimationSet.setDuration(DURATION); alphaAnimation = new AlphaAnimation(1, 0); translateAnimation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0, Animation.ABSOLUTE, -h); OutAnimationSet.addAnimation(alphaAnimation); OutAnimationSet.addAnimation(translateAnimation); OutAnimationSet.setDuration(DURATION); } private void nextView() { if (onSwitchListener != null) { onSwitchListener.preSwitch(); } marker = ++marker % texts.size(); Pair nextTxt = texts.get(marker); setText(nextTxt.first, nextTxt.second); if (onSwitchListener != null) { onSwitchListener.onSwitch(); } } public void setOnSwitchListener(OnSwitchListener listener) { this.onSwitchListener = listener; } public interface OnSwitchListener { void preSwitch(); void onSwitch(); } private final Runnable task = new Runnable() { @Override public void run() { nextView(); handler.postDelayed(task, delayTime * 2L); } }; }