Java Code Examples for android.util.FloatMath#sqrt()

The following examples show how to use android.util.FloatMath#sqrt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TwoFingerGestureDetector.java    From OpenMapKitAndroid with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Return the current distance between the two pointers forming the
 * gesture in progress.
 *
 * @return Distance between pointers in pixels.
 */
public float getCurrentSpan() {
    if (mCurrLen == -1) {
        final float cvx = mCurrFingerDiffX;
        final float cvy = mCurrFingerDiffY;
        mCurrLen = FloatMath.sqrt(cvx * cvx + cvy * cvy);
    }
    return mCurrLen;
}
 
Example 2
Source File: BitmapUtils.java    From android-open-project-demo with Apache License 2.0 5 votes vote down vote up
public static BitmapSize getScaledSize(int originalWidth, int originalHeight, int numPixels) {
	float ratio = (float)originalWidth/originalHeight;
	
	int scaledHeight = (int)FloatMath.sqrt((float)numPixels/ratio);
	int scaledWidth = (int)(ratio * FloatMath.sqrt((float)numPixels/ratio));
			
	return new BitmapSize(scaledWidth, scaledHeight);
}
 
Example 3
Source File: Shake2Share.java    From ShareSDKShareDifMsgDemo-Android with MIT License 5 votes vote down vote up
public void onSensorChanged(SensorEvent event) {
	long currentTime = System.currentTimeMillis();
	long diffTime = currentTime - mLastUpdateTime;
	if (diffTime > UPDATE_INTERVAL) {
		if(mLastUpdateTime != 0) {
			float x = event.values[0];
			float y = event.values[1];
			float z = event.values[2];
			float deltaX = x - mLastX;
			float deltaY = y - mLastY;
			float deltaZ = z - mLastZ;
			float delta = FloatMath.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) / diffTime * 10000;
			if (delta > SHAKE_THRESHOLD) {
				if (!shaken) {
					shaken = true;
					finish();
				}

				if (listener != null) {
					listener.onShake();
				}
			}
			mLastX = x;
			mLastY = y;
			mLastZ = z;
		}
		mLastUpdateTime = currentTime;
	}
}
 
Example 4
Source File: PicViewer.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
/**
    * 两点的距离
    */
   @SuppressLint("FloatMath")
private float spacing(MotionEvent event) {
       float x = event.getX(0) - event.getX(1);
       float y = event.getY(0) - event.getY(1);
       return FloatMath.sqrt(x * x + y * y);
   }
 
Example 5
Source File: TwoFingerGestureDetector.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Return the current distance between the two pointers forming the
 * gesture in progress.
 * 
 * @return Distance between pointers in pixels.
 */
public float getCurrentSpan() {
    if (mCurrLen == -1) {
        final float cvx = mCurrFingerDiffX;
        final float cvy = mCurrFingerDiffY;
        mCurrLen = FloatMath.sqrt(cvx*cvx + cvy*cvy);
    }
    return mCurrLen;
}
 
Example 6
Source File: Shake2Share.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public void onSensorChanged(SensorEvent event) {
	long currentTime = System.currentTimeMillis();
	long diffTime = currentTime - mLastUpdateTime;
	if (diffTime > UPDATE_INTERVAL) {
		if(mLastUpdateTime != 0) {
			float x = event.values[0];
			float y = event.values[1];
			float z = event.values[2];
			float deltaX = x - mLastX;
			float deltaY = y - mLastY;
			float deltaZ = z - mLastZ;
			float delta = FloatMath.sqrt(deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ) / diffTime * 10000;
			if (delta > SHAKE_THRESHOLD) {
				if (!shaken) {
					shaken = true;
					finish();
				}

				if (listener != null) {
					listener.onShake();
				}
			}
			mLastX = x;
			mLastY = y;
			mLastZ = z;
		}
		mLastUpdateTime = currentTime;
	}
}
 
Example 7
Source File: PLMath.java    From PanoramaGL with Apache License 2.0 4 votes vote down vote up
/**distance methods*/

