Android自定义TextBanner实现自动滚动

所属分类: 软件编程 / Android 阅读数: 115
收藏 0 赞 0 分享

本文实例为大家分享了Android自定义TextBanner实现自动滚动的具体代码,供大家参考,具体内容如下

1、TextBanner

package com.example.myapplication.customview;
 
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ViewFlipper;
 
import com.example.myapplication.R;
 
import java.util.ArrayList;
import java.util.List;
 
public class TextBanner extends ViewGroup {
 private List<String> mData = new ArrayList<>();
 private ViewFlipper viewFlipper;
 private int parentWidthSpec;
 
 public TextBanner(Context context) {
 super(context);
 }
 
 public TextBanner(Context context, AttributeSet attrs) {
 super(context, attrs);
 }
 
 public TextBanner(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 }
 
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
 
 
 int top = 0;
 int bottom = getChildAt(0).getMeasuredHeight();
 
 
 int left = 0;
 for (int i = 0; i < getChildCount(); i++) {
  View view = getChildAt(i);
  left = (parentWidthSpec - view.getMeasuredWidth()) / 2;
  view.layout(left, top, left + view.getMeasuredWidth(), bottom);
  top += view.getMeasuredHeight();
  bottom = top + view.getMeasuredHeight();
 
 }
 Log.d("tzg", "bottom: " + bottom);
 Log.d("tzg", "top: " + top);
 
 
 }
 
 
 public void setData(List<String> data) {
 mData.clear();
 if (data.isEmpty()) {
  return;
 }
 this.mData = data;
 
 setTextList();
 }
 
 private void setTextList() {
 viewFlipper = (ViewFlipper) LayoutInflater.from(getContext()).inflate(R.layout.flow_layout_viewflip, this, false);
 for (String mDatum : mData) {
 
  TextView view = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.flow_layout_textview, this, false);
  view.setText(mDatum);
  viewFlipper.addView(view);
 
 }
 viewFlipper.setInAnimation(getContext(), R.anim.come_in);
 viewFlipper.setOutAnimation(getContext(), R.anim.come_out);
 viewFlipper.setFlipInterval(2000);
 addView(viewFlipper);
 }
 
 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
 parentWidthSpec = MeasureSpec.getSize(widthMeasureSpec);
 int parentHeightSpec = MeasureSpec.getSize(heightMeasureSpec);
 
 
 int childWidth = MeasureSpec.makeMeasureSpec(parentWidthSpec, MeasureSpec.AT_MOST);
 int childHeight = MeasureSpec.makeMeasureSpec(parentHeightSpec, MeasureSpec.AT_MOST);
 
 int totalHeight = getChildAt(0).getMeasuredHeight();
 
 for (int i = 0; i < getChildCount(); i++) {
  View view = getChildAt(i);
  measureChild(view, childWidth, childHeight);
 }
 Log.d("tzg", "totalCount: " + totalHeight);
 setMeasuredDimension(parentWidthSpec, totalHeight);
 
 }
 
 
 public void startAnimation() {
 // 1、设置幻灯片的形式滚动
 // viewFlipper.startFlipping();
 
 // 2、设置自动翻页滚动
 viewFlipper.setAutoStart(true);
 viewFlipper.isAutoStart();
 }
}

用到的资源 

1、动画资源

(1)、come_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
 
 <translate
 android:duration="1000"
 android:fromYDelta="100%p"
 android:toYDelta="0"/>
 
</set>

(2)、come_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
 
 <translate
 android:duration="1000"
 android:fromYDelta="0"
 android:toYDelta="-100%p"/>
 
</set>

2、布局资源

(1)、flow_layout_viewflip.xml

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center">
</ViewFlipper>

(2)、flow_layout_textview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:padding="5dp"
 android:text="demo"
 android:textColor="#FF00FF" />

3、在mainActivity中的使用

package com.example.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
 
import com.example.myapplication.customview.FlowLayout;
import com.example.myapplication.customview.TextBanner;
 
import java.util.ArrayList;
 
public class MainActivity extends AppCompatActivity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 ArrayList<String> arrayList = new ArrayList<>();
 arrayList.add("111111111");
 arrayList.add("222222222222444444444444");
 arrayList.add("你好5");
 arrayList.add("你好633");
 arrayList.add("你好a7好a7");
 arrayList.add("你好7889");
 arrayList.add("你好2323423423 ");
 arrayList.add("你好sdfsfada你好sdfsfada ");
 arrayList.add("你好34345");
 arrayList.add("pppppppp");
 arrayList.add("你好");
 arrayList.add("你好你好");
 arrayList.add("电视");
 arrayList.add("冰箱冰箱冰箱冰箱冰箱冰箱冰箱冰箱冰箱冰箱");
 arrayList.add("woaoni");
 arrayList.add("你好");
 arrayList.add("你好");
 TextBanner viewById = this.findViewById(R.id.text_banner);
 viewById.setData(arrayList);
 viewById.startAnimation();
 }
}

具体效果

没有自测哦  有bug自己解决

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多