Skip to content

Commit

Permalink
Smooth Scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
milindrc committed Jan 4, 2020
1 parent 21d6d05 commit f3d4226
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 41 deletions.
1 change: 1 addition & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
android:id="@+id/vScroll">

<com.matrixdev.mosaic.HScroll

android:id="@+id/hScroll"
android:scrollbars="none"
android:layout_width="match_parent"
Expand Down
41 changes: 28 additions & 13 deletions mosaic/src/main/java/com/matrixdev/mosaic/HScroll.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;

public class HScroll extends HorizontalScrollView {


OnTouchListener onTouchListener;
boolean isBlocked;
ScrollView scrollView;

public HScroll(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Expand All @@ -23,20 +24,26 @@ public HScroll(Context context) {
super(context);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
// return onTouchListener.onTouch(null,ev);
return isBlocked;
}
// @Override
//// public boolean onTouchEvent(MotionEvent ev) {
////// return onTouchListener.onTouch(null,ev);
//// return false;
//// }

// @Override public boolean onTouchEvent(MotionEvent event)
// {
// boolean ret = super.onTouchEvent(event);
// ret = ret | scrollView.onTouchEvent(event);
// return ret;
// }
//
// @Override public boolean onInterceptTouchEvent(MotionEvent event)
// {
// boolean ret = super.onInterceptTouchEvent(event);
// ret = ret | scrollView.onInterceptTouchEvent(event);
// return ret;
// }

public boolean isBlocked() {
return isBlocked;
}

public void setBlocked(boolean blocked) {
isBlocked = blocked;
}

public OnTouchListener getOnTouchListener() {
return onTouchListener;
Expand All @@ -46,4 +53,12 @@ public OnTouchListener getOnTouchListener() {
public void setOnTouchListener(OnTouchListener onTouchListener) {
this.onTouchListener = onTouchListener;
}

public ScrollView getScrollView() {
return scrollView;
}

public void setScrollView(ScrollView scrollView) {
this.scrollView = scrollView;
}
}
24 changes: 8 additions & 16 deletions mosaic/src/main/java/com/matrixdev/mosaic/MosaicView.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Toast;


Expand Down Expand Up @@ -53,15 +55,15 @@ public class MosaicView extends View {

private int halfOffset = 60;
private int fullOffset = 120;
private int leftDistance;
private int rightDistance;
private int leftDistance = 1;
private int rightDistance=1;
private int midX;
private int currentPosX;
private int maxScrollY;
private int verLim;
private int midY;
private int topDistance;
private int bottomDistance;
private int topDistance = 1;
private int bottomDistance =1;
private int currentPosY;
private Bitmap placeholder;

Expand All @@ -75,8 +77,6 @@ public class MosaicView extends View {
private Handler timer;
private ItemChooseInterface onItemChooseListener;
private ViewGroup rootview;
private float mx;
private float my;

private boolean isRoundedEdges;
private boolean isFadeEnabled;
Expand Down Expand Up @@ -240,17 +240,15 @@ private void init() {
hscroll = ((HScroll) ((LinearLayout) getParent()).getParent());
vScroll = (VScroll) ((HScroll) ((LinearLayout) getParent()).getParent()).getParent();

rootview = (ViewGroup) vScroll.getParent();
rootview.setOnTouchListener(new OnTouchListener() {
vScroll.setHs(hscroll);
vScroll.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
float curX, curY;

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:
mx = event.getX();
my = event.getY();
touchX = event.getX() + hscroll.getScrollX();
touchY = event.getY() + vScroll.getScrollY();
invalidate();
Expand All @@ -268,10 +266,6 @@ public boolean onTouch(View view, MotionEvent event) {
touchX = 0;
touchY = 0;
}
vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
hscroll.scrollBy((int) (mx - curX), (int) (my - curY));
mx = curX;
my = curY;
break;
case MotionEvent.ACTION_UP:
if (touchX != 0) {
Expand All @@ -283,8 +277,6 @@ public boolean onTouch(View view, MotionEvent event) {
}
curX = event.getX();
curY = event.getY();
vScroll.scrollBy((int) (mx - curX), (int) (my - curY));
hscroll.scrollBy((int) (mx - curX), (int) (my - curY));
break;
}

Expand Down
41 changes: 29 additions & 12 deletions mosaic/src/main/java/com/matrixdev/mosaic/VScroll.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;

public class VScroll extends ScrollView {

private int verticalScrollOffset;
private boolean isBlocked;
OnTouchListener onTouchListener;
HorizontalScrollView hs;


public VScroll(Context context, AttributeSet attrs, int defStyle) {
Expand All @@ -25,10 +26,26 @@ public VScroll(Context context) {
super(context);
}

// @Override
// public boolean onTouchEvent(MotionEvent ev) {
//// return onTouchListener.onTouch(null,ev);
// return false;
// }

@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
onTouchListener.onTouch(null,event);
Log.d("----vtouch",""+event.getAction());
hs.dispatchTouchEvent(event);
return true;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
// return onTouchListener.onTouch(null,ev);
return isBlocked;
public boolean onInterceptTouchEvent(MotionEvent event) {
super.onInterceptTouchEvent(event);
hs.onInterceptTouchEvent(event);
return true;
}

@Override
Expand All @@ -45,14 +62,6 @@ public void setVerticalScrollOffset(int verticalScrollOffset) {
this.verticalScrollOffset = verticalScrollOffset;
}

public boolean isBlocked() {
return isBlocked;
}

public void setBlocked(boolean blocked) {
isBlocked = blocked;
}

public OnTouchListener getOnTouchListener() {
return onTouchListener;
}
Expand All @@ -61,4 +70,12 @@ public OnTouchListener getOnTouchListener() {
public void setOnTouchListener(OnTouchListener onTouchListener) {
this.onTouchListener = onTouchListener;
}

public HorizontalScrollView getHs() {
return hs;
}

public void setHs(HorizontalScrollView hs) {
this.hs = hs;
}
}

0 comments on commit f3d4226

Please sign in to comment.