* 图片滚动类
*
* @author SideDoctor
*
*/
public class MyViewPagerScroll extends ViewPager {
Activity mActivity; // 上下文
List<View> mListViews; // 图片组
int mScrollTime = 0;
Timer timer;
int oldIndex = 0;
int curIndex = 0;
boolean listis2 = false;
/*
* 每隔固定时间切换广告栏图片
*/
@SuppressLint("HandlerLeak") private final Handler viewHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
MyViewPagerScroll.this.setCurrentItem(MyViewPagerScroll.this.getCurrentItem() + 1);
super.handleMessage(msg);
}
};
public MyViewPagerScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 开始广告滚动
*
* @param mainActivity
* 显示广告的主界面
* @param imgList
* 图片列表, 不能为null ,最少一张
* @param scrollTime
* 滚动间隔 ,0为不滚动
* @param ovalLayout
* 圆点容器,可为空,LinearLayout类型
* @param ovalLayoutId
* ovalLayout为空时 写0, 圆点layout XMl
* @param ovalLayoutItemId
* ovalLayout为空时 写0,圆点layout XMl 圆点XMl下View ID
* @param focusedId
* ovalLayout为空时 写0, 圆点layout XMl 选中时的动画
* @param normalId
* ovalLayout为空时 写0, 圆点layout XMl 正常时背景
*/
public void start(Activity mainActivity, List<View> imgList, int scrollTime,
LinearLayout ovalLayout, int ovalLayoutId, int ovalLayoutItemId, int focusedId,
int normalId, boolean listis2) {
this.listis2 = listis2;
mActivity = mainActivity;
mListViews = imgList;
mScrollTime = scrollTime;
ovalLayout.removeAllViewsInLayout();
if(imgList.size()>1){
// 设置圆点
setOvalLayout(ovalLayout, ovalLayoutId, ovalLayoutItemId, focusedId, normalId);
}
this.setAdapter(new MyPagerAdapter());// 设置适配器
if (scrollTime != 0 && mListViews.size() > 1) {
// 设置滑动动画时间 ,如果用默认动画时间可不用 ,反射技术实现
// new FixedSpeedScroller(mActivity).setDuration(this, 700);
startTimer();
// 触摸时停止滚动
this.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
MyViewPagerScroll.this.setCurrentItem(MyViewPagerScroll.this.getCurrentItem());
startTimer();
} else {
stopTimer();
}
return false;
}
});
}
}
// 设置圆点
private void setOvalLayout(final LinearLayout ovalLayout, int ovalLayoutId,
final int ovalLayoutItemId, final int focusedId, final int normalId) {
if (ovalLayout != null) {
LayoutInflater inflater = LayoutInflater.from(mActivity);
if (listis2) {
for (int i = 0; i < mListViews.size() - 2; i++) {
ovalLayout.addView(inflater.inflate(ovalLayoutId, null));
}
// 选中第一个
ovalLayout.getChildAt(0).findViewById(ovalLayoutItemId).setBackgroundResource(
focusedId);
this.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int i) {
curIndex = i % mListViews.size();
switch (curIndex) {
case 2:
curIndex = 0;
break;
case 3:
curIndex = 1;
default:
break;
}
// 取消圆点选中
ovalLayout.getChildAt(oldIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
normalId);
// 圆点选中
ovalLayout.getChildAt(curIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
focusedId);
oldIndex = curIndex;
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
});
} else {
for (int i = 0; i < mListViews.size(); i++) {
ovalLayout.addView(inflater.inflate(ovalLayoutId, null));
}
// 选中第一个
ovalLayout.getChildAt(0).findViewById(ovalLayoutItemId).setBackgroundResource(
focusedId);
this.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int i) {
curIndex = i % mListViews.size();
// 取消圆点选中
ovalLayout.getChildAt(oldIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
normalId);
// 圆点选中
ovalLayout.getChildAt(curIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
focusedId);
oldIndex = curIndex;
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
});
}
}
}
/**
* 取得当明选中下标
*
* @return
*/
public int getCurIndex() {
return curIndex;
}
/**
* 停止滚动
*/
public void stopTimer() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
/**
* 开始滚动
*/
public void startTimer() {
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
mActivity.runOnUiThread(new Runnable() {
public void run() {
viewHandler.sendEmptyMessage(1);
}
});
}
}, mScrollTime, mScrollTime);
}
}
// 适配器 //循环设置
private class MyPagerAdapter extends PagerAdapter {
public void finishUpdate(View arg0) {
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public int getCount() {
if (mListViews.size() == 1) {// 一张图片时不用流动
return mListViews.size();
}
return Integer.MAX_VALUE;
}
public Object instantiateItem(View v, int i) {
((ViewPager) v).removeView(mListViews.get(i % mListViews.size()));
((ViewPager) v).addView(mListViews.get(i % mListViews.size()), 0);
return mListViews.get(i % mListViews.size());
}
@Override
public void destroyItem(View container, int position, Object object) {
}
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == (arg1);
}
public void restoreState(Parcelable arg0, ClassLoader arg1) {
}
public Parcelable saveState() {
return null;
}
public void startUpdate(View arg0) {
}
}
}
/**
* 加载滚动图片
*/
private void initLoadImg(final List<Picture> imgList){
int screenWidth=this.getActivity().getResources().getDisplayMetrics().widthPixels;
int height=(int) (screenWidth*9.0/16);
listViews = new ArrayList<View>();
if(MyApplication.getUser()==null){
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ScaleType.FIT_XY);
ViewGroup.LayoutParams p =new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
imageView.setLayoutParams(p);
imageView.setBackgroundResource(R.drawable.img_url_for_user_login);
listViews.add(imageView);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),LoginActivity.class));
}
});
}
for (int i = 0; i < imgList.size(); i++) {
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ScaleType.FIT_XY);
ViewGroup.LayoutParams p =new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
imageView.setLayoutParams(p);
final String imagUrl=imgList.get(i).getUrl();
ImageLoader.getInstance().displayImage(Config.IMAGE_PALY_URL+imgList.get(i).getPicAddress(), imageView);
listViews.add(imageView);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
// 开始滚动圆点
if(listViews.size()>0){
fragment_index_img_player.start(getActivity(), listViews, 3000,
fragment_index_img_player_circle, R.layout.fragmet_index_img_paly_circle,
R.id.ad_item_v, R.drawable.circle_hecked,R.drawable.circle_unhecked, false);
}
}
<FrameLayout
android:id="@+id/fragment_index_linear_head_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/fragment_index_relative_img_paly"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.view.MyViewPagerScroll
android:id="@+id/fragment_index_img_player"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/fragment_index_img_player_circle"
android:layout_width="match_parent"
android:layout_height="20dip"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</FrameLayout>