List of usage examples for android.graphics RectF width
public final float width()
From source file:de.j4velin.picturechooser.crop.CropFragment.java
@Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { View v = inflater.inflate(R.layout.crop, null); DisplayMetrics metrics = new DisplayMetrics(); ((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay() .getMetrics(metrics);/* w ww . j a v a2 s. com*/ int availableHeight = metrics.heightPixels; int availableWidth = metrics.widthPixels; TypedValue tv = new TypedValue(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { if (getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) availableHeight -= TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()); } int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) availableHeight -= getResources().getDimensionPixelSize(resourceId); final float[] imgDetails = new float[3]; ImageView iv = (ImageView) v.findViewById(R.id.image); iv.setImageBitmap(ImageLoader.decode(getArguments().getString("imgPath"), availableWidth, availableHeight, imgDetails)); float imageViewWidth = imgDetails[0]; float imageViewHeight = imgDetails[1]; if (imageViewWidth > availableWidth || imageViewHeight > availableHeight) { while (imageViewWidth > availableWidth || imageViewHeight > availableHeight) { imageViewWidth *= 0.99f; imageViewHeight *= 0.99f; } } else { while (imageViewWidth < availableWidth && imageViewHeight < availableHeight) { imageViewWidth *= 1.01f; imageViewHeight *= 1.01f; } } float spareWidth = availableWidth - imageViewWidth; float spareHeight = availableHeight - imageViewHeight; final CropView cv = (CropView) v.findViewById(R.id.crop); final RectF imagePosition = new RectF(); imagePosition.left = spareWidth / 2; imagePosition.top = spareHeight / 2; imagePosition.right = imagePosition.left + imageViewWidth; imagePosition.bottom = imagePosition.top + imageViewHeight; cv.setImagePosition(imagePosition); cv.setScale(imgDetails[0] / imagePosition.width()); cv.setAspect(getArguments().getFloat("aspect", 0)); v.findViewById(R.id.save).setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View view) { new SaveTask((Main) getActivity(), cv, imagePosition).execute(getArguments().getString("imgPath")); } }); return v; }
From source file:lib.picturechooser.crop.CropFragment.java
@Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) { View v = inflater.inflate(R.layout.crop, null); DisplayMetrics metrics = new DisplayMetrics(); ((WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay() .getMetrics(metrics);//ww w . j ava2 s. c om int availableHeight = metrics.heightPixels; int availableWidth = metrics.widthPixels; TypedValue tv = new TypedValue(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { if (getActivity().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) availableHeight -= TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics()); } int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) availableHeight -= getResources().getDimensionPixelSize(resourceId); final float[] imgDetails = new float[3]; ImageView iv = (ImageView) v.findViewById(R.id.image); iv.setImageBitmap(ImageLoader.decode(getArguments().getString("imgPath"), availableWidth, availableHeight, imgDetails)); float imageViewWidth = imgDetails[0]; float imageViewHeight = imgDetails[1]; if (imageViewWidth > availableWidth || imageViewHeight > availableHeight) { while (imageViewWidth > availableWidth || imageViewHeight > availableHeight) { imageViewWidth *= 0.99f; imageViewHeight *= 0.99f; } } else { while (imageViewWidth < availableWidth && imageViewHeight < availableHeight) { imageViewWidth *= 1.01f; imageViewHeight *= 1.01f; } } float spareWidth = availableWidth - imageViewWidth; float spareHeight = availableHeight - imageViewHeight; final CropView cv = (CropView) v.findViewById(R.id.crop); final RectF imagePosition = new RectF(); imagePosition.left = spareWidth / 2; imagePosition.top = spareHeight / 2; imagePosition.right = imagePosition.left + imageViewWidth; imagePosition.bottom = imagePosition.top + imageViewHeight; cv.setImagePosition(imagePosition); cv.setScale(imgDetails[0] / imagePosition.width()); cv.setAspect(getArguments().getFloat("aspect", 0)); v.findViewById(R.id.save).setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View view) { new SaveTask((SelectPictureActivity) getActivity(), cv, imagePosition) .execute(getArguments().getString("imgPath")); } }); return v; }
From source file:com.example.gatsu.theevent.HighlightView.java
void growBy(float dx, float dy) { if (mMaintainAspectRatio) { if (dx != 0) { dy = dx / mInitialAspectRatio; } else if (dy != 0) { dx = dy * mInitialAspectRatio; }//w ww. j a v a2 s. co m } // Don't let the cropping rectangle grow too fast. // Grow at most half of the difference between the image rectangle and // the cropping rectangle. RectF r = new RectF(mCropRect); if (dx > 0F && r.width() + 2 * dx > mImageRect.width()) { float adjustment = (mImageRect.width() - r.width()) / 2F; dx = adjustment; if (mMaintainAspectRatio) { dy = dx / mInitialAspectRatio; } } if (dy > 0F && r.height() + 2 * dy > mImageRect.height()) { float adjustment = (mImageRect.height() - r.height()) / 2F; dy = adjustment; if (mMaintainAspectRatio) { dx = dy * mInitialAspectRatio; } } r.inset(-dx, -dy); // Don't let the cropping rectangle shrink too fast. final float widthCap = 25F; if (r.width() < widthCap) { r.inset(-(widthCap - r.width()) / 2F, 0F); } float heightCap = mMaintainAspectRatio ? (widthCap / mInitialAspectRatio) : widthCap; if (r.height() < heightCap) { r.inset(0F, -(heightCap - r.height()) / 2F); } // Put the cropping rectangle inside the image rectangle. if (r.left < mImageRect.left) { r.offset(mImageRect.left - r.left, 0F); } else if (r.right > mImageRect.right) { r.offset(-(r.right - mImageRect.right), 0); } if (r.top < mImageRect.top) { r.offset(0F, mImageRect.top - r.top); } else if (r.bottom > mImageRect.bottom) { r.offset(0F, -(r.bottom - mImageRect.bottom)); } mCropRect.set(r); mDrawRect = computeLayout(); mContext.invalidate(); }
From source file:org.caojun.library.cropimage.HighlightView.java
void growBy(float dx, float dy) { if (mMaintainAspectRatio) { if (dx != 0) { dy = dx / mInitialAspectRatio; } else if (dy != 0) { dx = dy * mInitialAspectRatio; }//from w w w. j ava 2 s . co m } // Don't let the cropping rectangle grow too fast. // Grow at most half of the difference between the image rectangle and // the cropping rectangle. RectF r = new RectF(mCropRect); if (dx > 0F && r.width() + 2 * dx > mImageRect.width()) { float adjustment = (mImageRect.width() - r.width()) / 2F; dx = adjustment; if (mMaintainAspectRatio) { dy = dx / mInitialAspectRatio; } } if (dy > 0F && r.height() + 2 * dy > mImageRect.height()) { float adjustment = (mImageRect.height() - r.height()) / 2F; dy = adjustment; if (mMaintainAspectRatio) { dx = dy * mInitialAspectRatio; } } r.inset(-dx, -dy); // Don't let the cropping rectangle shrink too fast. final float widthCap = 25F; if (r.width() < widthCap) { r.inset(-(widthCap - r.width()) / 2F, 0F); } float heightCap = mMaintainAspectRatio ? (widthCap / mInitialAspectRatio) : widthCap; if (r.height() < heightCap) { r.inset(0F, -(heightCap - r.height()) / 2F); } // Put the cropping rectangle inside the image rectangle. if (r.left < mImageRect.left) { r.offset(mImageRect.left - r.left, 0F); } else if (r.right > mImageRect.right) { r.offset(-(r.right - mImageRect.right), 0); } if (r.top < mImageRect.top) { r.offset(0F, mImageRect.top - r.top); } else if (r.bottom > mImageRect.bottom) { r.offset(0F, -(r.bottom - mImageRect.bottom)); } mCropRect.set(r); mDrawRect = computeLayout(); mView.invalidate(); }
From source file:com.apptentive.android.sdk.util.image.PreviewImageView.java
/** * Prevent visual artifact when scaling/*from w w w . ja v a2 s.c o m*/ */ private void checkBorderAndCenterWhenScale() { RectF rect = getMatrixRectF(); float deltaX = 0; float deltaY = 0; int width = getWidth(); int height = getHeight(); if (rect.width() >= width) { if (rect.left > 0) { deltaX = -rect.left; } if (rect.right < width) { deltaX = width - rect.right; } } if (rect.height() >= height) { if (rect.top > 0) { deltaY = -rect.top; } if (rect.bottom < height) { deltaY = height - rect.bottom; } } // Always center the image when it's smaller than the imageView if (rect.width() < width) { deltaX = width * 0.5f - rect.right + 0.5f * rect.width(); } if (rect.height() < height) { deltaY = height * 0.5f - rect.bottom + 0.5f * rect.height(); } scaleMatrix.postTranslate(deltaX, deltaY); }
From source file:com.apptentive.android.sdk.util.image.PreviewImageView.java
@Override public boolean onTouch(View v, MotionEvent event) { gestureDetector.onTouchEvent(event); scaleGestureDetector.onTouchEvent(event); float x = 0, y = 0; // Get multiple touch points final int pointerCount = event.getPointerCount(); // Calculate average x and y for (int i = 0; i < pointerCount; i++) { x += event.getX(i);//from w w w .j a va 2 s . c om y += event.getY(i); } x = x / pointerCount; y = y / pointerCount; /** * Reset lastX and lastY */ if (pointerCount != lastPointerCount) { isCanDrag = false; lastX = x; lastY = y; } lastPointerCount = pointerCount; switch (event.getAction()) { case MotionEvent.ACTION_MOVE: float dx = x - lastX; float dy = y - lastY; if (!isCanDrag) { isCanDrag = isCanDrag(dx, dy); } if (isCanDrag) { RectF rectF = getMatrixRectF(); if (getDrawable() != null) { isCheckLeftAndRight = isCheckTopAndBottom = true; // No left/right translation if image width is less than screen width if (rectF.width() < getWidth()) { dx = 0; isCheckLeftAndRight = false; } // No Up/Down translation if image height is less than screen height if (rectF.height() < getHeight()) { dy = 0; isCheckTopAndBottom = false; } scaleMatrix.postTranslate(dx, dy); checkMatrixBounds(); setImageMatrix(scaleMatrix); } } lastX = x; lastY = y; break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: lastPointerCount = 0; break; } return true; }
From source file:org.stockchart.core.Area.java
public RectF getSideMargins() { RectF r = new RectF(); r.left = getLeftAxis().getSize(0f);/*from w w w .ja va 2 s. co m*/ r.right = getRightAxis().getSize(0f); r.top = getTopAxis().getSize(0f); r.bottom = getBottomAxis().getSize(0f); if (fLegend.isVisible()) { RectF size = fLegend.getSize(); switch (fLegend.getSide()) { case LEFT: r.left += size.width(); break; case RIGHT: r.right += size.width(); break; case BOTTOM: r.bottom += size.height(); break; case TOP: r.top += size.height(); break; } } return r; }
From source file:de.vanita5.twittnuker.view.ShapedImageView.java
/** * Given the source bitmap and a canvas, draws the bitmap through a circular * mask. Only draws a circle with diameter equal to the destination width. * * @param bitmap The source bitmap to draw. * @param canvas The canvas to draw it on. * @param source The source bound of the bitmap. * @param dest The destination bound on the canvas. *//* w w w . j av a 2 s . c o m*/ public void drawBitmapWithCircleOnCanvas(Bitmap bitmap, Canvas canvas, RectF source, @NonNull RectF dest) { if (bitmap == null) { if (getStyle() == SHAPE_CIRCLE) { canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f, mSolidColorPaint); } else { final float cornerRadius = getCalculatedCornerRadius(); canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mSolidColorPaint); } return; } // Draw bitmap through shader first. final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mMatrix.reset(); switch (getScaleType()) { case CENTER_CROP: { final float srcRatio = source.width() / source.height(); final float dstRatio = dest.width() / dest.height(); if (srcRatio > dstRatio) { // Source is wider than destination, fit height mTempDestination.top = dest.top; mTempDestination.bottom = dest.bottom; final float dstWidth = dest.height() * srcRatio; mTempDestination.left = dest.centerX() - dstWidth / 2; mTempDestination.right = dest.centerX() + dstWidth / 2; } else if (srcRatio < dstRatio) { mTempDestination.left = dest.left; mTempDestination.right = dest.right; final float dstHeight = dest.width() / srcRatio; mTempDestination.top = dest.centerY() - dstHeight / 2; mTempDestination.bottom = dest.centerY() + dstHeight / 2; } else { mTempDestination.set(dest); } break; } default: { mTempDestination.set(dest); break; } } // Fit bitmap to bounds. mMatrix.setRectToRect(source, mTempDestination, ScaleToFit.CENTER); shader.setLocalMatrix(mMatrix); mBitmapPaint.setShader(shader); if (getStyle() == SHAPE_CIRCLE) { canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f, mBitmapPaint); } else { final float cornerRadius = getCalculatedCornerRadius(); canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint); } }
From source file:org.getlantern.firetweet.view.ShapedImageView.java
/** * Given the source bitmap and a canvas, draws the bitmap through a circular * mask. Only draws a circle with diameter equal to the destination width. * * @param bitmap The source bitmap to draw. * @param canvas The canvas to draw it on. * @param source The source bound of the bitmap. * @param dest The destination bound on the canvas. *///from ww w .j a v a2s . co m public void drawBitmapWithCircleOnCanvas(Bitmap bitmap, Canvas canvas, RectF source, @NonNull RectF dest) { if (bitmap == null) { if (getStyle() == SHAPE_CIRCLE) { canvas.drawCircle(dest.centerX(), dest.centerY(), Math.min(dest.width(), dest.height()) / 2f, mSolidColorPaint); } else { final float cornerRadius = getCalculatedCornerRadius(); canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mSolidColorPaint); } return; } // Draw bitmap through shader first. final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); mMatrix.reset(); switch (getScaleType()) { case CENTER_CROP: { final float srcRatio = source.width() / source.height(); final float dstRatio = dest.width() / dest.height(); if (srcRatio > dstRatio) { // Source is wider than destination, fit height mTempDestination.top = dest.top; mTempDestination.bottom = dest.bottom; final float dstWidth = dest.height() * srcRatio; mTempDestination.left = dest.centerX() - dstWidth / 2; mTempDestination.right = dest.centerX() + dstWidth / 2; } else if (srcRatio < dstRatio) { mTempDestination.left = dest.left; mTempDestination.right = dest.right; final float dstHeight = dest.width() / srcRatio; mTempDestination.top = dest.centerY() - dstHeight / 2; mTempDestination.bottom = dest.centerY() + dstHeight / 2; } else { mTempDestination.set(dest); } break; } default: { mTempDestination.set(dest); break; } } // Fit bitmap to bounds. mMatrix.setRectToRect(source, mTempDestination, ScaleToFit.CENTER); shader.setLocalMatrix(mMatrix); mBitmapPaint.setShader(shader); if (mBorderEnabled) { final float inset = mBorderPaint.getStrokeWidth() / 2; if (getStyle() == SHAPE_CIRCLE) { final float circleRadius = Math.min(dest.width(), dest.height()) / 2f - inset / 2; canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBitmapPaint); } else { final float cornerRadius = getCalculatedCornerRadius(); dest.inset(inset, inset); canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint); dest.inset(-inset, -inset); } } else { if (getStyle() == SHAPE_CIRCLE) { final float circleRadius = Math.min(dest.width(), dest.height()) / 2f; canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBitmapPaint); } else { final float cornerRadius = getCalculatedCornerRadius(); canvas.drawRoundRect(dest, cornerRadius, cornerRadius, mBitmapPaint); } } }
From source file:de.vanita5.twittnuker.view.ShapedImageView.java
private void drawBorder(@NonNull final Canvas canvas) { final RectF transitionSrc = mTransitionSource, transitionDst = mTransitionDestination; if (transitionSrc != null && transitionDst != null) { final float progress = 1 - (mDestination.width() - transitionDst.width()) / (transitionSrc.width() - transitionDst.width()); mBorderPaint.setStrokeWidth(mStrokeWidth * progress); mBorderPaint.setAlpha(Math.round(mBorderAlpha * progress)); ViewCompat.setTranslationZ(this, -ViewCompat.getElevation(this) * (1 - progress)); } else {//from w w w.j av a 2s. co m mBorderPaint.setStrokeWidth(mStrokeWidth); mBorderPaint.setAlpha(mBorderAlpha); ViewCompat.setTranslationZ(this, 0); } if (getStyle() == SHAPE_CIRCLE) { canvas.drawCircle(mDestination.centerX(), mDestination.centerY(), mDestination.width() / 2f - mBorderPaint.getStrokeWidth() / 2, mBorderPaint); } else { final float radius = getCalculatedCornerRadius(); canvas.drawRoundRect(mDestination, radius, radius, mBorderPaint); } }