自定义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 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 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



    

(2)、come_out.xml



    

2、布局资源

(1)、flow_layout_viewflip.xml



(2)、flow_layout_textview.xml


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 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("你好");
        arrayList.add("你好");
        TextBanner viewById = this.findViewById(R.id.text_banner);
        viewById.setData(arrayList);
        viewById.startAnimation();
    }
}

具体效果

自定义TextBanner 自动滚动_第1张图片

没有自测哦  有bug自己解决

溜了~~~~~

你可能感兴趣的:(笔记,android)