本文根据该博主http://blog.csdn.net/cctcc/article/details/50127039,而来,很大部分是该博主的,自己一点的需要不一样,做了一点修改。
主要是在viewpager在显示各个图片时,底部加文字说明
网络加载图片,采用universalimageloader;
相关的布局fragment_image_pager.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="10dp" > <android.support.v4.view.ViewPager android:id="@+id/imagePager" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- 下面显示的圆点 --> <LinearLayout android:id="@+id/dotsImg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:gravity="center_horizontal" android:orientation="horizontal" android:layout_marginBottom="8dp"> </LinearLayout> </FrameLayout>item_image_viewpager.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="1dip" > <ImageView android:id="@+id/image" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:adjustViewBounds="true" android:contentDescription="@string/action_settings" android:layout_marginBottom="35dp" /> <ProgressBar android:id="@+id/loading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone" /> <TextView android:id="@+id/voice_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:text="dfffffffffff" android:textColor="@color/white" android:layout_marginBottom="45dp"/> <TextView android:id="@+id/voice_place" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:layout_marginBottom="25dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:text="vvvvvvvvvvvvvvvv" android:textColor="@color/white" /> </FrameLayout>
public class ImagePagerActivityFragment extends Fragment { ViewPager pager; LinearLayout dotimgs; private ImageView dotView; private ImageView[] dotsViews; private String[] IMAGE_TIME=null; private String[] IMAGE_LOCATION=null; private String[] IMAGE_URLS = null; private int pos=0; ImageAdapter imageAdapter; public ImagePagerActivityFragment() { } public void setPhotos(String[] photos){ IMAGE_URLS=photos; } public void setPhotosTime(String[] time){ IMAGE_TIME=time; } public void setPhotosLocation(String[] location){ IMAGE_LOCATION=location; } public void setPosition(int i){ pos=i; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_image_pager, container, false); pager = (ViewPager) rootView.findViewById(R.id.imagePager); dotimgs=(LinearLayout)rootView.findViewById(R.id.dotsImg); imageAdapter=new ImageAdapter(getActivity(), IMAGE_URLS,IMAGE_TIME,IMAGE_LOCATION); pager.setAdapter(imageAdapter); pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { // TODO Auto-generated method stub for (int i = 0; i < dotsViews.length; i++) { dotsViews[position].setBackgroundResource(R.drawable.ic_about); if (position != i) { dotsViews[i].setBackgroundResource(R.drawable.ic_pass_un_select); } } } @Override public void onPageScrollStateChanged(int state) { } }); //pager.setCurrentItem(pos); pager.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { pager.getViewTreeObserver().removeOnPreDrawListener(this); if (pos==0) { } else { pager.setCurrentItem(pos); } return true; } }); dotsViews = new ImageView[IMAGE_URLS.length]; for(int i =0;i<IMAGE_URLS.length;i++){ dotView = new ImageView(getActivity()); dotView.setLayoutParams(new LinearLayout.LayoutParams(20,20)); dotView.setPadding(20, 0, 20, 0); dotsViews[i] = dotView; //默认第一张图显示为选中状态 if (i == 0) { dotsViews[i].setBackgroundResource(R.drawable.ic_about); } else { dotsViews[i].setBackgroundResource(R.drawable.ic_pass_un_select); } dotimgs.addView(dotsViews[i]); } return rootView; } private static class ImageAdapter extends PagerAdapter { private LayoutInflater inflater; private DisplayImageOptions options; private String[] photours; private String[] photourTime; private String[] photourLoaction; ImageAdapter(Context context,String[] pts,String[] time,String[] location) { inflater = LayoutInflater.from(context); photours=pts; photourTime=time; photourLoaction=location; options = new DisplayImageOptions.Builder() .showImageForEmptyUri(R.drawable.plugin_camera_no_pictures) .showImageOnFail(R.drawable.ic_picture_loadfailed) .resetViewBeforeLoading(true) .cacheOnDisc(true) .imageScaleType(ImageScaleType.EXACTLY) .bitmapConfig(Bitmap.Config.RGB_565) .considerExifParams(true) .displayer(new FadeInBitmapDisplayer(300)) .build(); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public int getCount() { return photours.length; } @Override public Object instantiateItem(ViewGroup view, int position) { View imageLayout = inflater.inflate(R.layout.item_image_viewpager, view, false); assert imageLayout != null; final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image); TextView tvTime = (TextView)imageLayout.findViewById(R.id.voice_time); TextView tvAddress = (TextView)imageLayout.findViewById(R.id.voice_place); if(photourTime[position]!=null){ tvTime.setVisibility(View.VISIBLE); tvTime.setText("时间:"+photourTime[position]); }else{ tvTime.setVisibility(View.GONE); } if(photourLoaction[position]!=null){ tvAddress.setVisibility(View.VISIBLE); tvAddress.setText("地址:"+photourLoaction[position]); }else{ tvAddress.setVisibility(View.GONE); } final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(R.id.loading); ImageLoader.getInstance().displayImage(photours[position], imageView, options, new SimpleImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { spinner.setVisibility(View.VISIBLE); } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { String message = null; switch (failReason.getType()) { case IO_ERROR: message = "Input/Output error"; break; case DECODING_ERROR: message = "Image can't be decoded"; break; case NETWORK_DENIED: message = "Downloads are denied"; break; case OUT_OF_MEMORY: message = "Out Of Memory error"; break; case UNKNOWN: message = "Unknown error"; break; } // Toast.makeText(view.getContext(), message, Toast.LENGTH_SHORT).show(); imageView.setImageResource(R.drawable.ic_picture_loadfailed); spinner.setVisibility(View.GONE); } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { spinner.setVisibility(View.GONE); } }); //view.addView(imageLayout, 0); view.addView(imageLayout); return imageLayout; } @Override public boolean isViewFromObject(View view, Object object) { return view.equals(object); } @Override public void restoreState(Parcelable state, ClassLoader loader) { } @Override public Parcelable saveState() { return null; } } }
在使用的地方只要
ImagePagerActivityFragment fragment=new ImagePagerActivityFragment(); if(pturls!=null) {//<span style="font-family: Arial;">pturls是由图片的url用“ ,”拼接而成,其他变量也是,自己根据需求自己整</span> pturls=pturls.replaceAll("_small",""); String[] ptarrs=pturls.split(","); fragment.setPhotos(ptarrs); fragment.setPosition(pos); } if(ptime!=null){ String[] ptimes=ptime.split(","); fragment.setPhotosTime(ptimes); } if(plocation!=null){ String[] plocations=plocation.split(","); fragment.setPhotosLocation(plocations); } getSupportFragmentManager().beginTransaction().add(R.id.iContainer, fragment).commit();