Skip to content

Commit e253c1f

Browse files
committedDec 3, 2016
add initScaleType, fix #20
1 parent 90d5c03 commit e253c1f

20 files changed

+541
-20
lines changed
 

‎.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,10 @@ bintray.properties
66
.DS_Store
77
**/build
88
/captures
9+
10+
.buckd
11+
.okbuck
12+
buck-out
13+
.buckconfig
14+
.buckconfig.local
15+
**/BUCK

‎BigImageViewer/src/main/java/com/github/piasy/biv/view/BigImageView.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import android.Manifest;
2828
import android.content.Context;
29+
import android.content.res.TypedArray;
2930
import android.net.Uri;
3031
import android.provider.MediaStore;
3132
import android.support.annotation.UiThread;
@@ -39,6 +40,7 @@
3940
import com.davemorrissey.labs.subscaleview.ImageSource;
4041
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
4142
import com.github.piasy.biv.BigImageViewer;
43+
import com.github.piasy.biv.R;
4244
import com.github.piasy.biv.indicator.ProgressIndicator;
4345
import com.github.piasy.biv.loader.ImageLoader;
4446
import com.tbruyelle.rxpermissions2.RxPermissions;
@@ -56,6 +58,10 @@
5658
*/
5759

5860
public class BigImageView extends FrameLayout implements ImageLoader.Callback {
61+
public static final int INIT_SCALE_TYPE_CENTER_INSIDE = 1;
62+
public static final int INIT_SCALE_TYPE_CENTER_CROP = 2;
63+
public static final int INIT_SCALE_TYPE_AUTO = 3;
64+
5965
private final SubsamplingScaleImageView mImageView;
6066

6167
private final ImageLoader mImageLoader;
@@ -80,6 +86,7 @@ public void run() {
8086
}
8187
}
8288
};
89+
private DisplayOptimizeListener mDisplayOptimizeListener;
8390

