Example usage for android.graphics Rect centerX

List of usage examples for android.graphics Rect centerX

Introduction

In this page you can find the example usage for android.graphics Rect centerX.

Prototype

public final int centerX() 

Source Link

Usage

From source file:com.cyan.widget.refreshlayout.SwipeProgressBar.java

void draw(Canvas canvas) {
    Rect bounds = mBounds;
    final int width = bounds.width();
    final int height = bounds.height();
    final int cx = bounds.centerX();
    final int cy = bounds.centerY();
    boolean drawTriggerWhileFinishing = false;
    int restoreCount = canvas.save();
    canvas.clipRect(bounds);//from   w ww. j a  v a  2s .  com

    if (mRunning || (mFinishTime > 0)) {
        long now = AnimationUtils.currentAnimationTimeMillis();
        long elapsed = (now - mStartTime) % ANIMATION_DURATION_MS;
        long iterations = (now - mStartTime) / ANIMATION_DURATION_MS;
        float rawProgress = (elapsed / (ANIMATION_DURATION_MS / 100f));

        // If we're not running anymore, that means we're running through
        // the finish animation.
        if (!mRunning) {
            // If the finish animation is done, don't draw anything, and
            // don't repost.
            if ((now - mFinishTime) >= FINISH_ANIMATION_DURATION_MS) {
                mFinishTime = 0;
                return;
            }

            // Otherwise, use a 0 opacity alpha layer to clear the animation
            // from the inside out. This layer will prevent the circles from
            // drawing within its bounds.
            long finishElapsed = (now - mFinishTime) % FINISH_ANIMATION_DURATION_MS;
            float finishProgress = (finishElapsed / (FINISH_ANIMATION_DURATION_MS / 100f));
            float pct = (finishProgress / 100f);
            // Radius of the circle is half of the screen.
            float clearRadius = width / 2 * INTERPOLATOR.getInterpolation(pct);
            mClipRect.set(cx - clearRadius, 0, cx + clearRadius, height);
            canvas.saveLayerAlpha(mClipRect, 0, 0);
            // Only draw the trigger if there is a space in the center of
            // this refreshing view that needs to be filled in by the
            // trigger. If the progress view is just still animating, let it
            // continue animating.
            drawTriggerWhileFinishing = true;
        }

        // First fill in with the last color that would have finished drawing.
        if (iterations == 0) {
            canvas.drawColor(mColor1);
        } else {
            if (rawProgress >= 0 && rawProgress < 25) {
                canvas.drawColor(mColor4);
            } else if (rawProgress >= 25 && rawProgress < 50) {
                canvas.drawColor(mColor1);
            } else if (rawProgress >= 50 && rawProgress < 75) {
                canvas.drawColor(mColor2);
            } else {
                canvas.drawColor(mColor3);
            }
        }

        // Then draw up to 4 overlapping concentric circles of varying radii, based on how far
        // along we are in the cycle.
        // progress 0-50 draw mColor2
        // progress 25-75 draw mColor3
        // progress 50-100 draw mColor4
        // progress 75 (wrap to 25) draw mColor1
        if ((rawProgress >= 0 && rawProgress <= 25)) {
            float pct = (((rawProgress + 25) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor1, pct);
        }
        if (rawProgress >= 0 && rawProgress <= 50) {
            float pct = ((rawProgress * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor2, pct);
        }
        if (rawProgress >= 25 && rawProgress <= 75) {
            float pct = (((rawProgress - 25) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor3, pct);
        }
        if (rawProgress >= 50 && rawProgress <= 100) {
            float pct = (((rawProgress - 50) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor4, pct);
        }
        if ((rawProgress >= 75 && rawProgress <= 100)) {
            float pct = (((rawProgress - 75) * 2) / 100f);
            drawCircle(canvas, cx, cy, mColor1, pct);
        }
        if (mTriggerPercentage > 0 && drawTriggerWhileFinishing) {
            // There is some portion of trigger to draw. Restore the canvas,
            // then draw the trigger. Otherwise, the trigger does not appear
            // until after the bar has finished animating and appears to
            // just jump in at a larger width than expected.
            canvas.restoreToCount(restoreCount);
            restoreCount = canvas.save();
            canvas.clipRect(bounds);
            drawTrigger(canvas, cx, cy);
        }
        // Keep running until we finish out the last cycle.
        ViewCompat.postInvalidateOnAnimation(mParent, bounds.left, bounds.top, bounds.right, bounds.bottom);
    } else {
        // Otherwise if we're in the middle of a trigger, draw that.
        if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {
            drawTrigger(canvas, cx, cy);
        }
    }
    canvas.restoreToCount(restoreCount);
}

From source file:io.github.sin3hz.wifispinnerview.WifiSpinnerDrawable.java

@Override
protected void onBoundsChange(Rect bounds) {
    super.onBoundsChange(bounds);
    if (mBounds == null) {
        mBounds = new Rect();
    }/*from w  ww.j  a va 2  s.co  m*/
    int size = Math.min(bounds.height(), bounds.width());
    mBounds.left = bounds.centerX() - size / 2;
    mBounds.right = mBounds.left + size;
    mBounds.top = bounds.centerY() - size / 2;
    mBounds.bottom = mBounds.top + size;
    configureBounds();
}

From source file:andoridhost.imczy.com.activitymaterial.custom.ChangePosition.java

@Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
    if (null == startValues || null == endValues) {
        return null;
    }/*from  ww  w .  ja  v a2s  .c om*/

    if (startValues.view.getId() > 0) {
        Rect startRect = (Rect) startValues.values.get(PROPNAME_POSITION);
        Rect endRect = (Rect) endValues.values.get(PROPNAME_POSITION);

        final View view = endValues.view;

        Log.e(TAG, "createAnimator: startRect = " + startRect + " , endRect = " + endRect);

        Path changePosPath = getPathMotion().getPath(startRect.centerX(), startRect.centerY(),
                endRect.centerX(), endRect.centerY());

        int radius = startRect.centerY() - endRect.centerY();

        ObjectAnimator objectAnimator = ObjectAnimator.ofObject(view,
                new PropPosition(PointF.class, "position", new PointF(endRect.centerX(), endRect.centerY())),
                null, changePosPath);
        objectAnimator.setInterpolator(new FastOutSlowInInterpolator());

        return objectAnimator;
    }
    return null;

}

From source file:andoridhost.imczy.com.activitymaterial.custom.ReturnChangePosition.java

@Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
    if (null == startValues || null == endValues) {
        return null;
    }//from ww  w.ja v  a 2s.com

    if (startValues.view.getId() > 0) {
        Rect startRect = (Rect) startValues.values.get(PROPNAME_POSITION);
        Rect endRect = (Rect) endValues.values.get(PROPNAME_POSITION);

        final View view = endValues.view;

        Log.e(TAG, "createAnimator: startRect = " + startRect + " , endRect = " + endRect);

        Path changePosPath = getPathMotion().getPath(startRect.centerX(), startRect.centerY(),
                endRect.centerX(), endRect.centerY() - endRect.height() / 2);

        int radius = startRect.centerY() - endRect.centerY();

        Log.e(TAG, "createAnimator: startRect center x = " + startRect.centerX() + " , centerY= "
                + startRect.centerY());
        Log.w(TAG, "createAnimator: end rect  center x = " + endRect.centerX() + " , centerY= "
                + endRect.centerY());

        ObjectAnimator objectAnimator = ObjectAnimator.ofObject(view, new PropPosition(PointF.class, "position",
                new PointF(startRect.centerX(), startRect.centerY())), null, changePosPath);
        objectAnimator.setInterpolator(new FastOutSlowInInterpolator());

        return objectAnimator;
    }
    return null;

}

From source file:de.uni_weimar.m18.anatomiederstadt.element.SliderFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_slider, container, false);
    SeekBar sb = (SeekBar) view.findViewById(R.id.seekBar);
    // scale mMin and mMax
    mMin = (int) (mMin / mGranularity);
    mMax = (int) (mMax / mGranularity);
    sb.setMax(mMax - mMin);/*from   w w  w.j av a2  s . c o m*/
    sb.setProgress((mMax - mMin) / 2);
    final RelativeLayout balloon = (RelativeLayout) view.findViewById(R.id.indicatorBalloon);
    final Space space = (Space) view.findViewById(R.id.balloonSpace);

    final Context context = getActivity();
    final TextView balloonText = (TextView) view.findViewById(R.id.indicatorTextView);
    final float[] value = { 0.0f };
    sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            p.addRule(RelativeLayout.ABOVE, seekBar.getId());
            Rect thumbRect = seekBar.getThumb().getBounds();
            value[0] = mMin + progress * mGranularity;
            String valueString = new DecimalFormat("#.##").format(value[0]);
            balloonText.setText(String.valueOf(valueString) + " " + mSuffix);
            int balloonWidth = balloonText.getWidth();
            p.setMargins(thumbRect.centerX() - balloonWidth / 2, 0, 0, 0);
            balloon.setLayoutParams(p);
            balloon.setVisibility(View.VISIBLE);

            final Animation animFadeOut = AnimationUtils.loadAnimation(context, R.anim.fade_out);
            balloon.startAnimation(animFadeOut);
            animFadeOut.setStartOffset(1000);
            animFadeOut.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    balloon.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            LevelStateManager stateManager = ((AnatomieDerStadtApplication) getActivity()
                    .getApplicationContext()).getStateManager(getActivity());
            stateManager.saveFloat(mVar, value[0]);
        }
    });
    return view;
}

