ines-num lines-num-old">
572
|
private void cancelFling() {
|
|
573
|
|
if (null != mCurrentFlingRunnable) {
|
|
574
|
|
mCurrentFlingRunnable.cancelFling();
|
|
575
|
|
mCurrentFlingRunnable = null;
|
|
576
|
|
}
|
|
577
|
|
}
|
|
578
|
|
|
|
579
|
|
/**
|
|
580
|
|
* Helper method that simply checks the Matrix, and then displays the result
|
|
581
|
|
*/
|
|
582
|
|
private void checkAndDisplayMatrix() {
|
|
583
|
|
checkMatrixBounds();
|
|
584
|
|
setImageViewMatrix(getDisplayMatrix());
|
|
585
|
|
}
|
|
586
|
|
|
|
587
|
|
private void checkImageViewScaleType() {
|
|
588
|
|
ImageView imageView = getImageView();
|
|
589
|
|
|
|
590
|
|
/**
|
|
591
|
|
* PhotoView's getScaleType() will just divert to this.getScaleType() so
|
|
592
|
|
* only call if we're not attached to a PhotoView.
|
|
593
|
|
*/
|
|
594
|
|
if (null != imageView && !(imageView instanceof PhotoView)) {
|
|
595
|
|
if (imageView.getScaleType() != ScaleType.MATRIX) {
|
|
596
|
|
throw new IllegalStateException(
|
|
597
|
|
"The ImageView's ScaleType has been changed since attaching a PhotoViewAttacher");
|
|
598
|
|
}
|
|
599
|
|
}
|
|
600
|
|
}
|
|
601
|
|
|
|
602
|
|
private void checkMatrixBounds() {
|
|
603
|
|
final ImageView imageView = getImageView();
|
|
604
|
|
if (null == imageView) {
|
|
605
|
|
return;
|
|
606
|
|
}
|
|
607
|
|
|
|
608
|
|
final RectF rect = getDisplayRect(getDisplayMatrix());
|
|
609
|
|
if (null == rect) {
|
|
610
|
|
return;
|
|
611
|
|
}
|
|
612
|
|
|
|
613
|
|
final float height = rect.height(), width = rect.width();
|
|
614
|
|
float deltaX = 0, deltaY = 0;
|
|
615
|
|
|
|
616
|
|
final int viewHeight = imageView.getHeight();
|
|
617
|
|
if (height <= viewHeight) {
|
|
618
|
|
switch (mScaleType) {
|
|
619
|
|
case FIT_START:
|
|
620
|
|
deltaY = -rect.top;
|
|
621
|
|
break;
|
|
622
|
|
case FIT_END:
|
|
623
|
|
deltaY = viewHeight - height - rect.top;
|
|
624
|
|
break;
|
|
625
|
|
default:
|
|
626
|
|
deltaY = (viewHeight - height) / 2 - rect.top;
|
|
627
|
|
break;
|
|
628
|
|
}
|
|
629
|
|
} else if (rect.top > 0) {
|
|
630
|
|
deltaY = -rect.top;
|
|
631
|
|
} else if (rect.bottom < viewHeight) {
|
|
632
|
|
deltaY = viewHeight - rect.bottom;
|
|
633
|
|
}
|
|
634
|
|
|
|
635
|
|
final int viewWidth = imageView.getWidth();
|
|
636
|
|
if (width <= viewWidth) {
|
|
637
|
|
switch (mScaleType) {
|
|
638
|
|
case FIT_START:
|
|
639
|
|
deltaX = -rect.left;
|
|
640
|
|
break;
|
|
641
|
|
case FIT_END:
|
|
642
|
|
deltaX = viewWidth - width - rect.left;
|
|
643
|
|
break;
|
|
644
|
|
default:
|
|
645
|
|
deltaX = (viewWidth - width) / 2 - rect.left;
|
|
646
|
|
break;
|
|
647
|
|
}
|
|
648
|
|
mScrollEdge = EDGE_BOTH;
|
|
649
|
|
} else if (rect.left > 0) {
|
|
650
|
|
mScrollEdge = EDGE_LEFT;
|
|
651
|
|
deltaX = -rect.left;
|
|
652
|
|
} else if (rect.right < viewWidth) {
|
|
653
|
|
deltaX = viewWidth - rect.right;
|
|
654
|
|
mScrollEdge = EDGE_RIGHT;
|
|
655
|
|
} else {
|
|
656
|
|
mScrollEdge = EDGE_NONE;
|
|
657
|
|
}
|
|
658
|
|
|
|
659
|
|
// Finally actually translate the matrix
|
|
660
|
|
mSuppMatrix.postTranslate(deltaX, deltaY);
|
|
661
|
|
}
|
|
662
|
|
|
|
663
|
|
/**
|
|
664
|
|
* Helper method that maps the supplied Matrix to the current Drawable
|
|
665
|
|
*
|
|
666
|
|
* @param matrix
|
|
667
|
|
* - Matrix to map Drawable against
|
|
668
|
|
* @return RectF - Displayed Rectangle
|
|
669
|
|
*/
|
|
670
|
|
private RectF getDisplayRect(Matrix matrix) {
|
|
671
|
|
ImageView imageView = getImageView();
|
|
672
|
|
|
|
673
|
|
if (null != imageView) {
|
|
674
|
|
Drawable d = imageView.getDrawable();
|
|
675
|
|
if (null != d) {
|
|
676
|
|
mDisplayRect.set(0, 0, d.getIntrinsicWidth(),
|
|
677
|
|
d.getIntrinsicHeight());
|
|
678
|
|
matrix.mapRect(mDisplayRect);
|
|
679
|
|
return mDisplayRect;
|
|
680
|
|
}
|
|
681
|
|
}
|
|
682
|
|
return null;
|
|
683
|
|
}
|
|
684
|
|
|
|
685
|
|
/**
|
|
686
|
|
* Helper method that 'unpacks' a Matrix and returns the required value
|
|
687
|
|
*
|
|
688
|
|
* @param matrix
|
|
689
|
|
* - Matrix to unpack
|
|
690
|
|
* @param whichValue
|
|
691
|
|
* - Which value from Matrix.M* to return
|
|
692
|
|
* @return float - returned value
|
|
693
|
|
*/
|
|
694
|
|
private float getValue(Matrix matrix, int whichValue) {
|
|
695
|
|
matrix.getValues(mMatrixValues);
|
|
696
|
|
return mMatrixValues[whichValue];
|
|
697
|
|
}
|
|
698
|
|
|
|
699
|
|
/**
|
|
700
|
|
* Resets the Matrix back to FIT_CENTER, and then displays it.s
|
|
701
|
|
*/
|
|
702
|
|
private void resetMatrix() {
|
|
703
|
|
mSuppMatrix.reset();
|
|
704
|
|
setImageViewMatrix(getDisplayMatrix());
|
|
705
|
|
checkMatrixBounds();
|
|
706
|
|
}
|
|
707
|
|
|
|
708
|
|
private void setImageViewMatrix(Matrix matrix) {
|
|
709
|
|
ImageView imageView = getImageView();
|
|
710
|
|
if (null != imageView) {
|
|
711
|
|
|
|
712
|
|
checkImageViewScaleType();
|
|
713
|
|
imageView.setImageMatrix(matrix);
|
|
714
|
|
|
|
715
|
|
// Call MatrixChangedListener if needed
|
|
716
|
|
if (null != mMatrixChangeListener) {
|
|
717
|
|
RectF displayRect = getDisplayRect(matrix);
|
|
718
|
|
if (null != displayRect) {
|
|
719
|
|
mMatrixChangeListener.onMatrixChanged(displayRect);
|
|
720
|
|
}
|
|
721
|
|
}
|
|
722
|
|
}
|
|
723
|
|
}
|
|
724
|
|
|
|
725
|
|
/**
|
|
726
|
|
* Calculate Matrix for FIT_CENTER
|
|
727
|
|
*
|
|
728
|
|
* @param d
|
|
729
|
|
* - Drawable being displayed
|
|
730
|
|
*/
|
|
731
|
|
private void updateBaseMatrix(Drawable d) {
|
|
732
|
|
ImageView imageView = getImageView();
|
|
733
|
|
if (null == imageView || null == d) {
|
|
734
|
|
return;
|
|
735
|
|
}
|
|
736
|
|
|
|
737
|
|
final float viewWidth = imageView.getWidth();
|
|
738
|
|
final float viewHeight = imageView.getHeight();
|
|
739
|
|
final int drawableWidth = d.getIntrinsicWidth();
|
|
740
|
|
final int drawableHeight = d.getIntrinsicHeight();
|
|
741
|
|
|
|
742
|
|
mBaseMatrix.reset();
|
|
743
|
|
|
|
744
|
|
final float widthScale = viewWidth / drawableWidth;
|
|
745
|
|
final float heightScale = viewHeight / drawableHeight;
|
|
746
|
|
|
|
747
|
|
if (mScaleType == ScaleType.CENTER) {
|
|
748
|
|
mBaseMatrix.postTranslate((viewWidth - drawableWidth) / 2F,
|
|
749
|
|
(viewHeight - drawableHeight) / 2F);
|
|
750
|
|
|
|
751
|
|
} else if (mScaleType == ScaleType.CENTER_CROP) {
|
|
752
|
|
float scale = Math.max(widthScale, heightScale);
|
|
753
|
|
mBaseMatrix.postScale(scale, scale);
|
|
754
|
|
mBaseMatrix.postTranslate((viewWidth - drawableWidth * scale) / 2F,
|
|
755
|
|
(viewHeight - drawableHeight * scale) / 2F);
|
|
756
|
|
|
|
757
|
|
} else if (mScaleType == ScaleType.CENTER_INSIDE) {
|
|
758
|
|
float scale = Math.min(1.0f, Math.min(widthScale, heightScale));
|
|
759
|
|
mBaseMatrix.postScale(scale, scale);
|
|
760
|
|
mBaseMatrix.postTranslate((viewWidth - drawableWidth * scale) / 2F,
|
|
761
|
|
(viewHeight - drawableHeight * scale) / 2F);
|
|
762
|
|
|
|
763
|
|
} else {
|
|
764
|
|
RectF mTempSrc = new RectF(0, 0, drawableWidth, drawableHeight);
|
|
765
|
|
RectF mTempDst = new RectF(0, 0, viewWidth, viewHeight);
|
|
766
|
|
|
|
767
|
|
switch (mScaleType) {
|
|
768
|
|
case FIT_CENTER:
|
|
769
|
|
mBaseMatrix
|
|
770
|
|
.setRectToRect(mTempSrc, mTempDst, ScaleToFit.CENTER);
|
|
771
|
|
break;
|
|
772
|
|
|
|
773
|
|
case FIT_START:
|
|
774
|
|
mBaseMatrix.setRectToRect(mTempSrc, mTempDst, ScaleToFit.START);
|
|
775
|
|
break;
|
|
776
|
|
|
|
777
|
|
case FIT_END:
|
|
778
|
|
mBaseMatrix.setRectToRect(mTempSrc, mTempDst, ScaleToFit.END);
|
|
779
|
|
break;
|
|
780
|
|
|
|
781
|
|
case FIT_XY:
|
|
782
|
|
mBaseMatrix.setRectToRect(mTempSrc, mTempDst, ScaleToFit.FILL);
|
|
783
|
|
break;
|
|
784
|
|
|
|
785
|
|
default:
|
|
786
|
|
break;
|
|
787
|
|
}
|
|
788
|
|
}
|
|
789
|
|
|
|
790
|
|
resetMatrix();
|
|
791
|
|
}
|
|
792
|
|
|
|
793
|
|
/**
|
|
794
|
|
* Interface definition for a callback to be invoked when the internal
|
|
795
|
|
* Matrix has changed for this View.
|
|
796
|
|
*
|
|
797
|
|
* @author Chris Banes
|
|
798
|
|
*/
|
|
799
|
|
public static interface OnMatrixChangedListener {
|
|
800
|
|
/**
|
|
801
|
|
* Callback for when the Matrix displaying the Drawable has changed.
|
|
802
|
|
* This could be because the View's bounds have changed, or the user has
|
|
803
|
|
* zoomed.
|
|
804
|
|
*
|
|
805
|
|
* @param rect
|
|
806
|
|
* - Rectangle displaying the Drawable's new bounds.
|
|
807
|
|
*/
|
|
808
|
|
void onMatrixChanged(RectF rect);
|
|
809
|
|
}
|
|
810
|
|
|
|
811
|
|
/**
|
|
812
|
|
* Interface definition for a callback to be invoked when the Photo is
|
|
813
|
|
* tapped with a single tap.
|
|
814
|
|
*
|
|
815
|
|
* @author Chris Banes
|
|
816
|
|
*/
|
|
817
|
|
public static interface OnPhotoTapListener {
|
|
818
|
|
|
|
819
|
|
/**
|
|
820
|
|
* A callback to receive where the user taps on a photo. You will only
|
|
821
|
|
* receive a callback if the user taps on the actual photo, tapping on
|
|
822
|
|
* 'whitespace' will be ignored.
|
|
823
|
|
*
|
|
824
|
|
* @param view
|
|
825
|
|
* - View the user tapped.
|
|
826
|
|
* @param x
|
|
827
|
|
* - where the user tapped from the of the Drawable, as
|
|
828
|
|
* percentage of the Drawable width.
|
|
829
|
|
* @param y
|
|
830
|
|
* - where the user tapped from the top of the Drawable, as
|
|
831
|
|
* percentage of the Drawable height.
|
|
832
|
|
*/
|
|
833
|
|
void onPhotoTap(View view, float x, float y);
|
|
834
|
|
}
|
|
835
|
|
|
|
836
|
|
/**
|
|
837
|
|
* Interface definition for a callback to be invoked when the ImageView is
|
|
838
|
|
* tapped with a single tap.
|
|
839
|
|
*
|
|
840
|
|
* @author Chris Banes
|
|
841
|
|
*/
|
|
842
|
|
public static interface OnViewTapListener {
|
|
843
|
|
|
|
844
|
|
/**
|
|
845
|
|
* A callback to receive where the user taps on a ImageView. You will
|
|
846
|
|
* receive a callback if the user taps anywhere on the view, tapping on
|
|
847
|
|
* 'whitespace' will not be ignored.
|
|
848
|
|
*
|
|
849
|
|
* @param view
|
|
850
|
|
* - View the user tapped.
|
|
851
|
|
* @param x
|
|
852
|
|
* - where the user tapped from the left of the View.
|
|
853
|
|
* @param y
|
|
854
|
|
* - where the user tapped from the top of the View.
|
|
855
|
|
*/
|
|
856
|
|
void onViewTap(View view, float x, float y);
|
|
857
|
|
}
|
|
858
|
|
|
|
859
|
|
private class AnimatedZoomRunnable implements Runnable {
|
|
860
|
|
|
|
861
|
|
// These are 'postScale' values, means they're compounded each iteration
|
|
862
|
|
static final float ANIMATION_SCALE_PER_ITERATION_IN = 1.07f;
|
|
863
|
|
static final float ANIMATION_SCALE_PER_ITERATION_OUT = 0.93f;
|
|
864
|
|
|
|
865
|
|
private final float mFocalX, mFocalY;
|
|
866
|
|
private final float mTargetZoom;
|
|
867
|
|
private final float mDeltaScale;
|
|
868
|
|
|
|
869
|
|
public AnimatedZoomRunnable(final float currentZoom,
|
|
870
|
|
final float targetZoom, final float focalX, final float focalY) {
|
|
871
|
|
mTargetZoom = targetZoom;
|
|
872
|
|
mFocalX = focalX;
|
|
873
|
|
mFocalY = focalY;
|
|
874
|
|
|
|
875
|
|
if (currentZoom < targetZoom) {
|
|
876
|
|
mDeltaScale = ANIMATION_SCALE_PER_ITERATION_IN;
|
|
877
|
|
} else {
|
|
878
|
|
mDeltaScale = ANIMATION_SCALE_PER_ITERATION_OUT;
|
|
879
|
|
}
|
|
880
|
|
}
|
|
881
|
|
|
|
882
|
|
public void run() {
|
|
883
|
|
ImageView imageView = getImageView();
|
|
884
|
|
|
|
885
|
|
if (null != imageView) {
|
|
886
|
|
mSuppMatrix.postScale(mDeltaScale, mDeltaScale, mFocalX,
|
|
887
|
|
mFocalY);
|
|
888
|
|
checkAndDisplayMatrix();
|
|
889
|
|
|
|
890
|
|
final float currentScale = getScale();
|
|
891
|
|
|
|
892
|
|
if ((mDeltaScale > 1f && currentScale < mTargetZoom)
|
|
893
|
|
|| (mDeltaScale < 1f && mTargetZoom < currentScale)) {
|
|
894
|
|
// We haven't hit our target scale yet, so post ourselves
|
|
895
|
|
// again
|
|
896
|
|
Compat.postOnAnimation(imageView, this);
|
|
897
|
|
|
|
898
|
|
} else {
|
|
899
|
|
// We've scaled past our target zoom, so calculate the
|
|
900
|
|
// necessary scale so we're back at target zoom
|
|
901
|
|
final float delta = mTargetZoom / currentScale;
|
|
902
|
|
mSuppMatrix.postScale(delta, delta, mFocalX, mFocalY);
|
|
903
|
|
checkAndDisplayMatrix();
|
|
904
|
|
}
|
|
905
|
|
}
|
|
906
|
|
}
|
|
907
|
|
}
|
|
908
|
|
|
|
909
|
|
private class FlingRunnable implements Runnable {
|
|
910
|
|
|
|
911
|
|
private final ScrollerProxy mScroller;
|
|
912
|
|
private int mCurrentX, mCurrentY;
|
|
913
|
|
|
|
914
|
|
public FlingRunnable(Context context) {
|
|
915
|
|
mScroller = ScrollerProxy.getScroller(context);
|
|
916
|
|
}
|
|
917
|
|
|
|
918
|
|
public void cancelFling() {
|
|
919
|
|
if (DEBUG) {
|
|
920
|
|
Log.d(LOG_TAG, "Cancel Fling");
|
|
921
|
|
}
|
|
922
|
|
mScroller.forceFinished(true);
|
|
923
|
|
}
|
|
924
|
|
|
|
925
|
|
public void fling(int viewWidth, int viewHeight, int velocityX,
|
|
926
|
|
int velocityY) {
|
|
927
|
|
final RectF rect = getDisplayRect();
|
|
928
|
|
if (null == rect) {
|
|
929
|
|
return;
|
|
930
|
|
}
|
|
931
|
|
|
|
932
|
|
final int startX = Math.round(-rect.left);
|
|
933
|
|
final int minX, maxX, minY, maxY;
|
|
934
|
|
|
|
935
|
|
if (viewWidth < rect.width()) {
|
|
936
|
|
minX = 0;
|
|
937
|
|
maxX = Math.round(rect.width() - viewWidth);
|
|
938
|
|
} else {
|
|
939
|
|
minX = maxX = startX;
|
|
940
|
|
}
|
|
941
|
|
|
|
942
|
|
final int startY = Math.round(-rect.top);
|
|
943
|
|
if (viewHeight < rect.height()) {
|
|
944
|
|
minY = 0;
|
|
945
|
|
maxY = Math.round(rect.height() - viewHeight);
|
|
946
|
|
} else {
|
|
947
|
|
minY = maxY = startY;
|
|
948
|
|
}
|
|
949
|
|
|
|
950
|
|
mCurrentX = startX;
|
|
951
|
|
mCurrentY = startY;
|
|
952
|
|
|
|
953
|
|
if (DEBUG) {
|
|
954
|
|
Log.d(LOG_TAG, "fling. StartX:" + startX + " StartY:" + startY
|
|
955
|
|
+ " MaxX:" + maxX + " MaxY:" + maxY);
|
|
956
|
|
}
|
|
957
|
|
|
|
958
|
|
// If we actually can move, fling the scroller
|
|
959
|
|
if (startX != maxX || startY != maxY) {
|
|
960
|
|
mScroller.fling(startX, startY, velocityX, velocityY, minX,
|
|
961
|
|
maxX, minY, maxY, 0, 0);
|
|
962
|
|
}
|
|
963
|
|
}
|
|
964
|
|
|
|
965
|
|
@Override
|
|
966
|
|
public void run() {
|
|
967
|
|
ImageView imageView = getImageView();
|
|
968
|
|
if (null != imageView && mScroller.computeScrollOffset()) {
|
|
969
|
|
|
|
970
|
|
final int newX = mScroller.getCurrX();
|
|
971
|
|
final int newY = mScroller.getCurrY();
|
|
972
|
|
|
|
973
|
|
if (DEBUG) {
|
|
974
|
|
Log.d(LOG_TAG, "fling run(). CurrentX:" + mCurrentX
|
|
975
|
|
+ " CurrentY:" + mCurrentY + " NewX:" + newX
|
|
976
|
|
+ " NewY:" + newY);
|
|
977
|
|
}
|
|
978
|
|
|
|
979
|
|
mSuppMatrix.postTranslate(mCurrentX - newX, mCurrentY - newY);
|
|
980
|
|
setImageViewMatrix(getDisplayMatrix());
|
|
981
|
|
|
|
982
|
|
mCurrentX = newX;
|
|
983
|
|
mCurrentY = newY;
|
|
984
|
|
|
|
985
|
|
// Post On animation
|
|
986
|
|
Compat.postOnAnimation(imageView, this);
|
|
987
|
|
}
|
|
988
|
|
}
|
|
989
|
|
}
|
|
990
|
|
}
|
|
|
@ -1,13 +0,0 @@
|
|
1
|
|
package com.electric.chargingpile.zoom;
|
|
2
|
|
|
|
3
|
|
import android.annotation.TargetApi;
|
|
4
|
|
import android.view.View;
|
|
5
|
|
|
|
6
|
|
@TargetApi(16)
|
|
7
|
|
public class SDK16 {
|
|
8
|
|
|
|
9
|
|
public static void postOnAnimation(View view, Runnable r) {
|
|
10
|
|
view.postOnAnimation(r);
|
|
11
|
|
}
|
|
12
|
|
|
|
13
|
|
}
|
|
|
@ -1,101 +0,0 @@
|
|
1
|
|
package com.electric.chargingpile.zoom;
|
|
2
|
|
|
|
3
|
|
import android.annotation.TargetApi;
|
|
4
|
|
import android.content.Context;
|
|
5
|
|
import android.os.Build.VERSION;
|
|
6
|
|
import android.os.Build.VERSION_CODES;
|
|
7
|
|
import android.widget.OverScroller;
|
|
8
|
|
import android.widget.Scroller;
|
|
9
|
|
|
|
10
|
|
public abstract class ScrollerProxy {
|
|
11
|
|
|
|
12
|
|
public static ScrollerProxy getScroller(Context context) {
|
|
13
|
|
if (VERSION.SDK_INT < VERSION_CODES.GINGERBREAD) {
|
|
14
|
|
return new PreGingerScroller(context);
|
|
15
|
|
} else {
|
|
16
|
|
return new GingerScroller(context);
|
|
17
|
|
}
|
|
18
|
|
}
|
|
19
|
|
|
|
20
|
|
public abstract boolean computeScrollOffset();
|
|
21
|
|
|
|
22
|
|
public abstract void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY,
|
|
23
|
|
int maxY, int overX, int overY);
|
|
24
|
|
|
|
25
|
|
public abstract void forceFinished(boolean finished);
|
|
26
|
|
|
|
27
|
|
public abstract int getCurrX();
|
|
28
|
|
|
|
29
|
|
public abstract int getCurrY();
|
|
30
|
|
|
|
31
|
|
@TargetApi(9)
|
|
32
|
|
private static class GingerScroller extends ScrollerProxy {
|
|
33
|
|
|
|
34
|
|
private OverScroller mScroller;
|
|
35
|
|
|
|
36
|
|
public GingerScroller(Context context) {
|
|
37
|
|
mScroller = new OverScroller(context);
|
|
38
|
|
}
|
|
39
|
|
|
|
40
|
|
@Override
|
|
41
|
|
public boolean computeScrollOffset() {
|
|
42
|
|
return mScroller.computeScrollOffset();
|
|
43
|
|
}
|
|
44
|
|
|
|
45
|
|
@Override
|
|
46
|
|
public void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY,
|
|
47
|
|
int overX, int overY) {
|
|
48
|
|
mScroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY, overX, overY);
|
|
49
|
|
}
|
|
50
|
|
|
|
51
|
|
@Override
|
|
52
|
|
public void forceFinished(boolean finished) {
|
|
53
|
|
mScroller.forceFinished(finished);
|
|
54
|
|
}
|
|
55
|
|
|
|
56
|
|
@Override
|
|
57
|
|
public int getCurrX() {
|
|
58
|
|
return mScroller.getCurrX();
|
|
59
|
|
}
|
|
60
|
|
|
|
61
|
|
@Override
|
|
62
|
|
public int getCurrY() {
|
|
63
|
|
return mScroller.getCurrY();
|
|
64
|
|
}
|
|
65
|
|
}
|
|
66
|
|
|
|
67
|
|
private static class PreGingerScroller extends ScrollerProxy {
|
|
68
|
|
|
|
69
|
|
private Scroller mScroller;
|
|
70
|
|
|
|
71
|
|
public PreGingerScroller(Context context) {
|
|
72
|
|
mScroller = new Scroller(context);
|
|
73
|
|
}
|
|
74
|
|
|
|
75
|
|
@Override
|
|
76
|
|
public boolean computeScrollOffset() {
|
|
77
|
|
return mScroller.computeScrollOffset();
|
|
78
|
|
}
|
|
79
|
|
|
|
80
|
|
@Override
|
|
81
|
|
public void fling(int startX, int startY, int velocityX, int velocityY, int minX, int maxX, int minY, int maxY,
|
|
82
|
|
int overX, int overY) {
|
|
83
|
|
mScroller.fling(startX, startY, velocityX, velocityY, minX, maxX, minY, maxY);
|
|
84
|
|
}
|
|
85
|
|
|
|
86
|
|
@Override
|
|
87
|
|
public void forceFinished(boolean finished) {
|
|
88
|
|
mScroller.forceFinished(finished);
|
|
89
|
|
}
|
|
90
|
|
|
|
91
|
|
@Override
|
|
92
|
|
public int getCurrX() {
|
|
93
|
|
return mScroller.getCurrX();
|
|
94
|
|
}
|
|
95
|
|
|
|
96
|
|
@Override
|
|
97
|
|
public int getCurrY() {
|
|
98
|
|
return mScroller.getCurrY();
|
|
99
|
|
}
|
|
100
|
|
}
|
|
101
|
|
}
|
|
|
@ -1,253 +0,0 @@
|
|
1
|
|
package com.electric.chargingpile.zoom;
|
|
2
|
|
|
|
3
|
|
import android.annotation.TargetApi;
|
|
4
|
|
import android.content.Context;
|
|
5
|
|
import android.os.Build;
|
|
6
|
|
import android.util.FloatMath;
|
|
7
|
|
import android.view.MotionEvent;
|
|
8
|
|
import android.view.ScaleGestureDetector;
|
|
9
|
|
import android.view.ScaleGestureDetector.OnScaleGestureListener;
|
|
10
|
|
import android.view.VelocityTracker;
|
|
11
|
|
import android.view.ViewConfiguration;
|
|
12
|
|
|
|
13
|
|
public abstract class VersionedGestureDetector {
|
|
14
|
|
static final String LOG_TAG = "VersionedGestureDetector";
|
|
15
|
|
OnGestureListener mListener;
|
|
16
|
|
|
|
17
|
|
public static VersionedGestureDetector newInstance(Context context, OnGestureListener listener) {
|
|
18
|
|
final int sdkVersion = Build.VERSION.SDK_INT;
|
|
19
|
|
VersionedGestureDetector detector = null;
|
|
20
|
|
|
|
21
|
|
if (sdkVersion < Build.VERSION_CODES.ECLAIR) {
|
|
22
|
|
detector = new CupcakeDetector(context);
|
|
23
|
|
} else if (sdkVersion < Build.VERSION_CODES.FROYO) {
|
|
24
|
|
detector = new EclairDetector(context);
|
|
25
|
|
} else {
|
|
26
|
|
detector = new FroyoDetector(context);
|
|
27
|
|
}
|
|
28
|
|
|
|
29
|
|
detector.mListener = listener;
|
|
30
|
|
|
|
31
|
|
return detector;
|
|
32
|
|
}
|
|
33
|
|
|
|
34
|
|
public abstract boolean onTouchEvent(MotionEvent ev);
|
|
35
|
|
|
|
36
|
|
public abstract boolean isScaling();
|
|
37
|
|
|
|
38
|
|
public static interface OnGestureListener {
|
|
39
|
|
public void onDrag(float dx, float dy);
|
|
40
|
|
|
|
41
|
|
public void onFling(float startX, float startY, float velocityX, float velocityY);
|
|
42
|
|
|
|
43
|
|
public void onScale(float scaleFactor, float focusX, float focusY);
|
|
44
|
|
}
|
|
45
|
|
|
|
46
|
|
private static class CupcakeDetector extends VersionedGestureDetector {
|
|
47
|
|
|
|
48
|
|
float mLastTouchX;
|
|
49
|
|
float mLastTouchY;
|
|
50
|
|
final float mTouchSlop;
|
|
51
|
|
final float mMinimumVelocity;
|
|
52
|
|
|
|
53
|
|
public CupcakeDetector(Context context) {
|
|
54
|
|
final ViewConfiguration configuration = ViewConfiguration.get(context);
|
|
55
|
|
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
|
|
56
|
|
mTouchSlop = configuration.getScaledTouchSlop();
|
|
57
|
|
}
|
|
58
|
|
|
|
59
|
|
private VelocityTracker mVelocityTracker;
|
|
60
|
|
private boolean mIsDragging;
|
|
61
|
|
|
|
62
|
|
float getActiveX(MotionEvent ev) {
|
|
63
|
|
return ev.getX();
|
|
64
|
|
}
|
|
65
|
|
|
|
66
|
|
float getActiveY(MotionEvent ev) {
|
|
67
|
|
return ev.getY();
|
|
68
|
|
}
|
|
69
|
|
|
|
70
|
|
public boolean isScaling() {
|
|
71
|
|
return false;
|
|
72
|
|
}
|
|
73
|
|
|
|
74
|
|
@Override
|
|
75
|
|
public boolean onTouchEvent(MotionEvent ev) {
|
|
76
|
|
switch (ev.getAction()) {
|
|
77
|
|
case MotionEvent.ACTION_DOWN: {
|
|
78
|
|
mVelocityTracker = VelocityTracker.obtain();
|
|
79
|
|
mVelocityTracker.addMovement(ev);
|
|
80
|
|
|
|
81
|
|
mLastTouchX = getActiveX(ev);
|
|
82
|
|
mLastTouchY = getActiveY(ev);
|
|
83
|
|
mIsDragging = false;
|
|
84
|
|
break;
|
|
85
|
|
}
|
|
86
|
|
|
|
87
|
|
case MotionEvent.ACTION_MOVE: {
|
|
88
|
|
final float x = getActiveX(ev);
|
|
89
|
|
final float y = getActiveY(ev);
|
|
90
|
|
final float dx = x - mLastTouchX, dy = y - mLastTouchY;
|
|
91
|
|
|
|
92
|
|
if (!mIsDragging) {
|
|
93
|
|
// Use Pythagoras to see if drag length is larger than
|
|
94
|
|
// touch slop
|
|
95
|
|
mIsDragging = Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
|
|
96
|
|
}
|
|
97
|
|
|
|
98
|
|
if (mIsDragging) {
|
|
99
|
|
mListener.onDrag(dx, dy);
|
|
100
|
|
mLastTouchX = x;
|
|
101
|
|
mLastTouchY = y;
|
|
102
|
|
|
|
103
|
|
if (null != mVelocityTracker) {
|
|
104
|
|
mVelocityTracker.addMovement(ev);
|
|
105
|
|
}
|
|
106
|
|
}
|
|
107
|
|
break;
|
|
108
|
|
}
|
|
109
|
|
|
|
110
|
|
case MotionEvent.ACTION_CANCEL: {
|
|
111
|
|
// Recycle Velocity Tracker
|
|
112
|
|
if (null != mVelocityTracker) {
|
|
113
|
|
mVelocityTracker.recycle();
|
|
114
|
|
mVelocityTracker = null;
|
|
115
|
|
}
|
|
116
|
|
break;
|
|
117
|
|
}
|
|
118
|
|
|
|
119
|
|
case MotionEvent.ACTION_UP: {
|
|
120
|
|
if (mIsDragging) {
|
|
121
|
|
if (null != mVelocityTracker) {
|
|
122
|
|
mLastTouchX = getActiveX(ev);
|
|
123
|
|
mLastTouchY = getActiveY(ev);
|
|
124
|
|
|
|
125
|
|
// Compute velocity within the last 1000ms
|
|
126
|
|
mVelocityTracker.addMovement(ev);
|
|
127
|
|
mVelocityTracker.computeCurrentVelocity(1000);
|
|
128
|
|
|
|
129
|
|
final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker.getYVelocity();
|
|
130
|
|
|
|
131
|
|
// If the velocity is greater than minVelocity, call
|
|
132
|
|
// listener
|
|
133
|
|
if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) {
|
|
134
|
|
mListener.onFling(mLastTouchX, mLastTouchY, -vX, -vY);
|
|
135
|
|
}
|
|
136
|
|
}
|
|
137
|
|
}
|
|
138
|
|
|
|
139
|
|
// Recycle Velocity Tracker
|
|
140
|
|
if (null != mVelocityTracker) {
|
|
141
|
|
mVelocityTracker.recycle();
|
|
142
|
|
mVelocityTracker = null;
|
|
143
|
|
}
|
|
144
|
|
break;
|
|
145
|
|
}
|
|
146
|
|
}
|
|
147
|
|
|
|
148
|
|
return true;
|
|
149
|
|
}
|
|
150
|
|
}
|
|
151
|
|
|
|
152
|
|
@TargetApi(5)
|
|
153
|
|
private static class EclairDetector extends CupcakeDetector {
|
|
154
|
|
private static final int INVALID_POINTER_ID = -1;
|
|
155
|
|
private int mActivePointerId = INVALID_POINTER_ID;
|
|
156
|
|
private int mActivePointerIndex = 0;
|
|
157
|
|
|
|
158
|
|
public EclairDetector(Context context) {
|
|
159
|
|
super(context);
|
|
160
|
|
}
|
|
161
|
|
|
|
162
|
|
@Override
|
|
163
|
|
float getActiveX(MotionEvent ev) {
|
|
164
|
|
try {
|
|
165
|
|
return ev.getX(mActivePointerIndex);
|
|
166
|
|
} catch (Exception e) {
|
|
167
|
|
return ev.getX();
|
|
168
|
|
}
|
|
169
|
|
}
|
|
170
|
|
|
|
171
|
|
@Override
|
|
172
|
|
float getActiveY(MotionEvent ev) {
|
|
173
|
|
try {
|
|
174
|
|
return ev.getY(mActivePointerIndex);
|
|
175
|
|
} catch (Exception e) {
|
|
176
|
|
return ev.getY();
|
|
177
|
|
}
|
|
178
|
|
}
|
|
179
|
|
|
|
180
|
|
@Override
|
|
181
|
|
public boolean onTouchEvent(MotionEvent ev) {
|
|
182
|
|
final int action = ev.getAction();
|
|
183
|
|
switch (action & MotionEvent.ACTION_MASK) {
|
|
184
|
|
case MotionEvent.ACTION_DOWN:
|
|
185
|
|
mActivePointerId = ev.getPointerId(0);
|
|
186
|
|
break;
|
|
187
|
|
case MotionEvent.ACTION_CANCEL:
|
|
188
|
|
case MotionEvent.ACTION_UP:
|
|
189
|
|
mActivePointerId = INVALID_POINTER_ID;
|
|
190
|
|
break;
|
|
191
|
|
case MotionEvent.ACTION_POINTER_UP:
|
|
192
|
|
final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
|
|
193
|
|
final int pointerId = ev.getPointerId(pointerIndex);
|
|
194
|
|
if (pointerId == mActivePointerId) {
|
|
195
|
|
// This was our active pointer going up. Choose a new
|
|
196
|
|
// active pointer and adjust accordingly.
|
|
197
|
|
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
|
|
198
|
|
mActivePointerId = ev.getPointerId(newPointerIndex);
|
|
199
|
|
mLastTouchX = ev.getX(newPointerIndex);
|
|
200
|
|
mLastTouchY = ev.getY(newPointerIndex);
|
|
201
|
|
}
|
|
202
|
|
break;
|
|
203
|
|
}
|
|
204
|
|
|
|
205
|
|
mActivePointerIndex = ev.findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId : 0);
|
|
206
|
|
return super.onTouchEvent(ev);
|
|
207
|
|
}
|
|
208
|
|
}
|
|
209
|
|
|
|
210
|
|
@TargetApi(8)
|
|
211
|
|
private static class FroyoDetector extends EclairDetector {
|
|
212
|
|
|
|
213
|
|
private final ScaleGestureDetector mDetector;
|
|
214
|
|
|
|
215
|
|
// Needs to be an inner class so that we don't hit
|
|
216
|
|
// VerifyError's on API 4.
|
|
217
|
|
private final OnScaleGestureListener mScaleListener = new OnScaleGestureListener() {
|
|
218
|
|
|
|
219
|
|
@Override
|
|
220
|
|
public boolean onScale(ScaleGestureDetector detector) {
|
|
221
|
|
mListener.onScale(detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY());
|
|
222
|
|
return true;
|
|
223
|
|
}
|
|
224
|
|
|
|
225
|
|
@Override
|
|
226
|
|
public boolean onScaleBegin(ScaleGestureDetector detector) {
|
|
227
|
|
return true;
|
|
228
|
|
}
|
|
229
|
|
|
|
230
|
|
@Override
|
|
231
|
|
public void onScaleEnd(ScaleGestureDetector detector) {
|
|
232
|
|
// NO-OP
|
|
233
|
|
}
|
|
234
|
|
};
|
|
235
|
|
|
|
236
|
|
public FroyoDetector(Context context) {
|
|
237
|
|
super(context);
|
|
238
|
|
mDetector = new ScaleGestureDetector(context, mScaleListener);
|
|
239
|
|
}
|
|
240
|
|
|
|
241
|
|
@Override
|
|
242
|
|
public boolean isScaling() {
|
|
243
|
|
return mDetector.isInProgress();
|
|
244
|
|
}
|
|
245
|
|
|
|
246
|
|
@Override
|
|
247
|
|
public boolean onTouchEvent(MotionEvent ev) {
|
|
248
|
|
mDetector.onTouchEvent(ev);
|
|
249
|
|
return super.onTouchEvent(ev);
|
|
250
|
|
}
|
|
251
|
|
|
|
252
|
|
}
|
|
253
|
|
}
|
|
|
@ -1,36 +0,0 @@
|
|
1
|
|
package com.electric.chargingpile.zoom;
|
|
2
|
|
|
|
3
|
|
import android.content.Context;
|
|
4
|
|
import android.util.AttributeSet;
|
|
5
|
|
import android.view.MotionEvent;
|
|
6
|
|
|
|
7
|
|
public class ViewPagerFixed extends android.support.v4.view.ViewPager {
|
|
8
|
|
|
|
9
|
|
public ViewPagerFixed(Context context) {
|
|
10
|
|
super(context);
|
|
11
|
|
}
|
|
12
|
|
|
|
13
|
|
public ViewPagerFixed(Context context, AttributeSet attrs) {
|
|
14
|
|
super(context, attrs);
|
|
15
|
|
}
|
|
16
|
|
|
|
17
|
|
@Override
|
|
18
|
|
public boolean onTouchEvent(MotionEvent ev) {
|
|
19
|
|
try {
|
|
20
|
|
return super.onTouchEvent(ev);
|
|
21
|
|
} catch (IllegalArgumentException ex) {
|
|
22
|
|
ex.printStackTrace();
|
|
23
|
|
}
|
|
24
|
|
return false;
|
|
25
|
|
}
|
|
26
|
|
|
|
27
|
|
@Override
|
|
28
|
|
public boolean onInterceptTouchEvent(MotionEvent ev) {
|
|
29
|
|
try {
|
|
30
|
|
return super.onInterceptTouchEvent(ev);
|
|
31
|
|
} catch (IllegalArgumentException ex) {
|
|
32
|
|
ex.printStackTrace();
|
|
33
|
|
}
|
|
34
|
|
return false;
|
|
35
|
|
}
|
|
36
|
|
}
|