android.util.FloatMath Java Examples

The following examples show how to use android.util.FloatMath. 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: Cocos2dxBitmap.java    From Earlybird with Apache License 2.0 6 votes vote down vote up
private static TextProperty computeTextProperty(final String string,
		final int width, final int height, final Paint paint) {
	final FontMetricsInt fm = paint.getFontMetricsInt();
	final int h = (int) Math.ceil(fm.bottom - fm.top);
	int maxContentWidth = 0;

	final String[] lines = Cocos2dxBitmap.splitString(string, width,
			height, paint);

	if (width != 0) {
		maxContentWidth = width;
	} else {
		/* Compute the max width. */
		int temp = 0;
		for (final String line : lines) {
			temp = (int) FloatMath.ceil(paint.measureText(line, 0,
					line.length()));
			if (temp > maxContentWidth) {
				maxContentWidth = temp;
			}
		}
	}

	return new TextProperty(maxContentWidth, h, lines);
}
 
Example #2
Source File: LinePageIndicator.java    From android-open-project-demo with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the height of this view
 *
 * @param measureSpec
 *            A measureSpec packed into an int
 * @return The height of the view, honoring constraints from measureSpec
 */
private int measureHeight(int measureSpec) {
    float result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY) {
        //We were told how big to be
        result = specSize;
    } else {
        //Measure the height
        result = mPaintSelected.getStrokeWidth() + getPaddingTop() + getPaddingBottom();
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int)FloatMath.ceil(result);
}
 
Example #3
Source File: LinePageIndicator.java    From barterli_android with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the width of this view
 *
 * @param measureSpec
 *            A measureSpec packed into an int
 * @return The width of the view, honoring constraints from measureSpec
 */
private int measureWidth(int measureSpec) {
    float result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
        //We were told how big to be
        result = specSize;
    } else {
        //Calculate the width according the views count
        final int count = mViewPager.getAdapter().getCount();
        result = getPaddingLeft() + getPaddingRight() + (count * mLineWidth) + ((count - 1) * mGapWidth);
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int)FloatMath.ceil(result);
}
 
Example #4
Source File: LinePageIndicator.java    From Atomic with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Determines the width of this view
 *
 * @param measureSpec
 *            A measureSpec packed into an int
 * @return The width of the view, honoring constraints from measureSpec
 */
private int measureWidth(int measureSpec) {
    float result = 0;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
        //We were told how big to be
        result = specSize;
    } else {
        //Calculate the width according the views count
        final int count = mViewPager.getAdapter().getCount();
        result = getPaddingLeft() + getPaddingRight() + (count * mLineWidth) + ((count - 1) * mGapWidth);
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int)FloatMath.ceil(result);
}
 
Example #5
Source File: LinePageIndicator.java    From WayHoo with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the width of this view
 *
 * @param measureSpec
 *            A measureSpec packed into an int
 * @return The width of the view, honoring constraints from measureSpec
 */
private int measureWidth(int measureSpec) {
    float result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
        //We were told how big to be
        result = specSize;
    } else {
        //Calculate the width according the views count
        final int count = mViewPager.getAdapter().getCount();
        result = getPaddingLeft() + getPaddingRight() + (count * mLineWidth) + ((count - 1) * mGapWidth);
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int)FloatMath.ceil(result);
}
 
Example #6
Source File: LinePageIndicator.java    From BigApp_WordPress_Android with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the height of this view
 *
 * @param measureSpec
 *            A measureSpec packed into an int
 * @return The height of the view, honoring constraints from measureSpec
 */
private int measureHeight(int measureSpec) {
    float result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY) {
        //We were told how big to be
        result = specSize;
    } else {
        //Measure the height
        result = mPaintSelected.getStrokeWidth() + getPaddingTop() + getPaddingBottom();
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int)FloatMath.ceil(result);
}
 