From source file:com.android.camera.HighlightView.java

public int getHit(float x, float y) {
    Rect r = computeLayout();
    final float hysteresis = 20F;
    int retval = GROW_NONE;

    if (mCircle) {
        float distX = x - r.centerX();
        float distY = y - r.centerY();
        int distanceFromCenter = (int) Math.sqrt(distX * distX + distY * distY);
        int radius = mDrawRect.width() / 2;
        int delta = distanceFromCenter - radius;
        if (Math.abs(delta) <= hysteresis) {
            if (Math.abs(distY) > Math.abs(distX)) {
                if (distY < 0) {
                    retval = GROW_TOP_EDGE;
                } else {
                    retval = GROW_BOTTOM_EDGE;
                }//  ww w . j a v a 2s. c o m
            } else {
                if (distX < 0) {
                    retval = GROW_LEFT_EDGE;
                } else {
                    retval = GROW_RIGHT_EDGE;
                }
            }
        } else if (distanceFromCenter < radius) {
            retval = MOVE;
        } else {
            retval = GROW_NONE;
        }
    } else {
        // verticalCheck makes sure the position is between the top and
        // the bottom edge (with some tolerance). Similar for horizCheck.
        boolean verticalCheck = (y >= r.top - hysteresis) && (y < r.bottom + hysteresis);
        boolean horizCheck = (x >= r.left - hysteresis) && (x < r.right + hysteresis);

        // Check whether the position is near some edge(s).
        if ((Math.abs(r.left - x) < hysteresis) && verticalCheck) {
            retval |= GROW_LEFT_EDGE;
        }
        if ((Math.abs(r.right - x) < hysteresis) && verticalCheck) {
            retval |= GROW_RIGHT_EDGE;
        }
        if ((Math.abs(r.top - y) < hysteresis) && horizCheck) {
            retval |= GROW_TOP_EDGE;
        }
        if ((Math.abs(r.bottom - y) < hysteresis) && horizCheck) {
            retval |= GROW_BOTTOM_EDGE;
        }

        // Not near any edge but inside the rectangle: move.
        if (retval == GROW_NONE && r.contains((int) x, (int) y)) {
            retval = MOVE;
        }
    }
    return retval;
}

