Skip to content

Commit 2aa144c

Browse files
committed
modified the code to include zoom
1 parent 4175861 commit 2aa144c

File tree

37 files changed

+1293
-26
lines changed

37 files changed

+1293
-26
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,7 @@ dependencies {
3636
testImplementation 'junit:junit:4.+'
3737
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
3838
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
39+
40+
implementation 'com.github.bumptech.glide:glide:4.12.0'
41+
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
3942
}

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="divyansh.tech.twodscrollablezoomable">
44

5+
<uses-permission android:name="android.permission.INTERNET" />
6+
57
<application
68
android:allowBackup="true"
79
android:icon="@mipmap/ic_launcher"
810
android:label="@string/app_name"
911
android:roundIcon="@mipmap/ic_launcher_round"
1012
android:supportsRtl="true"
1113
android:theme="@style/Theme.TwoDScrollableZoomable">
12-
<activity android:name=".MainActivity">
14+
<activity android:name=".MainActivity" android:exported="true">
1315
<intent-filter>
1416
<action android:name="android.intent.action.MAIN" />
1517

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package divyansh.tech.twodscrollablezoomable;
2+
3+
import android.content.Context;
4+
import android.util.AttributeSet;
5+
import android.view.GestureDetector;
6+
import android.view.MotionEvent;
7+
import android.view.ScaleGestureDetector;
8+
import android.view.animation.ScaleAnimation;
9+
import android.widget.ScrollView;
10+
11+
public class CustomZoomScrollView extends ScrollView {
12+
13+
14+
// step 1: add some instance
15+
private float mScale = 1f;
16+
private ScaleGestureDetector mScaleDetector;
17+
GestureDetector gestureDetector;
18+
19+
20+
public CustomZoomScrollView(Context context) {
21+
super(context);
22+
}
23+
24+
public CustomZoomScrollView(Context context, AttributeSet attrs) {
25+
super(context, attrs);
26+
//step 2: create instance from GestureDetector(this step should be place into onCreate())
27+
gestureDetector = new GestureDetector(getContext(), new GestureListener());
28+
29+
mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.SimpleOnScaleGestureListener() {
30+
@Override
31+
public boolean onScale(ScaleGestureDetector detector) {
32+
float scale = 1 - detector.getScaleFactor();
33+
34+
float prevScale = mScale;
35+
mScale += scale;
36+
37+
if (mScale < 0.1f) // Minimum scale condition:
38+
mScale = 0.1f;
39+
40+
if (mScale > 10f) // Maximum scale condition:
41+
mScale = 10f;
42+
ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(), detector.getFocusY());
43+
scaleAnimation.setDuration(0);
44+
scaleAnimation.setFillAfter(true);
45+
getRootView().startAnimation(scaleAnimation);
46+
return true;
47+
}
48+
});
49+
}
50+
51+
@Override
52+
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
53+
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
54+
}
55+
56+
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
57+
@Override
58+
public boolean onDown(MotionEvent e) {
59+
return true;
60+
}
61+
// event when double tap occurs
62+
@Override
63+
public boolean onDoubleTap(MotionEvent e) {
64+
// double tap fired.
65+
return true;
66+
}
67+
}
68+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package divyansh.tech.twodscrollablezoomable;
22

33
import androidx.appcompat.app.AppCompatActivity;
4+
import androidx.appcompat.widget.AppCompatImageView;
45

56
import android.os.Bundle;
67

8+
import com.bumptech.glide.Glide;
9+
710
public class MainActivity extends AppCompatActivity {
811

912
@Override
1013
protected void onCreate(Bundle savedInstanceState) {
1114
super.onCreate(savedInstanceState);
1215
setContentView(R.layout.activity_main);
16+
17+
AppCompatImageView iv = findViewById(R.id.image);
18+
TwoDScrollableZoomableView view = findViewById(R.id.scrollable);
19+
Glide.with(this).load("https://wallpaperaccess.com/full/391249.jpg").into(iv);
1320
}
1421
}

0 commit comments

Comments
 (0)