ViewFlipper处理轮播

1.ViewFlipper与ViewPager的区别:

ViewFlipper继承ViewAnimator,切换时可设置动画效果,适合展示静态数据少量数据。

ViewPager继承ViewGroup,官方描述:ViewPager通常和Fragment一起使用,Fragment有自己的生命周期。fragments 和 ViewPager一起使用时,Android为其提供标准的adapter:FragmentPagerAdapter。所以ViewPager适合复杂视图的切换以及适应复杂数据的动态加载

2.ViewFlipper实现文字广告轮播

1.attr.xml


    
    
    
    
    
        
        
        
    

2.自定义MarqueeView

public class MarqueeView extends ViewFlipper {

private Context mContext;

private boolean isSetAnimDuration;//是否设置动画时间
private MarqueeViewItemClickListener mMarqueeViewItemClickListener;

private int textSize = 14;
private int textColor = 0xffffffff;
private int interval = 2000;
private int animDuration = 500;

private List noticeList;

private int gravity = Gravity.CENTER_VERTICAL;
private static final int TEXT_GRAVITY_LEFT = 0, TEXT_GRAVITY_CENTER = 1, TEXT_GRAVITY_RIGHT = 2;

public MarqueeView(Context context) {
    super(context);
}

public MarqueeView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs, 0);
}

private void init(Context context, AttributeSet attrs, int defStyle) {
    this.mContext = context;
    TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeViewStyle, defStyle, 0);
    interval = typeArray.getInteger(R.styleable.MarqueeViewStyle_mvInterval, interval);
    textColor = typeArray.getColor(R.styleable.MarqueeViewStyle_mvTextColor, textColor);
    if (typeArray.hasValue(R.styleable.MarqueeViewStyle_mvTextSize)) {
        textSize = typeArray.getDimensionPixelSize(R.styleable.MarqueeViewStyle_mvTextSize, textSize);
        textSize = (int) DensityUtil.px2sp(context, textSize);
    }
    animDuration = typeArray.getInteger(R.styleable.MarqueeViewStyle_mvAnimDuration, animDuration);

    int gravityType = typeArray.getInteger(R.styleable.MarqueeViewStyle_mvGravity, gravity);
    switch (gravityType) {
        case TEXT_GRAVITY_LEFT:
            gravity = Gravity.CENTER_VERTICAL | Gravity.LEFT;
            break;
        case TEXT_GRAVITY_CENTER:
            gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER;
            break;
        case TEXT_GRAVITY_RIGHT:
            gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER;
            break;
    }
    typeArray.recycle();
    setFlipInterval(interval);
    Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
    if (isSetAnimDuration) animIn.setDuration(animDuration);
    setInAnimation(animIn);

    Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
    if (isSetAnimDuration) animOut.setDuration(animDuration);
    setOutAnimation(animOut);


}

public void startMarqueeWithList(final List notices) {
    if (notices == null || notices.size() == 0) return;
    getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void onGlobalLayout() {
            checkNotices(notices);
            getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });


}

private void checkNotices(List notices) {
    if (noticeList == null) noticeList = new ArrayList();
    for (String notice : notices) {
        int noticeLength = notice.length();
        int dpW = (int) DensityUtil.px2dp(mContext, getWidth());
        int limit = dpW / textSize;
        if (dpW == 0) {
            throw new RuntimeException("Please set MarqueeView width !");
        }
        if (noticeLength <= limit) {
            noticeList.add(notice);
        } else {
            //截取

            //截取的段数
            int index = noticeLength / limit + (noticeLength % limit == 0 ? 0 : 1);
            for (int i = 0; i < index; i++) {
                int startIndex = i * limit;
                int endIndex = (i + 1) * limit > noticeLength ? noticeLength : (i + 1) * limit;
                noticeList.add(notice.substring(startIndex, endIndex));
            }
        }

        start();

    }
}

private void start() {

    removeAllViews();
    for (int i = 0; i < noticeList.size(); i++) {
        TextView textView = createTextView(noticeList.get(i), i);
        addView(textView);
    }

    if (noticeList.size() > 1) {
        startFlipping();
    }


}

private TextView createTextView(String noticeStr, final int position) {
    TextView tv = new TextView(mContext);
    tv.setText(noticeStr);
    tv.setTextColor(textColor);
    tv.setTextSize(textSize);
    tv.setGravity(gravity);
    tv.setTag(position);
    tv.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            mMarqueeViewItemClickListener.marqueeViewItemClick(position);
        }
    });

    return tv;
}

public interface MarqueeViewItemClickListener {
    void marqueeViewItemClick(int position);
}

public void setMarqueeViewItemClickListener(MarqueeViewItemClickListener marqueeViewItemClickListener) {
    mMarqueeViewItemClickListener = marqueeViewItemClickListener;
}

}

3.activity

public class MainActivity extends Activity implements     MarqueeView.MarqueeViewItemClickListener{
private MarqueeView marqueeView;
private List noices;
private String[] str={"田朴珺撩汉往事,世界就这样被走野路子的女人抢走","万科复牌96小时惊魂 宝能是一条可能溃堤的悬河?","万科复牌4日市值蒸发545亿元 宝能喊话愿做战略财务投资人"};

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();



}

private void initView() {
    marqueeView=(MarqueeView)findViewById(R.id.marqueeView);
    marqueeView.setMarqueeViewItemClickListener(this);
    noices= Arrays.asList(str);
    marqueeView.startMarqueeWithList(noices);
}

@Override
public void marqueeViewItemClick(int position) {
    ToastUtil.show(this,position+"",0);
}

}


4.main.xml



    

    

你可能感兴趣的:(ViewFlipper处理轮播)