Skip to content

Commit

Permalink
优化手势识别类TouchGestureDetector,ScaleGestureDetector使用Api27源码
Browse files Browse the repository at this point in the history
  • Loading branch information
huangziwei committed Aug 1, 2018
1 parent d1c38c2 commit 3920cb0
Show file tree
Hide file tree
Showing 7 changed files with 636 additions and 48 deletions.
1 change: 1 addition & 0 deletions AndroidsDemo/res/layout/activity_touchgesturedetector.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<com.example.androidsdemo.view.EasyPaintView
android:id="@+id/easy_paint_view"
android:layout_width="match_parent"
android:background="#88888888"
android:layout_height="match_parent"/>

<TextView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.Toast;

import com.example.androidsdemo.view.EasyPaintView;

import cn.forward.androids.ScaleGestureDetectorApi27;
import cn.forward.androids.TouchGestureDetector;

/**
Expand Down Expand Up @@ -85,20 +85,25 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d


@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
public boolean onScaleBegin(ScaleGestureDetectorApi27 detector) {
Log.d(TAG, "onScaleBegin: ");
mLastFocusX = null;
mLastFocusY = null;
return true;
}

@Override
public void onScaleEnd(ScaleGestureDetector detector) {
public void onScaleEnd(ScaleGestureDetectorApi27 detector) {
Log.d(TAG, "onScaleEnd: ");
float scale = 1f;
mTargetView.setScaleX(scale);
mTargetView.setScaleY(scale);
mTargetView.setTranslationX(0);
mTargetView.setTranslationY(0);
}

@Override
public boolean onScale(ScaleGestureDetector detector) {
public boolean onScale(ScaleGestureDetectorApi27 detector) {
Log.d(TAG, "onScale: ");
// 屏幕上的焦点
mTouchCentreX = detector.getFocusX();
Expand All @@ -109,8 +114,8 @@ public boolean onScale(ScaleGestureDetector detector) {
final float dy = mTouchCentreY - mLastFocusY;
// 移动图片

mTargetView.setTranslationX(mTargetView.getTranslationX() + dx);
mTargetView.setTranslationY(mTargetView.getTranslationY() + dy);
// mTargetView.setTranslationX(mTargetView.getTranslationX() + dx);
// mTargetView.setTranslationY(mTargetView.getTranslationY() + dy);
}

// 缩放图片
Expand All @@ -134,13 +139,14 @@ public boolean onScale(ScaleGestureDetector detector) {

mTouchGestureDetector.setIsScrollAfterScaled(false);

findViewById(R.id.container).setOnTouchListener(new View.OnTouchListener() {
mEasyPaintView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return mTouchGestureDetector.onTouchEvent(event);
}
});

mEasyPaintView.setScaleX(0.5f);
mEasyPaintView.setScaleY(0.5f);
}

}
4 changes: 2 additions & 2 deletions androids/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionCode 15
versionName "1.2.1"
versionCode 16
versionName "1.2.2"
}
buildTypes {
release {
Expand Down
5 changes: 5 additions & 0 deletions androids/res/values-sw600dp/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Use a larger scaling span for larger screen devices. -->
<dimen name="androids_api27_config_minScalingSpan">32mm</dimen>
</resources>
14 changes: 14 additions & 0 deletions androids/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Minimum span needed to begin a touch scaling gesture.
If the span is equal to or greater than this size, a scaling gesture
will begin, where supported. (See android.view.ScaleGestureDetectorApi27)
This also takes into account the size of any active touch points.
Devices with screens that deviate too far from their assigned density
bucket should consider tuning this value in a device-specific overlay.
For best results, care should be taken such that this value remains
larger than the minimum reported touchMajor/touchMinor values
reported by the hardware. -->
<dimen name="androids_api27_config_minScalingSpan">27mm</dimen>
</resources>
Loading

0 comments on commit 3920cb0

Please sign in to comment.