diff --git a/README.md b/README.md index 8d44dba..2e6df5a 100644 --- a/README.md +++ b/README.md @@ -41,37 +41,36 @@ viewpagerCommonAdapter.setOnImageLongClickListener(new OnImageLongClickListener( } }); -ImageViewer.newInstance() - .setIndex(0) +final ImageViewer imageViewer = new ImageViewer.Builder( + new ImageLoader() { + @Override + public void showImage(final int position, String path, ImageView imageView) { + final OnLoadListener loadListener = this.getOnLoadListener(); + final View view = this.getView(); + loadListener.onStart(position); + Glide.with(OCApplication.getContext()) + .load(path) + .listener(new RequestListener() { + @Override + public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { + loadListener.onError(position); + return false; + } + + @Override + public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { + loadListener.onSuccess(position); + return false; + } + }) + .into(imageView); + } + }, + viewpagerCommonAdapter) + .setIndex(2) .setPaths(paths) - //setTransformerType 可以设置ImageViewer切换图片时的动画 - .setTransformerType(ImageViewer.TYPE_ZOOMOUT_TRANSFORMER) - .setAdapter(viewpagerCommonAdapter) - .setImageLoader(new ImageLoader() { - @Override - public void showImage(final int position, String path, ImageView imageView) { - final OnLoadListener loadListener = this.getOnLoadListener(); - final View view = this.getView(); - - loadListener.onStart(position); - Glide.with(OCApplication.getContext()) - .load(path) - .listener(new RequestListener() { - @Override - public boolean onException(Exception e, String model, Target target, boolean isFirstResource) { - loadListener.onError(position); - return false; - } - - @Override - public boolean onResourceReady(GlideDrawable resource, String model, Target target, boolean isFromMemoryCache, boolean isFirstResource) { - loadListener.onSuccess(position); - return false; - } - }) - .into(imageView); - } - }) + .setTransformerType(ImageViewer.TYPE_CUBEOUT_TRANSFORMER) + .build() .show(getSupportFragmentManager(), "ImageViewer"); ``` @@ -117,18 +116,19 @@ public class CustomViewpagerAdapter extends ViewpagerAdapter { ## Customized ImageViewer init ```Java -ImageViewer.newInstance() - .setIndex(0) - .setPaths(paths) - .setTransformerType(ImageViewer.TYPE_DEPTHPAGE_TRANSFORMER) - .setAdapter(new CustomViewpagerAdapter(MainActivity.this)) - .setImageLoader(new ImageLoader() { - @Override - public void showImage(int position, String path, ImageView imageView) { - Glide.with(OCApplication.getContext()) - .load(path) - .into(imageView); - } - }) - .show(getSupportFragmentManager(), "ImageViewer"); +new ImageViewer.Builder( + new ImageLoader() { + @Override + public void showImage(int position, String path, ImageView imageView) { + Glide.with(OCApplication.getContext()) + .load(path) + .into(imageView); + } + }, + new CustomViewpagerAdapter(MainActivity.this)) + .setIndex(3) + .setPaths(paths) + .setTransformerType(ImageViewer.TYPE_ZOOMOUT_TRANSFORMER) + .build() + .show(getSupportFragmentManager(), "ImageViewer"); ``` diff --git a/app/src/main/java/cn/demo/MainActivity.java b/app/src/main/java/cn/demo/MainActivity.java index 5992cfc..5885f94 100644 --- a/app/src/main/java/cn/demo/MainActivity.java +++ b/app/src/main/java/cn/demo/MainActivity.java @@ -48,31 +48,28 @@ protected void onCreate(Bundle savedInstanceState) { button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - ImageViewer.newInstance() - .setIndex(3) - .setPaths(paths) - .setTransformerType(ImageViewer.TYPE_ZOOMOUT_TRANSFORMER) - .setAdapter(new CustomViewpagerAdapter(MainActivity.this)) - .setImageLoader(new ImageLoader() { + new ImageViewer.Builder( + new ImageLoader() { @Override public void showImage(int position, String path, ImageView imageView) { Glide.with(OCApplication.getContext()) .load(path) .into(imageView); } - }) + }, + new CustomViewpagerAdapter(MainActivity.this)) + .setIndex(3) + .setPaths(paths) + .setTransformerType(ImageViewer.TYPE_ZOOMOUT_TRANSFORMER) + .build() .show(getSupportFragmentManager(), "ImageViewer"); } }); final ViewpagerCommonAdapter viewpagerCommonAdapter = new ViewpagerCommonAdapter(MainActivity.this); - final ImageViewer imageViewer = ImageViewer.newInstance() - .setIndex(2) - .setPaths(paths) - .setTransformerType(ImageViewer.TYPE_CUBEOUT_TRANSFORMER) - .setAdapter(viewpagerCommonAdapter) - .setImageLoader(new ImageLoader() { + final ImageViewer imageViewer = new ImageViewer.Builder( + new ImageLoader() { @Override public void showImage(final int position, String path, ImageView imageView) { final OnLoadListener loadListener = this.getOnLoadListener(); @@ -95,7 +92,12 @@ public boolean onResourceReady(GlideDrawable resource, String model, Target paths = new ArrayList<>(); ImageLoader imageLoader; - - public static ImageViewer newInstance() { - Bundle args = new Bundle(); - ImageViewer fragment = new ImageViewer(); - fragment.setArguments(args); - return fragment; - } + ViewpagerAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { @@ -130,35 +123,41 @@ private void setupViewPager(ViewPager viewPager) { } } - public ImageViewer setPaths(List paths) { - this.paths = paths; - return ImageViewer.this; - } + public static class Builder { + int index = 0; + int transformerType = TYPE_DEFAULT_TRANSFORMER; + List paths = new ArrayList<>(); + ImageLoader imageLoader; + ViewpagerAdapter adapter; - public ImageViewer setPaths(List objects, ImageTramsform imageTramsform) { - for (T t : objects) { - paths.add(imageTramsform.tramsformPaths(t)); + public Builder(ImageLoader imageLoader, ViewpagerAdapter adapter) { + this.adapter = adapter; + this.imageLoader = imageLoader; } - return ImageViewer.this; - } - public ImageViewer setImageLoader(ImageLoader imageLoader) { - this.imageLoader = imageLoader; - return ImageViewer.this; - } + public Builder setIndex(int index) { + this.index = index; + return this; + } - public ImageViewer setIndex(int index) { - this.index = index; - return ImageViewer.this; - } + public Builder setTransformerType(int transformerType) { + this.transformerType = transformerType; + return this; + } - public ImageViewer setAdapter(ViewpagerAdapter adapter) { - this.adapter = adapter; - return ImageViewer.this; - } + public Builder setPaths(List paths) { + this.paths = paths; + return this; + } - public ImageViewer setTransformerType(int transformerType) { - this.transformerType = transformerType; - return ImageViewer.this; + public ImageViewer build() { + ImageViewer imageViewer = new ImageViewer(); + imageViewer.adapter = adapter; + imageViewer.imageLoader = imageLoader; + imageViewer.index = index; + imageViewer.transformerType = transformerType; + imageViewer.paths = paths; + return imageViewer; + } } }