public static float distanceBetweenPoints(CGPoint point1, CGPoint point2)
{
	return FloatMath.sqrt(((point2.x - point1.x) * (point2.x - point1.x)) + ((point2.y - point1.y) * (point2.y - point1.y)));
}
 
Example 8
Source File: EaseCircularIn.java    From 30-android-libraries-in-30-days with Apache License 2.0 4 votes vote down vote up
public static float getValue(final float pPercentage) {
	return -(FloatMath.sqrt(1 - pPercentage * pPercentage) - 1.0f);
}
 
Example 9
Source File: PhotoViewAttacher.java    From light-novel-library_Wenku8_Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public float getScale() {
    return FloatMath.sqrt((float) Math.pow(getValue(mSuppMatrix, Matrix.MSCALE_X), 2) + (float) Math.pow(getValue(mSuppMatrix, Matrix.MSKEW_Y), 2));
}
 
Example 10
Source File: VersionedGestureDetector.java    From WifiChat with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            mVelocityTracker = VelocityTracker.obtain();
            mVelocityTracker.addMovement(ev);

            mLastTouchX = getActiveX(ev);
            mLastTouchY = getActiveY(ev);
            mIsDragging = false;
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final float x = getActiveX(ev);
            final float y = getActiveY(ev);
            final float dx = x - mLastTouchX, dy = y - mLastTouchY;

            if (!mIsDragging) {
                // Use Pythagoras to see if drag length is larger than
                // touch slop
                mIsDragging = FloatMath.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
            }

            if (mIsDragging) {
                mListener.onDrag(dx, dy);
                mLastTouchX = x;
                mLastTouchY = y;

                if (null != mVelocityTracker) {
                    mVelocityTracker.addMovement(ev);
                }
            }
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            // Recycle Velocity Tracker
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        }

        case MotionEvent.ACTION_UP: {
            if (mIsDragging) {
                if (null != mVelocityTracker) {
                    mLastTouchX = getActiveX(ev);
                    mLastTouchY = getActiveY(ev);

                    // Compute velocity within the last 1000ms
                    mVelocityTracker.addMovement(ev);
                    mVelocityTracker.computeCurrentVelocity(1000);

                    final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker
                            .getYVelocity();

                    // If the velocity is greater than minVelocity, call
                    // listener
                    if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) {
                        mListener.onFling(mLastTouchX, mLastTouchY, -vX, -vY);
                    }
                }
            }

            // Recycle Velocity Tracker
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        }
    }

    return true;
}
 
Example 11
Source File: CropPhotoViewAttacher.java    From ImageSelector with Apache License 2.0 4 votes vote down vote up
@Override
public float getScale() {
 return FloatMath.sqrt((float) Math.pow(getValue(mSuppMatrix, Matrix.MSCALE_X), 2) + (float) Math.pow(getValue(mSuppMatrix, Matrix.MSKEW_Y), 2));
}
 
Example 12
Source File: Scroller.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
/**
 * Start scrolling based on a fling gesture. The distance travelled will
 * depend on the initial velocity of the fling.
 * 
 * @param startX Starting point of the scroll (X)
 * @param startY Starting point of the scroll (Y)
 * @param velocityX Initial velocity of the fling (X) measured in pixels per
 *        second.
 * @param velocityY Initial velocity of the fling (Y) measured in pixels per
 *        second
 * @param minX Minimum X value. The scroller will not scroll past this
 *        point.
 * @param maxX Maximum X value. The scroller will not scroll past this
 *        point.
 * @param minY Minimum Y value. The scroller will not scroll past this
 *        point.
 * @param maxY Maximum Y value. The scroller will not scroll past this
 *        point.
 */
