private void initUI(Context context) { imageViewsList = new ArrayList<ImageView>(); dotViewsList = new ArrayList<ImageView>(); imageUris = new ArrayList<String>(); imageLoaderTools = ImageLoaderTools.getInstance(context.getApplicationContext()); LayoutInflater.from(context).inflate(R.layout.layout_slideshow, this, true); mLinearLayout = (LinearLayout) findViewById(R.id.linearlayout); mViewPager = (ViewPager) findViewById(R.id.viewPager); // mFlashViewListener必须实例化 titleTv = (TextView) findViewById(R.id.tv_title); }
public void setImageUris(List<String> imageuris, List<String> titles) { if (imageUris.size() > 0) { imageUris.clear(); imageViewsList.clear(); dotViewsList.clear(); mLinearLayout.removeAllViews(); } if (imageuris.size() <= 0) // 如果得到的图片张数为0,则增加一张默认的图片 { imageUris.add("drawable://" + R.drawable.defaultflashview); } else { if (imageuris.size() == 2) { isTwo = true; imageUris.addAll(imageuris); imageUris.addAll(imageuris); } else { isTwo = false; imageUris.addAll(imageuris); } // // for (int i = 0; i < imageuris.size(); i++) // { // // imageUris.add(imageuris.get(i)); // if(i==1) // { // imageUris.add(imageuris.get(0)); // } // } } LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.setMargins(5, 0, 0, 0); for (int i = 0; i < imageUris.size(); i++) { ImageView imageView = new ImageView(getContext()); imageView.setScaleType(ScaleType.FIT_XY); // X和Y方向都填满 imageLoaderTools.displayImage(imageUris.get(i), imageView); imageViewsList.add(imageView); ImageView viewDot = new ImageView(getContext()); if (i == 0) { viewDot.setBackgroundResource(R.drawable.dot_white); } else { viewDot.setBackgroundResource(R.drawable.dot_light); } viewDot.setLayoutParams(lp); if (isTwo) // 为两张图片时加入的判断 { if (i > 1) { } else { dotViewsList.add(viewDot); mLinearLayout.addView(viewDot); } } else { dotViewsList.add(viewDot); mLinearLayout.addView(viewDot); } } mViewPager.setFocusable(true); mViewPager.setAdapter(new MyPagerAdapter()); mViewPager.setOnPageChangeListener(new MyPageChangeListener()); setEffect(effect); if (imageUris.size() <= 1) // 图片小于等于1张时,不轮播 { } else { // 利用反射修改自动轮播的动画持续时间 try { Field field = ViewPager.class.getDeclaredField("mScroller"); field.setAccessible(true); FixedSpeedScroller scroller = new FixedSpeedScroller(mViewPager.getContext(), new AccelerateInterpolator()); field.set(mViewPager, scroller); scroller.setmDuration(1000); mViewPager.setCurrentItem(100 * imageViewsList.size()); mhandler.sendEmptyMessageDelayed(ImageHandler.MSG_UPDATE_IMAGE, ImageHandler.MSG_DELAY); } catch (Exception e) { } } // 当刚刚的到标题信息的时候,将TextView控件上显示的文字,先替换成第一个标题 titleTv.setText(titles.get(0)); this.titles = titles; }