public class ScrollTextView extends LinearLayout {
private ImageView mBannerTV1;
private TextView mBannerTV2;
private Handler handler;
private boolean isShow = false;
private int startY1, endY1, startY2, endY2;
private Runnable runnable;
private List<EarningBean.DatasBean.CustProfitListBean> list;
private int position = 0;
private int offsetY = 100;
private boolean hasPostRunnable = false;
public ScrollTextView(Context context) {
this(context, null);
}
public ScrollTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ScrollTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
View view = LayoutInflater.from(context).inflate(R.layout.widget_scroll_text_layout, this);
mBannerTV1 = view.findViewById(R.id.tv_banner1);
mBannerTV2 = view.findViewById(R.id.tv_banner2);
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
isShow = !isShow;
if (position == list.size() - 1) {
position = 0;
}
if (isShow) {
EarningBean.DatasBean.CustProfitListBean custProfitListBean = list.get(position);
Glide.with(context).load(custProfitListBean.getCust_avatar()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(mBannerTV1);
mBannerTV2.setText(custProfitListBean.getContent());
} else {
EarningBean.DatasBean.CustProfitListBean custProfitListBean = list.get(position);
Glide.with(context).load(custProfitListBean.getCust_avatar()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(mBannerTV1);
mBannerTV2.setText(custProfitListBean.getContent());
}
ObjectAnimator.ofFloat(mBannerTV1, "translationX", startY1, endY1).setDuration(300).start();
ObjectAnimator.ofFloat(mBannerTV2, "translationY", startY2, endY2).setDuration(300).start();
position++;
handler.postDelayed(runnable, 3000);
}
};
}
public List<EarningBean.DatasBean.CustProfitListBean> getList() {
return list;
}
public void setList(List<EarningBean.DatasBean.CustProfitListBean> list) {
this.list = list;
//处理最后一条数据切换到第一条数据 太快的问题
if (list.size() > 1) {
list.add(list.get(position));
}
}
public void startScroll() {
// mBannerTV1.setText(list.get(0));
if (list.size() > 1) {
/* EarningBean.DatasBean.CustProfitListBean custProfitListBean = list.get(position);
Glide.with(getContext()).load(custProfitListBean.getCust_avatar()) .apply(RequestOptions.bitmapTransform(new CircleCrop())).into(mBannerTV1);
mBannerTV2.setText(custProfitListBean.getContent());*/
if (!hasPostRunnable) {
hasPostRunnable = true;
//处理第一次进入 第一条数据切换第二条 太快的问题
handler.postDelayed(runnable, 1);
}
} else {
//只有一条数据不进行滚动 hasPostRunnable = false;
// mBannerTV1.setText(list.get(0));
}
}
public void stopScroll() {
handler.removeCallbacks(runnable);
hasPostRunnable = false;
}
}
引入
<com.aizhiyi.widget.ScrollTextView
android:id="@+id/gundong"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/dp_10"
android:orientation="horizontal" />
activity使用
ProgressDialogUtils.dismissDialog();
if (earningBean.getCode() == 200 && earningBean.getDatas().getCust_profit_list().size() > 0) {
gundong.setVisibility(View.VISIBLE);
List<EarningBean.DatasBean.CustProfitListBean> cust_profit_list = ;
cust_profit_list.clear();
cust_profit_list.addAll(earningBean.getDatas().getCust_profit_list());
if (cust_profit_list.size() != 0) {
gundong.setList(cust_profit_list);
gundong.startScroll();
vf.setVisibility(View.VISIBLE);}