public void fling(int startX, int startY, int velocityX, int velocityY,
        int minX, int maxX, int minY, int maxY) {
    // Continue a scroll or fling in progress
    if (mFlywheel && !mFinished) {
        float oldVel = getCurrVelocity();

        float dx = (float) (mFinalX - mStartX);
        float dy = (float) (mFinalY - mStartY);
        float hyp = FloatMath.sqrt(dx * dx + dy * dy);

        float ndx = dx / hyp;
        float ndy = dy / hyp;

        float oldVelocityX = ndx * oldVel;
        float oldVelocityY = ndy * oldVel;
        if (Math.signum(velocityX) == Math.signum(oldVelocityX) &&
                Math.signum(velocityY) == Math.signum(oldVelocityY)) {
            velocityX += oldVelocityX;
            velocityY += oldVelocityY;
        }
    }

    mMode = FLING_MODE;
    mFinished = false;

    float velocity = FloatMath.sqrt(velocityX * velocityX + velocityY * velocityY);
 
    mVelocity = velocity;
    final double l = Math.log(START_TENSION * velocity / ALPHA);
    mDuration = (int) (1000.0 * Math.exp(l / (DECELERATION_RATE - 1.0)));
    mStartTime = AnimationUtils.currentAnimationTimeMillis();
    mStartX = startX;
    mStartY = startY;

    float coeffX = velocity == 0 ? 1.0f : velocityX / velocity;
    float coeffY = velocity == 0 ? 1.0f : velocityY / velocity;

    int totalDistance =
            (int) (ALPHA * Math.exp(DECELERATION_RATE / (DECELERATION_RATE - 1.0) * l));
    
    mMinX = minX;
    mMaxX = maxX;
    mMinY = minY;
    mMaxY = maxY;

    mFinalX = startX + Math.round(totalDistance * coeffX);
    // Pin to mMinX <= mFinalX <= mMaxX
    mFinalX = Math.min(mFinalX, mMaxX);
    mFinalX = Math.max(mFinalX, mMinX);
    
    mFinalY = startY + Math.round(totalDistance * coeffY);
    // Pin to mMinY <= mFinalY <= mMaxY
    mFinalY = Math.min(mFinalY, mMaxY);
    mFinalY = Math.max(mFinalY, mMinY);
}
 
Example 13
Source File: Scroller.java    From zen4android with MIT License 4 votes vote down vote up
/**
 * Start scrolling based on a fling gesture. The distance travelled will
 * depend on the initial velocity of the fling.
 * 
 * @param startX Starting point of the scroll (X)
 * @param startY Starting point of the scroll (Y)
 * @param velocityX Initial velocity of the fling (X) measured in pixels per
 *        second.
 * @param velocityY Initial velocity of the fling (Y) measured in pixels per
 *        second
 * @param minX Minimum X value. The scroller will not scroll past this
 *        point.
 * @param maxX Maximum X value. The scroller will not scroll past this
 *        point.
 * @param minY Minimum Y value. The scroller will not scroll past this
 *        point.
 * @param maxY Maximum Y value. The scroller will not scroll past this
 *        point.
 */