From source file:com.example.gatsu.theevent.HighlightView.java

public int getHit(float x, float y) {

    Rect r = computeLayout();
    final float hysteresis = 20F;
    int retval = GROW_NONE;

    if (mCircle) {
        float distX = x - r.centerX();
        float distY = y - r.centerY();
        int distanceFromCenter = (int) Math.sqrt(distX * distX + distY * distY);
        int radius = mDrawRect.width() / 2;
        int delta = distanceFromCenter - radius;
        if (Math.abs(delta) <= hysteresis) {
            if (Math.abs(distY) > Math.abs(distX)) {
                if (distY < 0) {
                    retval = GROW_TOP_EDGE;
                } else {
                    retval = GROW_BOTTOM_EDGE;
                }/*from   w  ww  .  ja  v  a2  s .  c o  m*/
            } else {
                if (distX < 0) {
                    retval = GROW_LEFT_EDGE;
                } else {
                    retval = GROW_RIGHT_EDGE;
                }
            }
        } else if (distanceFromCenter < radius) {
            retval = MOVE;
        } else {
            retval = GROW_NONE;
        }
    } else {
        // verticalCheck makes sure the position is between the top and
        // the bottom edge (with some tolerance). Similar for horizCheck.
        boolean verticalCheck = (y >= r.top - hysteresis) && (y < r.bottom + hysteresis);
        boolean horizCheck = (x >= r.left - hysteresis) && (x < r.right + hysteresis);

        // Check whether the position is near some edge(s).
        if ((Math.abs(r.left - x) < hysteresis) && verticalCheck) {
            retval |= GROW_LEFT_EDGE;
        }
        if ((Math.abs(r.right - x) < hysteresis) && verticalCheck) {
            retval |= GROW_RIGHT_EDGE;
        }
        if ((Math.abs(r.top - y) < hysteresis) && horizCheck) {
            retval |= GROW_TOP_EDGE;
        }
        if ((Math.abs(r.bottom - y) < hysteresis) && horizCheck) {
            retval |= GROW_BOTTOM_EDGE;
        }

        // Not near any edge but inside the rectangle: move.
        if (retval == GROW_NONE && r.contains((int) x, (int) y)) {
            retval = MOVE;
        }
    }
    return retval;
}

