本文主要是介绍ImagePagerActivity简单的仿微信图片浏览,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
自定义一个Activity,封装了ViewPager和LinearLayout,实现简单的图片浏览器,和上个自定义的九宫图控件一样,使用非常简单,只需要传入图片的url数组就可以了。当然图片加载使用的Glide,需要导入一个Glide。
dependencies {compile 'com.github.bumptech.glide:glide:3.7.0'
}
直接把Activity源码贴出,很简单的逻辑。
public class ImagePagerActivity extends Activity {public static final String INTENT_IMGURLS = "imgurls";public static final String INTENT_POSITION = "position";public static final String INTENT_IMAGESIZE = "imagesize";private List<View> guideViewList = new ArrayList<View>();private LinearLayout guideGroup;public ImageSize imageSize;private int startPos;private ArrayList<String> imgUrls;public static void startImagePagerActivity(Context context, List<String> imgUrls, int position, ImageSizeimageSize) {Intent intent = new Intent(context, ImagePagerActivity.class);intent.putStringArrayListExtra(INTENT_IMGURLS, new ArrayList<String>(imgUrls));intent.putExtra(INTENT_POSITION, position);intent.putExtra(INTENT_IMAGESIZE, imageSize);context.startActivity(intent);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_imagepager);ViewPager viewPager = (ViewPager) findViewById(R.id.pager);guideGroup = (LinearLayout) findViewById(R.id.guideGroup);getIntentData();ImageAdapter mAdapter = new ImageAdapter(this);mAdapter.setDatas(imgUrls);mAdapter.setImageSize(imageSize);viewPager.setAdapter(mAdapter);viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {@Overridepublic void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}@Overridepublic void onPageSelected(int position) {for (int i = 0; i < guideViewList.size(); i++) {guideViewList.get(i).setSelected(i == position ? true : false);}}@Overridepublic void onPageScrollStateChanged(int state) {}});viewPager.setCurrentItem(startPos);addGuideView(guideGroup, startPos, imgUrls);}private void getIntentData() {startPos = getIntent().getIntExtra(INTENT_POSITION, 0);imgUrls = getIntent().getStringArrayListExtra(INTENT_IMGURLS);imageSize = (ImageSize) getIntent().getSerializableExtra(INTENT_IMAGESIZE);}private void addGuideView(LinearLayout guideGroup, int startPos, ArrayList<String> imgUrls) {if (imgUrls != null && imgUrls.size() > 0) {guideViewList.clear();for (int i = 0; i < imgUrls.size(); i++) {View view = new View(this);view.setBackgroundResource(R.drawable.selector_guide_bg);view.setSelected(i == startPos ? true : false);LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(getResources().getDimensionPixelSize(R.dimen.gudieview_width), getResources().getDimensionPixelSize(R.dimen.gudieview_heigh));layoutParams.setMargins(10, 0, 0, 0);guideGroup.addView(view, layoutParams);guideViewList.add(view);}}}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {try {return super.dispatchTouchEvent(ev);} catch (Exception e) {e.printStackTrace();}return false;}private static class ImageAdapter extends PagerAdapter {private List<String> datas = new ArrayList<String>();private LayoutInflater inflater;private Context context;private ImageSize imageSize;private ImageView smallImageView = null;public void setDatas(List<String> datas) {if (datas != null)this.datas = datas;}public void setImageSize(ImageSize imageSize) {this.imageSize = imageSize;}public ImageAdapter(Context context) {this.context = context;this.inflater = LayoutInflater.from(context);}@Overridepublic int getCount() {if (datas == null)return 0;return datas.size();}@Overridepublic Object instantiateItem(ViewGroup container, final int position) {View view = inflater.inflate(R.layout.item_pager_image, container, false);if (view != null) {final ImageView imageView = (ImageView) view.findViewById(R.id.image);if (imageSize != null) {//预览imageViewsmallImageView = new ImageView(context);FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(imageSize.getWidth(),imageSize.getHeight());layoutParams.gravity = Gravity.CENTER;smallImageView.setLayoutParams(layoutParams);smallImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);((FrameLayout) view).addView(smallImageView);}//loadingfinal ProgressBar loading = new ProgressBar(context);FrameLayout.LayoutParams loadingLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);loadingLayoutParams.gravity = Gravity.CENTER;loading.setLayoutParams(loadingLayoutParams);((FrameLayout) view).addView(loading);final String imgurl = datas.get(position);Glide.with(context).load(imgurl).diskCacheStrategy(DiskCacheStrategy.ALL)//缓存多个尺寸.thumbnail(0.1f)//先显示缩略图 缩略图为原图的1/10.error(R.drawable.ic_launcher).into(new GlideDrawableImageViewTarget(imageView) {@Overridepublic void onLoadStarted(Drawable placeholder) {super.onLoadStarted(placeholder);/* if(smallImageView!=null){smallImageView.setVisibility(View.VISIBLE);Glide.with(context).load(imgurl).into(smallImageView);}*/loading.setVisibility(View.VISIBLE);}@Overridepublic void onLoadFailed(Exception e, Drawable errorDrawable) {super.onLoadFailed(e, errorDrawable);/*if(smallImageView!=null){smallImageView.setVisibility(View.GONE);}*/loading.setVisibility(View.GONE);}@Overridepublic void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable>animation) {super.onResourceReady(resource, animation);loading.setVisibility(View.GONE);/*if(smallImageView!=null){smallImageView.setVisibility(View.GONE);}*/}});container.addView(view, 0);}return view;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView((View) object);}@Overridepublic boolean isViewFromObject(View view, Object object) {return view.equals(object);}@Overridepublic void restoreState(Parcelable state, ClassLoader loader) {}@Overridepublic Parcelable saveState() {return null;}}@Overrideprotected void onDestroy() {guideViewList.clear();super.onDestroy();}public static class ImageSize implements Serializable {private int width;private int height;public ImageSize(int width, int height) {this.width = width;this.height = height;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}public int getWidth() {return width;}public void setWidth(int width) {this.width = width;}}
}
同样也只做使用示例,详细逻辑可以自己理一理,不难。先有数据源,
List<String>tableImgs= new ArrayList<>();tableImgs.add("http://p3.so.qhmsg.com/bdr/_240_/t01530591c4a0e6c033.jpg");tableImgs.add("http://p2.so.qhmsg.com/bdr/_240_/t01ab43c649e752002c.jpg");tableImgs.add("http://p2.so.qhmsg.com/bdr/_240_/t01dce1c2b41d8e272c.jpg");tableImgs.add("http://p1.so.qhmsg.com/bdr/_240_/t01974c03c076598946.jpg");tableImgs.add("http://p0.so.qhmsg.com/bdr/_240_/t0123c14f8d0d2be925.jpg");tableImgs.add("http://p1.so.qhmsg.com/bdr/_240_/t01938dc3846fae8de1.jpg");
启动Activity
ImagePagerActivity.startImagePagerActivity(MyPatientDetailActivity.this, tableImgs, position, newImagePagerActivity.ImageSize(view.getMeasuredWidth(), view.getMeasuredHeight()));
这里的position是你跳转过去显示的数据源第几张图
如果想要设置点击事件可以到ImagePagerActivity中去添加,这里就不单独提出来了。
具体使用参照:https://github.com/SingleShu/ImageActivityWX
这篇关于ImagePagerActivity简单的仿微信图片浏览的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!