Skip to content

Commit

Permalink
modified the code to include zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
justdvnsh committed Oct 11, 2021
1 parent 4175861 commit 2aa144c
Show file tree
Hide file tree
Showing 37 changed files with 1,293 additions and 26 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ dependencies {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}
4 changes: 3 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="divyansh.tech.twodscrollablezoomable">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TwoDScrollableZoomable">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package divyansh.tech.twodscrollablezoomable;

import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.animation.ScaleAnimation;
import android.widget.ScrollView;

public class CustomZoomScrollView extends ScrollView {


// step 1: add some instance
private float mScale = 1f;
private ScaleGestureDetector mScaleDetector;
GestureDetector gestureDetector;


public CustomZoomScrollView(Context context) {
super(context);
}

public CustomZoomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
//step 2: create instance from GestureDetector(this step should be place into onCreate())
gestureDetector = new GestureDetector(getContext(), new GestureListener());

mScaleDetector = new ScaleGestureDetector(getContext(), new ScaleGestureDetector.SimpleOnScaleGestureListener() {
@Override
public boolean onScale(ScaleGestureDetector detector) {
float scale = 1 - detector.getScaleFactor();

float prevScale = mScale;
mScale += scale;

if (mScale < 0.1f) // Minimum scale condition:
mScale = 0.1f;

if (mScale > 10f) // Maximum scale condition:
mScale = 10f;
ScaleAnimation scaleAnimation = new ScaleAnimation(1f / prevScale, 1f / mScale, 1f / prevScale, 1f / mScale, detector.getFocusX(), detector.getFocusY());
scaleAnimation.setDuration(0);
scaleAnimation.setFillAfter(true);
getRootView().startAnimation(scaleAnimation);
return true;
}
});
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
// event when double tap occurs
@Override
public boolean onDoubleTap(MotionEvent e) {
// double tap fired.
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package divyansh.tech.twodscrollablezoomable;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;

import android.os.Bundle;

import com.bumptech.glide.Glide;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

AppCompatImageView iv = findViewById(R.id.image);
TwoDScrollableZoomableView view = findViewById(R.id.scrollable);
Glide.with(this).load("https://wallpaperaccess.com/full/391249.jpg").into(iv);
}
}
Loading

0 comments on commit 2aa144c

Please sign in to comment.