Example #7
Source File: BitmapUtils.java    From android-open-project-demo with Apache License 2.0 6 votes vote down vote up
public static Bitmap getSampledBitmap(String filePath, int reqWidth, int reqHeight) {		
	Options options = new Options();
	options.inJustDecodeBounds = true;
	
	BitmapFactory.decodeFile(filePath, options);
	
	// Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = (int)FloatMath.floor(((float)height / reqHeight)+0.5f); //Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = (int)FloatMath.floor(((float)width / reqWidth)+0.5f); //Math.round((float)width / (float)reqWidth);
        }
    }
    
    options.inSampleSize = inSampleSize;
    options.inJustDecodeBounds = false;
    	    
    return BitmapFactory.decodeFile(filePath, options);
}
 
Example #8
Source File: LinePageIndicator.java    From narrate-android with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the width of this view
 *
 * @param measureSpec
 *            A measureSpec packed into an int
 * @return The width of the view, honoring constraints from measureSpec
 */
private int measureWidth(int measureSpec) {
    float result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
        //We were told how big to be
        result = specSize;
    } else {
        //Calculate the width according the views count
        final int count = mViewPager.getAdapter().getCount();
        result = getPaddingLeft() + getPaddingRight() + (count * mLineWidth) + ((count - 1) * mGapWidth);
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int)FloatMath.ceil(result);
}
 
Example #9
Source File: Cocos2dxBitmap.java    From Example-of-Cocos2DX with MIT License 6 votes vote down vote up
private static TextProperty computeTextProperty(final String string,
		final int width, final int height, final Paint paint) {
	final FontMetricsInt fm = paint.getFontMetricsInt();
	final int h = (int) Math.ceil(fm.bottom - fm.top);
	int maxContentWidth = 0;

	final String[] lines = Cocos2dxBitmap.splitString(string, width,
			height, paint);

	if (width != 0) {
		maxContentWidth = width;
	} else {
		/* Compute the max width. */
		int temp = 0;
		for (final String line : lines) {
			temp = (int) FloatMath.ceil(paint.measureText(line, 0,
					line.length()));
			if (temp > maxContentWidth) {
				maxContentWidth = temp;
			}
		}
	}

	return new TextProperty(maxContentWidth, h, lines);
}
 
Example #10
Source File: LinePageIndicator.java    From KlyphMessenger with MIT License 6 votes vote down vote up
/**
 * Determines the height of this view
 *
 * @param measureSpec
 *            A measureSpec packed into an int
 * @return The height of the view, honoring constraints from measureSpec
 */
private int measureHeight(int measureSpec) {
    float result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY) {
        //We were told how big to be
        result = specSize;
    } else {
        //Measure the height
        result = mPaintSelected.getStrokeWidth() + getPaddingTop() + getPaddingBottom();
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int)FloatMath.ceil(result);
}
 
Example #11
Source File: LinePageIndicator.java    From KlyphMessenger with MIT License 6 votes vote down vote up
/**
 * Determines the width of this view
 *
 * @param measureSpec
 *            A measureSpec packed into an int
 * @return The width of the view, honoring constraints from measureSpec
 */
private int measureWidth(int measureSpec) {
    float result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
        //We were told how big to be
        result = specSize;
    } else {
        //Calculate the width according the views count
        final int count = mViewPager.getAdapter().getCount();
        result = getPaddingLeft() + getPaddingRight() + (count * mLineWidth) + ((count - 1) * mGapWidth);
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int)FloatMath.ceil(result);
}
 
Example #12
Source File: LinePageIndicator.java    From AndroidCacheFoundation with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the width of this view
 *
 * @param measureSpec
 *            A measureSpec packed into an int
 * @return The width of the view, honoring constraints from measureSpec
 */
private int measureWidth(int measureSpec) {
    float result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
        //We were told how big to be
        result = specSize;
    } else {
        //Calculate the width according the views count
        final int count = mViewPager.getAdapter().getCount();
        result = getPaddingLeft() + getPaddingRight() + (count * mLineWidth) + ((count - 1) * mGapWidth);
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int)FloatMath.ceil(result);
}
 
Example #13
Source File: LinePageIndicator.java    From WayHoo with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the height of this view
 *
 * @param measureSpec
 *            A measureSpec packed into an int
 * @return The height of the view, honoring constraints from measureSpec
 */
