List of usage examples for android.graphics Rect width
public final int width()
From source file:com.example.imac.animationsdemo.ZoomActivity.java
/** * "Zooms" in a thumbnail view by assigning the high resolution image to a hidden "zoomed-in" * image view and animating its bounds to fit the entire activity content area. More * specifically:// w w w . j a v a2 s .c o m * <p/> * <ol> * <li>Assign the high-res image to the hidden "zoomed-in" (expanded) image view.</li> * <li>Calculate the starting and ending bounds for the expanded view.</li> * <li>Animate each of four positioning/sizing properties (X, Y, SCALE_X, SCALE_Y) * simultaneously, from the starting bounds to the ending bounds.</li> * <li>Zoom back out by running the reverse animation on click.</li> * </ol> * * @param thumbView The thumbnail view to zoom in. * @param imageResId The high-resolution version of the image represented by the thumbnail. */ private void zoomImageFromThumb(final View thumbView, int imageResId) { // If there's an animation in progress, cancel it immediately and proceed with this one. if (mCurrentAnimator != null) { mCurrentAnimator.cancel(); } // Load the high-resolution "zoomed-in" image. final ImageView expandedImageView = (ImageView) findViewById(R.id.expanded_image); expandedImageView.setImageResource(imageResId); // Calculate the starting and ending bounds for the zoomed-in image. This step // involves lots of math. Yay, math. final Rect startBounds = new Rect(); final Rect finalBounds = new Rect(); final Point globalOffset = new Point(); // The start bounds are the global visible rectangle of the thumbnail, and the // final bounds are the global visible rectangle of the container view. Also // set the container view's offset as the origin for the bounds, since that's // the origin for the positioning animation properties (X, Y). thumbView.getGlobalVisibleRect(startBounds); //? findViewById(R.id.container).getGlobalVisibleRect(finalBounds, globalOffset); startBounds.offset(-globalOffset.x, -globalOffset.y); finalBounds.offset(-globalOffset.x, -globalOffset.y); // Adjust the start bounds to be the same aspect ratio as the final bounds using the // "center crop" technique. This prevents undesirable stretching during the animation. // Also calculate the start scaling factor (the end scaling factor is always 1.0). float startScale; if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds.width() / startBounds.height()) { // Extend start bounds horizontally startScale = (float) startBounds.height() / finalBounds.height(); float startWidth = startScale * finalBounds.width(); float deltaWidth = (startWidth - startBounds.width()) / 2; startBounds.left -= deltaWidth; startBounds.right += deltaWidth; } else { // Extend start bounds vertically startScale = (float) startBounds.width() / finalBounds.width(); float startHeight = startScale * finalBounds.height(); //?? float deltaHeight = (startHeight - startBounds.height()) / 2; //? ??? 2 ? startBounds.top -= deltaHeight; // startBounds.bottom += deltaHeight; // } // Hide the thumbnail and show the zoomed-in view. When the animation begins, // it will position the zoomed-in view in the place of the thumbnail. thumbView.setAlpha(0f); expandedImageView.setVisibility(View.VISIBLE); // Set the pivot point for SCALE_X and SCALE_Y transformations to the top-left corner of // the zoomed-in view (the default is the center of the view). expandedImageView.setPivotX(0f); expandedImageView.setPivotY(0f); // Construct and run the parallel animation of the four translation and scale properties // (X, Y, SCALE_X, and SCALE_Y). Log.e("==startBounds===", startBounds.left + " " + startBounds.top); AnimatorSet set = new AnimatorSet(); Log.e("=====", startBounds.left + " " + startBounds.top + " " + thumbView.getLeft() + " " + thumbView.getTop()); set.play(ObjectAnimator.ofFloat(expandedImageView, "X", startBounds.left, finalBounds.left)) .with(ObjectAnimator.ofFloat(expandedImageView, "Y", startBounds.top, finalBounds.top)) .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScale, 1f)) .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScale, 1f)); set.setDuration(mShortAnimationDuration); set.setInterpolator(new DecelerateInterpolator()); set.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { mCurrentAnimator = null; } @Override public void onAnimationCancel(Animator animation) { mCurrentAnimator = null; } }); set.start(); mCurrentAnimator = set; // Upon clicking the zoomed-in image, it should zoom back down to the original bounds // and show the thumbnail instead of the expanded image. final float startScaleFinal = startScale; expandedImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (mCurrentAnimator != null) { mCurrentAnimator.cancel(); } // Animate the four positioning/sizing properties in parallel, back to their // original values. AnimatorSet set = new AnimatorSet(); set.play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left)) .with(ObjectAnimator.ofFloat(expandedImageView, View.Y, startBounds.top)) .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScaleFinal)) .with(ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScaleFinal)); set.setDuration(mShortAnimationDuration); set.setInterpolator(new DecelerateInterpolator()); set.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { thumbView.setAlpha(1f); // expandedImageView.setVisibility(View.GONE); mCurrentAnimator = null; } @Override public void onAnimationCancel(Animator animation) { thumbView.setAlpha(1f); // expandedImageView.setVisibility(View.GONE); mCurrentAnimator = null; } }); set.start(); mCurrentAnimator = set; } }); }
From source file:com.ebaonet.lawyer.ui.weight.DraggableGridViewPager.java
private void animateDragged() { if (mLastDragged >= 0) { final View v = getChildAt(mLastDragged); final Rect r = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); r.inset(-r.width() / 20, -r.height() / 20); v.measure(MeasureSpec.makeMeasureSpec(r.width(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(r.height(), MeasureSpec.EXACTLY)); v.layout(r.left, r.top, r.right, r.bottom); AnimationSet animSet = new AnimationSet(true); ScaleAnimation scale = new ScaleAnimation(0.9091f, 1, 0.9091f, 1, v.getWidth() / 2, v.getHeight() / 2); scale.setDuration(ANIMATION_DURATION); AlphaAnimation alpha = new AlphaAnimation(1, .8f); alpha.setDuration(ANIMATION_DURATION); animSet.addAnimation(scale);/*from w w w .ja va 2 s. c o m*/ animSet.addAnimation(alpha); animSet.setFillEnabled(true); animSet.setFillAfter(true); v.clearAnimation(); v.startAnimation(animSet); } }
From source file:com.raibow.yamahaspk.filtershow.imageshow.ImageShow.java
public void drawImageAndAnimate(Canvas canvas, Bitmap image) { if (image == null) { return;//from w w w . j a v a 2s . co m } MasterImage master = MasterImage.getImage(); Matrix m = master.computeImageToScreen(image, 0, false); if (m == null) { return; } canvas.save(); RectF d = new RectF(0, 0, image.getWidth(), image.getHeight()); m.mapRect(d); d.roundOut(mImageBounds); boolean showAnimatedImage = master.onGoingNewLookAnimation(); if (!showAnimatedImage && mDidStartAnimation) { // animation ended, but do we have the correct image to show? if (master.getPreset().equals(master.getCurrentPreset())) { // we do, let's stop showing the animated image mDidStartAnimation = false; MasterImage.getImage().resetAnimBitmap(); } else { showAnimatedImage = true; } } else if (showAnimatedImage) { mDidStartAnimation = true; } if (showAnimatedImage) { canvas.save(); // Animation uses the image before the change Bitmap previousImage = master.getPreviousImage(); Matrix mp = master.computeImageToScreen(previousImage, 0, false); RectF dp = new RectF(0, 0, previousImage.getWidth(), previousImage.getHeight()); mp.mapRect(dp); Rect previousBounds = new Rect(); dp.roundOut(previousBounds); float centerX = dp.centerX(); float centerY = dp.centerY(); boolean needsToDrawImage = true; if (master.getCurrentLookAnimation() == MasterImage.CIRCLE_ANIMATION) { float maskScale = MasterImage.getImage().getMaskScale(); if (maskScale >= 0.0f) { float maskW = sMask.getWidth() / 2.0f; float maskH = sMask.getHeight() / 2.0f; Point point = mActivity.hintTouchPoint(this); float maxMaskScale = 2 * Math.max(getWidth(), getHeight()) / Math.min(maskW, maskH); maskScale = maskScale * maxMaskScale; float x = point.x - maskW * maskScale; float y = point.y - maskH * maskScale; // Prepare the shader mShaderMatrix.reset(); mShaderMatrix.setScale(1.0f / maskScale, 1.0f / maskScale); mShaderMatrix.preTranslate(-x + mImageBounds.left, -y + mImageBounds.top); float scaleImageX = mImageBounds.width() / (float) image.getWidth(); float scaleImageY = mImageBounds.height() / (float) image.getHeight(); mShaderMatrix.preScale(scaleImageX, scaleImageY); mMaskPaint.reset(); mMaskPaint.setShader(createShader(image)); mMaskPaint.getShader().setLocalMatrix(mShaderMatrix); drawShadow(canvas, mImageBounds); // as needed canvas.drawBitmap(previousImage, m, mPaint); canvas.clipRect(mImageBounds); canvas.translate(x, y); canvas.scale(maskScale, maskScale); canvas.drawBitmap(sMask, 0, 0, mMaskPaint); needsToDrawImage = false; } } else if (master.getCurrentLookAnimation() == MasterImage.ROTATE_ANIMATION) { Rect d1 = computeImageBounds(master.getPreviousImage().getHeight(), master.getPreviousImage().getWidth()); Rect d2 = computeImageBounds(master.getPreviousImage().getWidth(), master.getPreviousImage().getHeight()); float finalScale = d1.width() / (float) d2.height(); finalScale = (1.0f * (1.0f - master.getAnimFraction())) + (finalScale * master.getAnimFraction()); canvas.rotate(master.getAnimRotationValue(), centerX, centerY); canvas.scale(finalScale, finalScale, centerX, centerY); } else if (master.getCurrentLookAnimation() == MasterImage.MIRROR_ANIMATION) { if (master.getCurrentFilterRepresentation() instanceof FilterMirrorRepresentation) { FilterMirrorRepresentation rep = (FilterMirrorRepresentation) master .getCurrentFilterRepresentation(); ImagePreset preset = master.getPreset(); ArrayList<FilterRepresentation> geometry = (ArrayList<FilterRepresentation>) preset .getGeometryFilters(); GeometryMathUtils.GeometryHolder holder = null; holder = GeometryMathUtils.unpackGeometry(geometry); if (holder.rotation.value() == 90 || holder.rotation.value() == 270) { if (rep.isHorizontal() && !rep.isVertical()) { canvas.scale(1, master.getAnimRotationValue(), centerX, centerY); } else if (rep.isVertical() && !rep.isHorizontal()) { canvas.scale(1, master.getAnimRotationValue(), centerX, centerY); } else if (rep.isHorizontal() && rep.isVertical()) { canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY); } else { canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY); } } else { if (rep.isHorizontal() && !rep.isVertical()) { canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY); } else if (rep.isVertical() && !rep.isHorizontal()) { canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY); } else if (rep.isHorizontal() && rep.isVertical()) { canvas.scale(1, master.getAnimRotationValue(), centerX, centerY); } else { canvas.scale(1, master.getAnimRotationValue(), centerX, centerY); } } } } if (needsToDrawImage) { drawShadow(canvas, previousBounds); // as needed canvas.drawBitmap(previousImage, mp, mPaint); } canvas.restore(); } else { drawShadow(canvas, mImageBounds); // as needed canvas.drawBitmap(image, m, mPaint); } canvas.restore(); }
From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java
public void drawImageAndAnimate(Canvas canvas, Bitmap image) { if (image == null) { return;/*from w ww . jav a 2 s. c om*/ } MasterImage master = MasterImage.getImage(); Matrix m = master.computeImageToScreen(image, 0, false); if (m == null) { return; } canvas.save(); RectF d = new RectF(0, 0, image.getWidth(), image.getHeight()); m.mapRect(d); d.roundOut(mImageBounds); boolean showAnimatedImage = master.onGoingNewLookAnimation(); if (!showAnimatedImage && mDidStartAnimation) { // animation ended, but do we have the correct image to show? if (master.getPreset().equals(master.getCurrentPreset())) { // we do, let's stop showing the animated image mDidStartAnimation = false; MasterImage.getImage().resetAnimBitmap(); } else { showAnimatedImage = true; } } else if (showAnimatedImage) { mDidStartAnimation = true; } if (showAnimatedImage) { canvas.save(); // Animation uses the image before the change Bitmap previousImage = master.getPreviousImage(); Matrix mp = master.computeImageToScreen(previousImage, 0, false); RectF dp = new RectF(0, 0, previousImage.getWidth(), previousImage.getHeight()); mp.mapRect(dp); Rect previousBounds = new Rect(); dp.roundOut(previousBounds); float centerX = dp.centerX(); float centerY = dp.centerY(); boolean needsToDrawImage = true; if (master.getCurrentLookAnimation() == MasterImage.CIRCLE_ANIMATION) { float maskScale = MasterImage.getImage().getMaskScale(); if (maskScale >= 0.0f) { float maskW = sMask.getWidth() / 2.0f; float maskH = sMask.getHeight() / 2.0f; Point point = mActivity.hintTouchPoint(this); float maxMaskScale = 2 * Math.max(getWidth(), getHeight()) / Math.min(maskW, maskH); maskScale = maskScale * maxMaskScale; float x = point.x - maskW * maskScale; float y = point.y - maskH * maskScale; // Prepare the shader mShaderMatrix.reset(); mShaderMatrix.setScale(1.0f / maskScale, 1.0f / maskScale); mShaderMatrix.preTranslate(-x + mImageBounds.left, -y + mImageBounds.top); float scaleImageX = mImageBounds.width() / (float) image.getWidth(); float scaleImageY = mImageBounds.height() / (float) image.getHeight(); mShaderMatrix.preScale(scaleImageX, scaleImageY); mMaskPaint.reset(); Shader maskShader = createShader(image); maskShader.setLocalMatrix(mShaderMatrix); mMaskPaint.setShader(maskShader); drawShadow(canvas, mImageBounds); // as needed canvas.drawBitmap(previousImage, m, mPaint); canvas.clipRect(mImageBounds); canvas.translate(x, y); canvas.scale(maskScale, maskScale); canvas.drawBitmap(sMask, 0, 0, mMaskPaint); needsToDrawImage = false; } } else if (master.getCurrentLookAnimation() == MasterImage.ROTATE_ANIMATION) { Rect d1 = computeImageBounds(master.getPreviousImage().getHeight(), master.getPreviousImage().getWidth()); Rect d2 = computeImageBounds(master.getPreviousImage().getWidth(), master.getPreviousImage().getHeight()); float finalScale = d1.width() / (float) d2.height(); finalScale = (1.0f * (1.0f - master.getAnimFraction())) + (finalScale * master.getAnimFraction()); canvas.rotate(master.getAnimRotationValue(), centerX, centerY); canvas.scale(finalScale, finalScale, centerX, centerY); } else if (master.getCurrentLookAnimation() == MasterImage.MIRROR_ANIMATION) { if (master.getCurrentFilterRepresentation() instanceof FilterMirrorRepresentation) { FilterMirrorRepresentation rep = (FilterMirrorRepresentation) master .getCurrentFilterRepresentation(); ImagePreset preset = master.getPreset(); ArrayList<FilterRepresentation> geometry = (ArrayList<FilterRepresentation>) preset .getGeometryFilters(); GeometryMathUtils.GeometryHolder holder = null; holder = GeometryMathUtils.unpackGeometry(geometry); if (holder.rotation.value() == 90 || holder.rotation.value() == 270) { if (rep.isHorizontal() && !rep.isVertical()) { canvas.scale(1, master.getAnimRotationValue(), centerX, centerY); } else if (rep.isVertical() && !rep.isHorizontal()) { canvas.scale(1, master.getAnimRotationValue(), centerX, centerY); } else if (rep.isHorizontal() && rep.isVertical()) { canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY); } else { canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY); } } else { if (rep.isHorizontal() && !rep.isVertical()) { canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY); } else if (rep.isVertical() && !rep.isHorizontal()) { canvas.scale(master.getAnimRotationValue(), 1, centerX, centerY); } else if (rep.isHorizontal() && rep.isVertical()) { canvas.scale(1, master.getAnimRotationValue(), centerX, centerY); } else { canvas.scale(1, master.getAnimRotationValue(), centerX, centerY); } } } } if (needsToDrawImage) { drawShadow(canvas, previousBounds); // as needed canvas.drawBitmap(previousImage, mp, mPaint); } canvas.restore(); } else { drawShadow(canvas, mImageBounds); // as needed canvas.drawBitmap(image, m, mPaint); } canvas.restore(); }
From source file:in.sc9.discreteslider.DiscreteSlider.java
private void updateProgressFromAnimation(float scale) { Rect bounds = mThumb.getBounds(); int halfThumb = bounds.width() / 2; int addedThumb = mAddedTouchBounds; int left = getPaddingLeft() + halfThumb + addedThumb; int right = getWidth() - (getPaddingRight() + halfThumb + addedThumb); int available = right - left; int progress = Math.round((scale * (mMax - mMin)) + mMin); //we don't want to just call setProgress here to avoid the animation being cancelled, //and this position is not bound to a real progress value but interpolated notifyProgress(mValue, true);/* w w w. j a va 2s . c om*/ if (progress != getProgress()) { mValue = progress; notifyProgress(mValue, true); updateProgressMessage(progress); } final int thumbPos = (int) (scale * available + 0.5f); updateThumbPos(thumbPos); }
From source file:in.sc9.discreteslider.DiscreteSlider.java
private void updateDragging(MotionEvent ev) { setHotspot(ev.getX(), ev.getY());/*from www .ja v a2s .com*/ int x = (int) ev.getX(); Log.d("eaf", x + ""); Rect oldBounds = mThumb.getBounds(); int halfThumb = oldBounds.width() / 2; int addedThumb = mAddedTouchBounds; int newX = x - mDragOffset + halfThumb; int left = getPaddingLeft() + halfThumb + addedThumb; int right = getWidth() - (getPaddingRight() + halfThumb + addedThumb); if (newX < left) { newX = left; } else if (newX > right) { newX = right; } int available = right - left; float scale = (float) (newX - left) / (float) available; if (isRtl()) { scale = 1f - scale; } int progress = Math.round((scale * (mMax - mMin)) + mMin); setProgress(progress, true); }
From source file:com.android.deskclock.timer.TimerFullScreenFragment.java
private Animator getRevealAnimator(View source, int revealColor) { final ViewGroup containerView = (ViewGroup) source.getRootView().findViewById(android.R.id.content); final Rect sourceBounds = new Rect(0, 0, source.getHeight(), source.getWidth()); containerView.offsetDescendantRectToMyCoords(source, sourceBounds); final int centerX = sourceBounds.centerX(); final int centerY = sourceBounds.centerY(); final int xMax = Math.max(centerX, containerView.getWidth() - centerX); final int yMax = Math.max(centerY, containerView.getHeight() - centerY); final float startRadius = Math.max(sourceBounds.width(), sourceBounds.height()) / 2.0f; final float endRadius = (float) Math.sqrt(xMax * xMax + yMax * yMax); final CircleView revealView = new CircleView(source.getContext()).setCenterX(centerX).setCenterY(centerY) .setFillColor(revealColor);// ww w . j a va2 s .co m containerView.addView(revealView); final Animator revealAnimator = ObjectAnimator.ofFloat(revealView, CircleView.RADIUS, startRadius, endRadius); revealAnimator.setInterpolator(PathInterpolatorCompat.create(0.0f, 0.0f, 0.2f, 1.0f)); final ValueAnimator fadeAnimator = ObjectAnimator.ofFloat(revealView, View.ALPHA, 0.0f); fadeAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { containerView.removeView(revealView); } }); final AnimatorSet animatorSet = new AnimatorSet(); animatorSet.setDuration(TimerFragment.ANIMATION_TIME_MILLIS); animatorSet.playSequentially(revealAnimator, fadeAnimator); return revealAnimator; }
From source file:com.android.hcframe.DraggableGridViewPager.java
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int childCount = getChildCount(); mPageCount = (childCount + mPageSize - 1) / mPageSize; mGridWidth = (getWidth() - mPaddingLeft - mPaddingRight - (mColCount - 1) * mGridGap) / mColCount; mGridHeight = (getHeight() - mPaddingTop - mPaddingButtom - (mRowCount - 1) * mGridGap) / mRowCount; // mGridWidth = mGridHeight = Math.min(mGridWidth, mGridHeight); mMaxOverScrollSize = mGridWidth / 2; mEdgeSize = mGridWidth / 2;/*from w ww .j av a 2s .c om*/ newPositions.clear(); for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); final Rect rect = getRectByPosition(i); child.measure(MeasureSpec.makeMeasureSpec(rect.width(), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(rect.height(), MeasureSpec.EXACTLY)); child.layout(rect.left, rect.top, rect.right, rect.bottom); newPositions.add(-1); } if (mCurItem > 0 && mCurItem < mPageCount) { final int curItem = mCurItem; mCurItem = 0; setCurrentItem(curItem); } }
From source file:uk.co.senab.photoview.PhotoViewAttacher.java
public void renderBitmap() { Log.d("RenderBitmap", "Rendering bitmap!!!"); ImageView iv = getImageView(); if (iv != null) { RectF rect = getDisplayRect();/*from w ww. ja va 2 s. co m*/ if (rect == null || rect.isEmpty()) { rect = new RectF(0, 0, originalBitmapSize.x, originalBitmapSize.y); } float ratioX = originalBitmapSize.x / (float) iv.getWidth(); float ratioY = originalBitmapSize.y / (float) iv.getHeight(); rect.set(rect.left * ratioX, rect.top * ratioY, rect.right * ratioX, rect.bottom * ratioY); Rect bounds = new Rect(); rect.round(bounds); Bitmap bitmap = Bitmap.createBitmap(originalBitmapSize.x, originalBitmapSize.y, Bitmap.Config.ARGB_8888); pdfiumCore.renderPageBitmap(pdfDocument, bitmap, pageIndex, bounds.left, bounds.top, bounds.width(), bounds.height()); iv.setImageBitmap(bitmap); } }
From source file:com.android.hcframe.DraggableGridViewPager.java
private int getTargetByXY(int x, int y) { final int position = getPositionByXY(x, y); if (position < 0) { return -1; }/* w ww .j ava2 s . com*/ final Rect r = getRectByPosition(position); final int page = position / mPageSize; r.inset(r.width() / 4, r.height() / 4); r.offset(-getWidth() * page, 0); if (!r.contains(x, y)) { return -1; } return position; }