public void fling(int startX, int startY, int velocityX, int velocityY,
        int minX, int maxX, int minY, int maxY) {
    // Continue a scroll or fling in progress
    if (mFlywheel && !mFinished) {
        float oldVel = getCurrVelocity();

        float dx = (float) (mFinalX - mStartX);
        float dy = (float) (mFinalY - mStartY);
        float hyp = FloatMath.sqrt(dx * dx + dy * dy);

        float ndx = dx / hyp;
        float ndy = dy / hyp;

        float oldVelocityX = ndx * oldVel;
        float oldVelocityY = ndy * oldVel;
        if (Math.signum(velocityX) == Math.signum(oldVelocityX) &&
                Math.signum(velocityY) == Math.signum(oldVelocityY)) {
            velocityX += oldVelocityX;
            velocityY += oldVelocityY;
        }
    }

    mMode = FLING_MODE;
    mFinished = false;

    float velocity = FloatMath.sqrt(velocityX * velocityX + velocityY * velocityY);
 
    mVelocity = velocity;
    final double l = Math.log(START_TENSION * velocity / ALPHA);
    mDuration = (int) (1000.0 * Math.exp(l / (DECELERATION_RATE - 1.0)));
    mStartTime = AnimationUtils.currentAnimationTimeMillis();
    mStartX = startX;
    mStartY = startY;

    float coeffX = velocity == 0 ? 1.0f : velocityX / velocity;
    float coeffY = velocity == 0 ? 1.0f : velocityY / velocity;

    int totalDistance =
            (int) (ALPHA * Math.exp(DECELERATION_RATE / (DECELERATION_RATE - 1.0) * l));
    
    mMinX = minX;
    mMaxX = maxX;
    mMinY = minY;
    mMaxY = maxY;

    mFinalX = startX + Math.round(totalDistance * coeffX);
    // Pin to mMinX <= mFinalX <= mMaxX
    mFinalX = Math.min(mFinalX, mMaxX);
    mFinalX = Math.max(mFinalX, mMinX);
    
    mFinalY = startY + Math.round(totalDistance * coeffY);
    // Pin to mMinY <= mFinalY <= mMaxY
    mFinalY = Math.min(mFinalY, mMaxY);
    mFinalY = Math.max(mFinalY, mMinY);
}
 
Example 14
Source File: PhotoViewAttacher.java    From Tweetin with Apache License 2.0 4 votes vote down vote up
@Override
public float getScale() {
    return FloatMath.sqrt((float) Math.pow(getValue(mSuppMatrix, Matrix.MSCALE_X), 2) + (float) Math.pow(getValue(mSuppMatrix, Matrix.MSKEW_Y), 2));
}
 
Example 15
Source File: ScaleImageView.java    From ImageChoose with MIT License 4 votes vote down vote up
private float distance(float x0, float x1, float y0, float y1) {
    float x = x0 - x1;
    float y = y0 - y1;
    return FloatMath.sqrt(x * x + y * y);
}
 
Example 16
Source File: Scroller.java    From WayHoo with Apache License 2.0 4 votes vote down vote up
/**
 * Start scrolling based on a fling gesture. The distance travelled will
 * depend on the initial velocity of the fling.
 *
 * @param startX Starting point of the scroll (X)
 * @param startY Starting point of the scroll (Y)
 * @param velocityX Initial velocity of the fling (X) measured in pixels per
 *        second.
 * @param velocityY Initial velocity of the fling (Y) measured in pixels per
 *        second
 * @param minX Minimum X value. The scroller will not scroll past this
 *        point.
 * @param maxX Maximum X value. The scroller will not scroll past this
 *        point.
 * @param minY Minimum Y value. The scroller will not scroll past this
 *        point.
 * @param maxY Maximum Y value. The scroller will not scroll past this
 *        point.
 */
public void fling(int startX, int startY, int velocityX, int velocityY,
        int minX, int maxX, int minY, int maxY) {
    // Continue a scroll or fling in progress
    if (mFlywheel && !mFinished) {
        float oldVel = getCurrVelocity();

        float dx = (float) (mFinalX - mStartX);
        float dy = (float) (mFinalY - mStartY);
        float hyp = FloatMath.sqrt(dx * dx + dy * dy);

        float ndx = dx / hyp;
        float ndy = dy / hyp;

        float oldVelocityX = ndx * oldVel;
        float oldVelocityY = ndy * oldVel;
        if (Math.signum(velocityX) == Math.signum(oldVelocityX)
                && Math.signum(velocityY) == Math.signum(oldVelocityY)) {
            velocityX += oldVelocityX;
            velocityY += oldVelocityY;
        }
    }

    mMode = FLING_MODE;
    mFinished = false;

    float velocity = FloatMath.sqrt(velocityX * velocityX + velocityY * velocityY);

    mVelocity = velocity;
    final double l = Math.log(START_TENSION * velocity / ALPHA);
    mDuration = (int) (1000.0 * Math.exp(l / (DECELERATION_RATE - 1.0)));
    mStartTime = AnimationUtils.currentAnimationTimeMillis();
    mStartX = startX;
    mStartY = startY;

    float coeffX = velocity == 0 ? 1.0f : velocityX / velocity;
    float coeffY = velocity == 0 ? 1.0f : velocityY / velocity;

    int totalDistance =
            (int) (ALPHA * Math.exp(DECELERATION_RATE / (DECELERATION_RATE - 1.0) * l));

    mMinX = minX;
    mMaxX = maxX;
    mMinY = minY;
    mMaxY = maxY;

    mFinalX = startX + Math.round(totalDistance * coeffX);
    // Pin to mMinX <= mFinalX <= mMaxX
    mFinalX = Math.min(mFinalX, mMaxX);
    mFinalX = Math.max(mFinalX, mMinX);

    mFinalY = startY + Math.round(totalDistance * coeffY);
    // Pin to mMinY <= mFinalY <= mMaxY
    mFinalY = Math.min(mFinalY, mMaxY);
    mFinalY = Math.max(mFinalY, mMinY);
}
 
