Skip to content

Commit

Permalink
[FIX] 使用Builder模式构造ImageViewer
Browse files Browse the repository at this point in the history
  • Loading branch information
sunyichao committed Aug 19, 2017
1 parent fba2c19 commit b4a7a9c
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 91 deletions.
88 changes: 44 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
loadListener.onError(position);
return false;
}

@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> 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<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
loadListener.onError(position);
return false;
}

@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
loadListener.onSuccess(position);
return false;
}
})
.into(imageView);
}
})
.setTransformerType(ImageViewer.TYPE_CUBEOUT_TRANSFORMER)
.build()
.show(getSupportFragmentManager(), "ImageViewer");
```

Expand Down Expand Up @@ -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");
```
30 changes: 16 additions & 14 deletions app/src/main/java/cn/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -95,7 +92,12 @@ public boolean onResourceReady(GlideDrawable resource, String model, Target<Glid
})
.into(imageView);
}
});
},
viewpagerCommonAdapter)
.setIndex(2)
.setPaths(paths)
.setTransformerType(ImageViewer.TYPE_CUBEOUT_TRANSFORMER)
.build();

viewpagerCommonAdapter.setOnImageSingleClickListener(new OnImageSingleClickListener() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import cn.imageviewer.helper.OnImageLongClickListener;
import cn.imageviewer.helper.OnImageSingleClickListener;
import cn.imageviewer.helper.ImageLoader;
import cn.imageviewer.helper.OnLoadListener;

/**
* Created by cloudist on 2017/6/28.
Expand Down
65 changes: 32 additions & 33 deletions imageviewer/src/main/java/cn/imageviewer/view/ImageViewer.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package cn.imageviewer.view;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -41,19 +41,12 @@ public class ImageViewer extends DialogFragment {

SwipeableFrameLayout layout;
FixedViewPager viewpager;
ViewpagerAdapter adapter;

int index = 0;
int transformerType = TYPE_DEFAULT_TRANSFORMER;
List<String> 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) {
Expand Down Expand Up @@ -130,35 +123,41 @@ private void setupViewPager(ViewPager viewPager) {
}
}

public ImageViewer setPaths(List<String> paths) {
this.paths = paths;
return ImageViewer.this;
}
public static class Builder {
int index = 0;
int transformerType = TYPE_DEFAULT_TRANSFORMER;
List<String> paths = new ArrayList<>();
ImageLoader imageLoader;
ViewpagerAdapter adapter;

public <T> ImageViewer setPaths(List<T> objects, ImageTramsform<T> 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<String> 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;
}
}
}

0 comments on commit b4a7a9c

Please sign in to comment.