From source file:net.nurik.roman.formwatchface.CompanionWatchFaceConfigActivity.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void updateUIToSelectedTheme(final String themeId, final boolean animate) {
    for (final ThemeUiHolder holder : mThemeUiHolders) {
        boolean selected = holder.theme.id.equals(themeId);

        holder.button.setSelected(selected);

        if (holder.selected != selected && selected) {
            if (mCurrentRevealAnimator != null) {
                mCurrentRevealAnimator.end();
                updatePreviewView(mAnimatingTheme, mMainClockContainerView, mMainClockView);
            }//from ww w.  ja  v a 2s .  c  o m

            if (animate && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mAnimatingTheme = holder.theme;
                updatePreviewView(mAnimatingTheme, mAnimateClockContainerView, mAnimateClockView);

                Rect buttonRect = new Rect();
                Rect clockContainerRect = new Rect();
                holder.button.getGlobalVisibleRect(buttonRect);
                mMainClockContainerView.getGlobalVisibleRect(clockContainerRect);

                int cx = buttonRect.centerX() - clockContainerRect.left;
                int cy = buttonRect.centerY() - clockContainerRect.top;
                clockContainerRect.offsetTo(0, 0);

                mCurrentRevealAnimator = ViewAnimationUtils.createCircularReveal(mAnimateClockContainerView, cx,
                        cy, 0, MathUtil.maxDistanceToCorner(clockContainerRect, cx, cy));
                mAnimateClockContainerView.setVisibility(View.VISIBLE);
                mCurrentRevealAnimator.setDuration(300);
                mCurrentRevealAnimator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        if (mCurrentRevealAnimator == animation) {
                            mAnimateClockContainerView.setVisibility(View.INVISIBLE);
                            updatePreviewView(holder.theme, mMainClockContainerView, mMainClockView);
                        }
                    }
                });

                mAnimateClockView.postInvalidateOnAnimation();
                mCurrentRevealAnimator.start();
            } else {
                updatePreviewView(holder.theme, mMainClockContainerView, mMainClockView);
            }
        }

        holder.selected = selected;
    }
}