Example 17
Source File: PhotoViewAttacher.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
@Override
public float getScale() {
    return FloatMath.sqrt((float) Math.pow(getValue(mSuppMatrix, Matrix.MSCALE_X), 2) + (float) Math.pow(getValue(mSuppMatrix, Matrix.MSKEW_Y), 2));
}
 
Example 18
Source File: CupcakeGestureDetector.java    From narrate-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN: {
            mVelocityTracker = VelocityTracker.obtain();
            if (null != mVelocityTracker) {
                mVelocityTracker.addMovement(ev);
            } else {
                Log.i(LOG_TAG, "Velocity tracker is null");
            }

            mLastTouchX = getActiveX(ev);
            mLastTouchY = getActiveY(ev);
            mIsDragging = false;
            break;
        }

        case MotionEvent.ACTION_MOVE: {
            final float x = getActiveX(ev);
            final float y = getActiveY(ev);
            final float dx = x - mLastTouchX, dy = y - mLastTouchY;

            if (!mIsDragging) {
                // Use Pythagoras to see if drag length is larger than
                // touch slop
                mIsDragging = FloatMath.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
            }

            if (mIsDragging) {
                mListener.onDrag(dx, dy);
                mLastTouchX = x;
                mLastTouchY = y;

                if (null != mVelocityTracker) {
                    mVelocityTracker.addMovement(ev);
                }
            }
            break;
        }

        case MotionEvent.ACTION_CANCEL: {
            // Recycle Velocity Tracker
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        }

        case MotionEvent.ACTION_UP: {
            if (mIsDragging) {
                if (null != mVelocityTracker) {
                    mLastTouchX = getActiveX(ev);
                    mLastTouchY = getActiveY(ev);

                    // Compute velocity within the last 1000ms
                    mVelocityTracker.addMovement(ev);
                    mVelocityTracker.computeCurrentVelocity(1000);

                    final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker
                            .getYVelocity();

                    // If the velocity is greater than minVelocity, call
                    // listener
                    if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) {
                        mListener.onFling(mLastTouchX, mLastTouchY, -vX,
                                -vY);
                    }
                }
            }

            // Recycle Velocity Tracker
            if (null != mVelocityTracker) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
            break;
        }
    }

    return true;
}
 
Example 19
Source File: OverScroller.java    From Klyph with MIT License 4 votes vote down vote up
/**
 * Returns the absolute value of the current velocity.
 *
 * @return The original velocity less the deceleration, norm of the X and Y velocity vector.
 */
public float getCurrVelocity() {
    float squaredNorm = mScrollerX.mCurrVelocity * mScrollerX.mCurrVelocity;
    squaredNorm += mScrollerY.mCurrVelocity * mScrollerY.mCurrVelocity;
    return FloatMath.sqrt(squaredNorm);
}
 
Example 20
Source File: PointF.java    From letv with Apache License 2.0 4 votes vote down vote up
public static float length(float x, float y) {
    return FloatMath.sqrt((x * x) + (y * y));
}