private int measureHeight(int measureSpec) {
    float result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if (specMode == MeasureSpec.EXACTLY) {
        //We were told how big to be
        result = specSize;
    } else {
        //Measure the height
        result = mPaintSelected.getStrokeWidth() + getPaddingTop() + getPaddingBottom();
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int)FloatMath.ceil(result);
}
 
Example #14
Source File: LinePageIndicator.java    From android-discourse with Apache License 2.0 6 votes vote down vote up
/**
 * Determines the width of this view
 *
 * @param measureSpec A measureSpec packed into an int
 * @return The width of the view, honoring constraints from measureSpec
 */
private int measureWidth(int measureSpec) {
    float result;
    int specMode = MeasureSpec.getMode(measureSpec);
    int specSize = MeasureSpec.getSize(measureSpec);

    if ((specMode == MeasureSpec.EXACTLY) || (mViewPager == null)) {
        //We were told how big to be
        result = specSize;
    } else {
        //Calculate the width according the views count
        final int count = mViewPager.getAdapter().getCount();
        result = getPaddingLeft() + getPaddingRight() + (count * mLineWidth) + ((count - 1) * mGapWidth);
        //Respect AT_MOST value if that was what is called for by measureSpec
        if (specMode == MeasureSpec.AT_MOST) {
            result = Math.min(result, specSize);
        }
    }
    return (int) FloatMath.ceil(result);
}
 
Example #15
Source File: MathUtils.java    From mobile-manager-tool with MIT License 5 votes vote down vote up
/**
 * Rotates p1 around p2 by angle degrees.
 * @param p1
 * @param p2
 * @param angle
 */
public void rotate(PointF p1, PointF p2, float angle) {
	float px = p1.x;
	float py = p1.y;
	float ox = p2.x;
	float oy = p2.y;
	p1.x = (FloatMath.cos(angle) * (px-ox) - FloatMath.sin(angle) * (py-oy) + ox);
	p1.y = (FloatMath.sin(angle) * (px-ox) + FloatMath.cos(angle) * (py-oy) + oy);
}
 
Example #16
Source File: PathModifier.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
public float getSegmentLength(final int pSegmentIndex) {
	final float[] coordinatesX = this.mXs;
	final float[] coordinatesY = this.mYs;

	final int nextSegmentIndex = pSegmentIndex + 1;

	final float dx = coordinatesX[pSegmentIndex] - coordinatesX[nextSegmentIndex];
	final float dy = coordinatesY[pSegmentIndex] - coordinatesY[nextSegmentIndex];

	return FloatMath.sqrt(dx * dx + dy * dy);
}
 
Example #17
Source File: AmbilWarnaPrefWidgetView.java    From Pi-Locker with GNU General Public License v2.0 5 votes vote down vote up
public AmbilWarnaPrefWidgetView(Context context, AttributeSet attrs) {
	super(context, attrs);
	
	float density = context.getResources().getDisplayMetrics().density;
	rectSize = FloatMath.floor(24.f * density + 0.5f);
	strokeWidth = FloatMath.floor(1.f * density + 0.5f);

	paint = new Paint();
	paint.setColor(0xffffffff);
	paint.setStyle(Style.STROKE);
	paint.setStrokeWidth(strokeWidth);
}
 
Example #18
Source File: BitmapUtils.java    From LB-Launcher with Apache License 2.0 5 votes vote down vote up
public static int computeSampleSizeLarger(float scale) {
    int initialSize = (int) FloatMath.floor(1f / scale);
    if (initialSize <= 1) return 1;

    return initialSize <= 8
            ? Utils.prevPowerOf2(initialSize)
            : initialSize / 8 * 8;
}
 