8491
public BigImageView(Context context) {
8592
this(context, null);
@@ -92,13 +99,23 @@ public BigImageView(Context context, AttributeSet attrs) {
9299
public BigImageView(Context context, AttributeSet attrs, int defStyleAttr) {
93100
super(context, attrs, defStyleAttr);
94101

102+
TypedArray array = context.getTheme()
103+
.obtainStyledAttributes(attrs, R.styleable.BigImageView, defStyleAttr, 0);
104+
int initScaleType = array.getInteger(R.styleable.BigImageView_initScaleType,
105+
INIT_SCALE_TYPE_CENTER_INSIDE);
106+
array.recycle();
107+
95108
mImageView = new SubsamplingScaleImageView(context, attrs);
96109
addView(mImageView);
97110
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
98111
ViewGroup.LayoutParams.MATCH_PARENT);
99112
mImageView.setLayoutParams(params);
100113
mImageView.setMinimumTileDpi(160);
101-
mImageView.setOnImageEventListener(new DisplayOptimizeListener(mImageView));
114+
115+
mDisplayOptimizeListener = new DisplayOptimizeListener(mImageView);
116+
mImageView.setOnImageEventListener(mDisplayOptimizeListener);
117+
118+
setInitScaleType(initScaleType);
102119

103120
mImageLoader = BigImageViewer.imageLoader();
104121

@@ -115,6 +132,22 @@ public void setOnLongClickListener(OnLongClickListener listener) {
115132
mImageView.setOnLongClickListener(listener);
116133
}
117134

135+
public void setInitScaleType(int initScaleType) {
136+
switch (initScaleType) {
137+
case INIT_SCALE_TYPE_CENTER_CROP:
138+
mImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP);
139+
break;
140+
case INIT_SCALE_TYPE_AUTO:
141+
mImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);
142+
break;
143+
case INIT_SCALE_TYPE_CENTER_INSIDE:
144+
default:
145+
mImageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_INSIDE);
146+
break;
147+
}
148+
mDisplayOptimizeListener.setInitScaleType(initScaleType);
149+
}
150+
118151
public void setImageSaveCallback(ImageSaveCallback imageSaveCallback) {
119152
mImageSaveCallback = imageSaveCallback;
120153
}
@@ -241,6 +274,7 @@ public void onStart() {
241274
post(new Runnable() {
242275
@Override
243276
public void run() {
277+
// why show thumbnail in onStart? because we may not need download it from internet
244278
if (mThumbnail != Uri.EMPTY) {
245279
mThumbnailView = mImageLoader.showThumbnail(BigImageView.this, mThumbnail);
246280
addView(mThumbnailView);

‎BigImageViewer/src/main/java/com/github/piasy/biv/view/DisplayOptimizeListener.java

+27
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class DisplayOptimizeListener implements SubsamplingScaleImageView.OnImag
3636

3737
private final SubsamplingScaleImageView mImageView;
3838

39+
private int mInitScaleType;
40+
3941
public DisplayOptimizeListener(SubsamplingScaleImageView imageView) {
4042
mImageView = imageView;
4143
}
@@ -75,6 +77,27 @@ public void onReady() {
7577
result += 0.2f;
7678
}
7779

80+
if (mInitScaleType == BigImageView.INIT_SCALE_TYPE_AUTO) {
81+
float maxScale = Math.max((float) viewWidth / imageWidth,
82+
(float) viewHeight / imageHeight);
83+
if (maxScale > 1) {
84+
// image is smaller than screen, it should be zoomed out to its origin size
85+
mImageView.setMinScale(1);
86+
87+
// and it should be zoomed in to fill the screen
88+
float defaultMaxScale = mImageView.getMaxScale();
89+
mImageView.setMaxScale(Math.max(defaultMaxScale, maxScale * 1.2F));
90+
} else {
91+
// image is bigger than screen, it should be zoomed out to fit the screen
92+
float minScale = Math.min((float) viewWidth / imageWidth,
93+
(float) viewHeight / imageHeight);
94+
mImageView.setMinScale(minScale);
95+
// but no need to set max scale
96+
}
97+
// scale to fit screen, and center
98+
mImageView.setScaleAndCenter(maxScale, new PointF(imageWidth / 2, imageHeight / 2));
99+
}
100+
78101
mImageView.setDoubleTapZoomScale(result);
79102
}
80103

@@ -102,4 +125,8 @@ public void onTileLoadError(Exception e) {
102125
public void onPreviewReleased() {
103126

104127
}
128+
129+
public void setInitScaleType(int initScaleType) {
130+
mInitScaleType = initScaleType;
131+
}
105132
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ The MIT License (MIT)
4+
~
5+
~ Copyright (c) 2016 Piasy
6+
~
7+
~ Permission is hereby granted, free of charge, to any person obtaining a copy
8+
~ of this software and associated documentation files (the "Software"), to deal
9+
~ in the Software without restriction, including without limitation the rights
10+
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
~ copies of the Software, and to permit persons to whom the Software is
12+
~ furnished to do so, subject to the following conditions:
13+
~
14+
~ The above copyright notice and this permission notice shall be included in all
15+
~ copies or substantial portions of the Software.
16+
~
17+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
~ SOFTWARE.
24+
-->
25+
26+
<resources>
27+
<declare-styleable name="BigImageView">
28+
<attr format="enum" name="initScaleType">
29+
<enum name="centerInside" value="1"/>
30+
<enum name="centerCrop" value="2"/>
31+
<enum name="auto" value="3"/>
32+
</attr>
33+
</declare-styleable>
34+
</resources>

‎CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Change log
22

3+
+ v1.2.3
4+
- add initScaleType
35
+ v1.2.2
46
- Fix #16
57
+ v1.2.1

‎README.md

+22
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,28 @@ bigImageView.saveImageIntoGallery();
114114
String path = bigImageView.currentImageFile(); // only valid when image file is downloaded.
115115
```
116116

117+
### Init scale type
118+
119+
``` xml
120+
<com.github.piasy.biv.view.BigImageView
121+
android:layout_width="match_parent"
122+
android:layout_height="match_parent"
123+
app:initScaleType="centerInside"
124+
/>
125+
```
126+
127+
``` java
128+
mBigImageView.setInitScaleType(BigImageView.INIT_SCALE_TYPE_CENTER_CROP);
129+
```
130+
131+
| value | effect |
132+
| ------| ------ |
133+
| centerInside | default, Scale the image so that both dimensions of the image will be equal to or less than the corresponding dimension of the view. The image is then centered in the view. This is the default behaviour and best for galleries. |
134+
| centerCrop | Scale the image uniformly so that both dimensions of the image will be equal to or larger than the corresponding dimension of the view. The image is then centered in the view. |
135+
| auto | determine the max scale and min scale by image size and view size, fit the image to screen and centered when loaded. |
136+
137+
You can try the example to checkout the differences! https://fir.im/BIV . Thanks for fir.im!
138+
117139
## Why another big image viewer?
118140

119141
There are several big image viewer libraries, [PhotoDraweeView](https://github.com/ongakuer/PhotoDraweeView), [FrescoImageViewer](https://github.com/stfalcon-studio/FrescoImageViewer), and [Subsampling Scale Image View](https://github.com/davemorrissey/subsampling-scale-image-view).

‎app/OpenKey.jks

1.31 KB
Binary file not shown.

‎app/build.gradle

+23-2
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,37 @@ android {
2929
buildToolsVersion rootProject.ext.androidBuildToolsVersion
3030

3131
defaultConfig {
32-
minSdkVersion rootProject.ext.minSdkVersion
32+
minSdkVersion 14
3333
targetSdkVersion rootProject.ext.targetSdkVersion
3434
versionCode rootProject.ext.releaseVersionCode
3535
versionName rootProject.ext.releaseVersionName
3636

3737
applicationId "com.github.piasy.biv.example"
38+
39+
vectorDrawables.useSupportLibrary = true
3840
}
41+
42+
signingConfigs {
43+
develop {
44+
storeFile file('OpenKey.jks')
45+
storePassword '123456'
46+
keyAlias '123456'
47+
keyPassword '123456'
48+
}
49+
}
50+
3951
buildTypes {
40-
release {
52+
debug {
4153
minifyEnabled false
54+
debuggable true
55+
signingConfig signingConfigs.develop
56+
}
57+
58+
release {
59+
minifyEnabled true
60+
debuggable false
61+
shrinkResources true
62+
signingConfig signingConfigs.develop
4263
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
4364
}
4465
}

‎app/src/main/AndroidManifest.xml

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2222
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
~ SOFTWARE.
24-
-->
25-
24+
-->
2625
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2726
package="com.github.piasy.biv.example">
2827

@@ -45,6 +44,8 @@
4544
</activity>
4645
<activity android:name=".LongImageActivity">
4746
</activity>
47+
<activity android:name=".ScaleTypeActivity">
48+
</activity>
4849
</application>
4950

5051
</manifest>

‎app/src/main/java/com/github/piasy/biv/example/FrescoLoaderActivity.java

-4
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@
2828
import android.os.Bundle;
2929
import android.support.v7.app.AppCompatActivity;
3030
import android.view.View;
31-
import com.github.piasy.biv.BigImageViewer;
3231
import com.github.piasy.biv.indicator.progresspie.ProgressPieIndicator;
33-
import com.github.piasy.biv.loader.fresco.FrescoImageLoader;
3432
import com.github.piasy.biv.view.BigImageView;
3533

3634
public class FrescoLoaderActivity extends AppCompatActivity {
3735

3836
@Override
3937
protected void onCreate(Bundle savedInstanceState) {
40-
BigImageViewer.initialize(FrescoImageLoader.with(getApplicationContext()));
41-
4238
super.onCreate(savedInstanceState);
4339
setContentView(R.layout.activity_big_image);
4440

‎app/src/main/java/com/github/piasy/biv/example/GlideLoaderActivity.java

-4
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@
2828
import android.os.Bundle;
2929
import android.support.v7.app.AppCompatActivity;
3030
import android.view.View;
31-
import com.github.piasy.biv.BigImageViewer;
3231
import com.github.piasy.biv.indicator.progresspie.ProgressPieIndicator;
33-
import com.github.piasy.biv.loader.glide.GlideImageLoader;
3432
import com.github.piasy.biv.view.BigImageView;
3533

3634
public class GlideLoaderActivity extends AppCompatActivity {
3735

3836
@Override
3937
protected void onCreate(Bundle savedInstanceState) {
40-
BigImageViewer.initialize(GlideImageLoader.with(getApplicationContext()));
41-
4238
super.onCreate(savedInstanceState);
4339
setContentView(R.layout.activity_big_image);
4440

‎app/src/main/java/com/github/piasy/biv/example/LongImageActivity.java

-4
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,14 @@
3131
import android.view.View;
3232
import android.widget.Toast;
3333
import com.afollestad.materialdialogs.MaterialDialog;
34-
import com.github.piasy.biv.BigImageViewer;
3534
import com.github.piasy.biv.indicator.progresspie.ProgressPieIndicator;
36-
import com.github.piasy.biv.loader.fresco.FrescoImageLoader;
3735
import com.github.piasy.biv.view.BigImageView;
3836
import com.github.piasy.biv.view.ImageSaveCallback;
3937

4038
public class LongImageActivity extends AppCompatActivity {
4139

4240
@Override
4341
protected void onCreate(Bundle savedInstanceState) {
44-
BigImageViewer.initialize(FrescoImageLoader.with(getApplicationContext()));
45-
4642
super.onCreate(savedInstanceState);
4743
setContentView(R.layout.activity_big_image);
4844

‎app/src/main/java/com/github/piasy/biv/example/MainActivity.java

+9
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@
2828
import android.os.Bundle;
2929
import android.support.v7.app.AppCompatActivity;
3030
import android.view.View;
31+
import com.github.piasy.biv.BigImageViewer;
32+
import com.github.piasy.biv.loader.fresco.FrescoImageLoader;
3133

3234
public class MainActivity extends AppCompatActivity {
3335

3436
@Override
3537
protected void onCreate(Bundle savedInstanceState) {
38+
BigImageViewer.initialize(FrescoImageLoader.with(getApplicationContext()));
3639

3740
super.onCreate(savedInstanceState);
3841
setContentView(R.layout.activity_main);
@@ -55,5 +58,11 @@ public void onClick(View v) {
5558
startActivity(new Intent(MainActivity.this, LongImageActivity.class));
5659
}
5760
});
61+
findViewById(R.id.mScaleType).setOnClickListener(new View.OnClickListener() {
62+
@Override
63+
public void onClick(View v) {
64+
startActivity(new Intent(MainActivity.this, ScaleTypeActivity.class));
65+
}
66+
});
5867
}
5968
}

0 commit comments

Comments
 (0)
Please sign in to comment.