From source file:com.itsronald.widget.IndicatorDotPathView.java

@NonNull
Animator connectPathAnimator() {//from   ww  w  . ja  v  a 2  s. co  m
    final Rect startSegmentBounds = viewRectInNeighborCoords(startPathSegment, endPathSegment);
    final Rect endSegmentBounds = viewRectInNeighborCoords(endPathSegment, startPathSegment);

    final int startSegmentToX = endSegmentBounds.centerX() < 0 ? endSegmentBounds.left : endSegmentBounds.right;
    final int startSegmentToY = endSegmentBounds.centerY() < 0 ? endSegmentBounds.top : endSegmentBounds.bottom;
    final int endSegmentToX = startSegmentBounds.centerX() < 0 ? startSegmentBounds.left
            : startSegmentBounds.right;
    final int endSegmentToY = startSegmentBounds.centerY() < 0 ? startSegmentBounds.top
            : startSegmentBounds.bottom;

    final Animator startSegmentAnimator = startPathSegment.stretchAnimator(PATH_STRETCH_ANIM_DURATION,
            startSegmentToX, startSegmentToY);
    final Animator endSegmentAnimator = endPathSegment.stretchAnimator(PATH_STRETCH_ANIM_DURATION,
            endSegmentToX, endSegmentToY);

    final AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playTogether(startSegmentAnimator, endSegmentAnimator, centerSegmentGrowAnimator());
    animatorSet.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation) {
            startDot.setVisibility(VISIBLE);
            endDot.setVisibility(VISIBLE);
        }
    });

    return animatorSet;
}

From source file:com.commit451.springy.CompanionWatchFaceConfigActivity.java

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void updateUIToSelectedTheme(final String themeId, final boolean animate) {
    for (final ThemeUiHolder holder : mThemeUiHolders) {
        boolean selected = holder.theme.id.equals(themeId);

        holder.button.setSelected(selected);

        if (holder.selected != selected && selected) {
            if (mCurrentRevealAnimator != null) {
                mCurrentRevealAnimator.end();
                updatePreviewView(mAnimatingTheme, mMainClockContainerView);
            }/*from www .jav a2  s  . c o m*/

            if (animate && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                mAnimatingTheme = holder.theme;
                updatePreviewView(mAnimatingTheme, mAnimateClockContainerView);

                Rect buttonRect = new Rect();
                Rect clockContainerRect = new Rect();
                holder.button.getGlobalVisibleRect(buttonRect);
                mMainClockContainerView.getGlobalVisibleRect(clockContainerRect);

                int cx = buttonRect.centerX() - clockContainerRect.left;
                int cy = buttonRect.centerY() - clockContainerRect.top;
                clockContainerRect.offsetTo(0, 0);

                mCurrentRevealAnimator = ViewAnimationUtils.createCircularReveal(mAnimateClockContainerView, cx,
                        cy, 0, MathUtil.maxDistanceToCorner(clockContainerRect, cx, cy));
                mAnimateClockContainerView.setVisibility(View.VISIBLE);
                mCurrentRevealAnimator.setDuration(300);
                mCurrentRevealAnimator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        if (mCurrentRevealAnimator == animation) {
                            mAnimateClockContainerView.setVisibility(View.INVISIBLE);
                            updatePreviewView(holder.theme, mMainClockContainerView);
                        }
                    }
                });

                mAnimateClockView.postInvalidateOnAnimation();
                mCurrentRevealAnimator.start();
            } else {
                updatePreviewView(holder.theme, mMainClockContainerView);
            }
        }

        holder.selected = selected;
    }
}