Example #19
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 #20
Source File: TriangleFeature.java    From geoar-app with Apache License 2.0 5 votes vote down vote up
@Override
	public void onCreateInGLESThread() {
		
		final int numOfVertices = (numBorderPoints+2) * 3;
		final int numOfColors = (numBorderPoints+2) * 4;
//		final int numOfTexCoords = (numBorderPoints+2) * 2;
		
		final float[] vertices = new float[numOfVertices];
		final float[] colors = new float[numOfColors];
		final float[] normals = new float[numOfVertices];
//		final float[] texCoords = new float[numOfTexCoords];
		
		for (int i = 3, point = 0; i < numOfVertices; i += 3, point++) {
			float radians = (float) Math.toRadians(360.f - point * 360.f / numBorderPoints);
			vertices[i] 	= FloatMath.cos(radians);
			vertices[i + 2] = FloatMath.sin(radians);

			normals[i + 1] = 1.0f;
		}
		
		/** set color coordinates - first point is in the middle */
		colors[0] = 1.0f;
		colors[3] = 1.0f;
		for (int i = 4; i < numOfColors; i += 4) {
			colors[i] = 1.0f;
			colors[i + 3] = 0.1f;
		}
		
		setRenderObjectives(vertices, colors, normals, null);
	}
 
Example #21
Source File: Shake2Share.java    From AndroidLinkup with GNU General Public License v2.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 #22
Source File: b.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private float a(MotionEvent motionevent)
{
    if (motionevent.getPointerCount() < 2)
    {
        return 0.0F;
    } else
    {
        float f1 = motionevent.getX(0) - motionevent.getX(1);
        float f2 = motionevent.getY(0) - motionevent.getY(1);
        return FloatMath.sqrt(f1 * f1 + f2 * f2);
    }
}
 
Example #23
Source File: Shake2Share.java    From Huochexing12306 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 #24
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 #25
Source File: MathUtils.java    From 30-android-libraries-in-30-days with Apache License 2.0 5 votes vote down vote up
public static float[] rotateAroundCenter(final float[] pVertices, final float pRotation, final float pRotationCenterX, final float pRotationCenterY) {
	if(pRotation != 0) {
		final float rotationRad = MathUtils.degToRad(pRotation);
		final float sinRotationRad = FloatMath.sin(rotationRad);
		final float cosRotationInRad = FloatMath.cos(rotationRad);

		for(int i = pVertices.length - 2; i >= 0; i -= 2) {
			final float pX = pVertices[i];
			final float pY = pVertices[i + 1];
			pVertices[i] = pRotationCenterX + (cosRotationInRad * (pX - pRotationCenterX) - sinRotationRad * (pY - pRotationCenterY));
			pVertices[i + 1] = pRotationCenterY + (sinRotationRad * (pX - pRotationCenterX) + cosRotationInRad * (pY - pRotationCenterY));
		}
	}
	return pVertices;
}
 
Example #26
Source File: PLHotspot.java    From PanoramaGL with Apache License 2.0 5 votes vote down vote up
/**calculate methods*/

protected PLPosition convertPitchAndYawToPosition(float pitch, float yaw)
{
	float r = this.getZ(), pr = (90.0f - pitch) * PLConstants.kToRadians, yr = -yaw * PLConstants.kToRadians;
	float x = r * FloatMath.sin(pr) * FloatMath.cos(yr);
	float y = r * FloatMath.sin(pr) * FloatMath.sin(yr);
	float z = r * FloatMath.cos(pr);
	return PLPosition.PLPositionMake(y, z, x);
}
 
Example #27
Source File: PLVector3.java    From PanoramaGL with Apache License 2.0 5 votes vote down vote up
public void normalize()
{
	float magnitude = (x * x + y * y + z * z);
	if (magnitude == 0)
		return;
	float mult = (1.0f / FloatMath.sqrt(magnitude));            
	x *= mult;
	y *= mult;
	z *= mult;
}
 
Example #28
Source File: VersionedGestureDetector.java    From school_shop with MIT License 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 #29
Source File: PullToRefreshWebView.java    From iSCAU-Android with GNU General Public License v3.0 4 votes vote down vote up
private int getScrollRange() {
	return (int) Math.max(0, FloatMath.floor(mRefreshableView.getContentHeight() * mRefreshableView.getScale())
			- (getHeight() - getPaddingBottom() - getPaddingTop()));
}
 
Example #30
Source File: CupcakeGestureDetector.java    From android-project-wo2b 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;
}