1.ViewFlipper与ViewPager的区别:
ViewFlipper继承ViewAnimator,切换时可设置动画效果,适合展示静态数据少量数据。
ViewPager继承ViewGroup,官方描述:ViewPager通常和Fragment一起使用,Fragment有自己的生命周期。fragments 和 ViewPager一起使用时,Android为其提供标准的adapter:FragmentPagerAdapter。所以ViewPager适合复杂视图的切换以及适应复杂数据的动态加载
2.ViewFlipper实现文字广告轮播
1.attr.xml
<code>
<declare-styleable name="MarqueeViewStyle">
<attr name="mvInterval" format="integer|reference"/>
<attr name="mvAnimDuration" format="integer"/>
<attr name="mvTextSize" format="dimension|reference"/>
<attr name="mvTextColor" format="color|reference"/>
<attr name="mvGravity" format="enum">
<enum name="left" value="0"/>
<enum name="center" value="1"/>
<enum name="right" value="2"/>
</attr>
</declare-styleable>
</code>
2.自定义MarqueeView
<code>
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<String> 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<String> 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<String> notices) {
if (noticeList == null) noticeList = new ArrayList<String>();
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;
}
}
</code>
3.activity
<code>
public class MainActivity extends Activity implements MarqueeView.MarqueeViewItemClickListener{
private MarqueeView marqueeView;
private List<String> 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);
}
}
</code>
4.main.xml
<code>
<RelativeLayout
android:layout_marginTop="10dp"
android:layout_below="@id/tl"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="30dp"
android:background="@drawable/xml_oval_half_transparent_bg"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<ImageView
android:id="@+id/iv_loudspeaker3"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_centerVertical="true"
android:src="@drawable/ic_red_loudspeaker"/>
<com.mianshi.lofter.widget.MarqueeView
android:id="@+id/marqueeView"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@+id/iv_loudspeaker3"
app:mvAnimDuration="500"
app:mvGravity="left"
app:mvInterval="2000"
app:mvTextColor="@color/red"/>
</RelativeLayout>
</code>