Example usage for android.graphics RectF centerX

List of usage examples for android.graphics RectF centerX

Introduction

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

Prototype

public final float centerX() 

Source Link

Usage

From source file:com.org.lengend.photoview.library.PhotoViewAttacher.java

@SuppressLint("ClickableViewAccessibility")
@Override//from w  w  w  . ja  va2  s  .c  o m
public boolean onTouch(View v, MotionEvent ev) {
    boolean handled = false;

    if (mZoomEnabled && hasDrawable((ImageView) v)) {
        ViewParent parent = v.getParent();
        switch (ev.getAction()) {
        case ACTION_DOWN:
            // First, disable the Parent from intercepting the touch
            // event
            if (null != parent) {
                parent.requestDisallowInterceptTouchEvent(true);
            } else {
                Logger.i(LOG_TAG, "onTouch getParent() returned null");
            }

            // If we're flinging, and the user presses down, cancel
            // fling
            cancelFling();
            break;

        case ACTION_CANCEL:
        case ACTION_UP:
            // If the user has zoomed less than min scale, zoom back
            // to min scale
            if (getScale() < mMinScale) {
                RectF rect = getDisplayRect();
                if (null != rect) {
                    v.post(new AnimatedZoomRunnable(getScale(), mMinScale, rect.centerX(), rect.centerY()));
                    handled = true;
                }
            }
            break;
        }

        // Try the Scale/Drag detector
        if (null != mScaleDragDetector) {
            boolean wasScaling = mScaleDragDetector.isScaling();
            boolean wasDragging = mScaleDragDetector.isDragging();

            handled = mScaleDragDetector.onTouchEvent(ev);

            boolean didntScale = !wasScaling && !mScaleDragDetector.isScaling();
            boolean didntDrag = !wasDragging && !mScaleDragDetector.isDragging();

            mBlockParentIntercept = didntScale && didntDrag;
        }

        // Check to see if the user double tapped
        if (null != mGestureDetector && mGestureDetector.onTouchEvent(ev)) {
            handled = true;
        }

    }

    return handled;
}

From source file:baizhuan.hangzhou.com.gankcopy.view.customview.photoview.PhotoViewAttacher.java

@SuppressLint("ClickableViewAccessibility")
@Override// w w w.j a  v a  2 s .  co  m
public boolean onTouch(View v, MotionEvent ev) {
    boolean handled = false;

    if (mZoomEnabled && hasDrawable((ImageView) v)) {
        ViewParent parent = v.getParent();
        switch (ev.getAction()) {
        case ACTION_DOWN:
            // First, disable the Parent from intercepting the touch
            // event
            if (null != parent) {
                parent.requestDisallowInterceptTouchEvent(true);
            } else {
                LogManager.getLogger().i(LOG_TAG, "onTouch getParent() returned null");
            }

            // If we're flinging, and the user presses down, cancel
            // fling
            cancelFling();
            break;

        case ACTION_CANCEL:
        case ACTION_UP:
            // If the user has zoomed less than min scale, zoom back
            // to min scale
            if (getScale() < mMinScale) {
                RectF rect = getDisplayRect();
                if (null != rect) {
                    v.post(new AnimatedZoomRunnable(getScale(), mMinScale, rect.centerX(), rect.centerY()));
                    handled = true;
                }
            }
            break;
        }

        // Try the Scale/Drag detector
        if (null != mScaleDragDetector) {
            boolean wasScaling = mScaleDragDetector.isScaling();
            boolean wasDragging = mScaleDragDetector.isDragging();

            handled = mScaleDragDetector.onTouchEvent(ev);

            boolean didntScale = !wasScaling && !mScaleDragDetector.isScaling();
            boolean didntDrag = !wasDragging && !mScaleDragDetector.isDragging();

            mBlockParentIntercept = didntScale && didntDrag;
        }

        // Check to see if the user double tapped
        if (null != mGestureDetector && mGestureDetector.onTouchEvent(ev)) {
            handled = true;
        }

    }

    return handled;
}

From source file:com.huyn.demogroup.freechild.FixedViewAttacher.java

private boolean checkMatrixBounds() {
    final RectF rect = getVisibleRect();
    if (rect == null) {
        return false;
    }//from w w w.j  ava 2 s. c  om

    RectF src = new RectF();
    src.set(rect);
    final float h = rect.height(), w = rect.width();
    float deltaX = 0, deltaY = 0;
    View parent = (View) mImageView.getParent();

    final int viewHeight = parent.getHeight();//getImageViewHeight(mImageView);
    final int viewWidth = parent.getWidth();//getImageViewWidth(mImageView);
    System.out.println("===============================================width:" + viewWidth + "/" + viewHeight);
    System.out
            .println("+++++++++rect.left:" + rect.left + "/" + rect.top + "/" + rect.right + "/" + rect.bottom);

    float scale = getScale();
    float centerX = rect.centerX();
    float centerY = rect.centerY();
    float targetW = w * scale;
    float targetH = h * scale;
    RectF targetRect = new RectF(centerX - targetW / 2, centerY - targetH / 2, centerX + targetW / 2,
            centerY + targetH / 2);

    rect.set(targetRect);
    final float height = rect.height(), width = rect.width();

    if (height <= viewHeight) {
        deltaY = (viewHeight - height) / 2 - rect.top;
    } else if (rect.top > 0) {
        deltaY = -rect.top;
    } else if (rect.bottom < viewHeight) {
        deltaY = viewHeight - rect.bottom;
    }

    if (width <= viewWidth) {
        deltaX = (viewWidth - width) / 2 - rect.left;
        mScrollEdge = EDGE_BOTH;
    } else if (rect.left > 0) {
        mScrollEdge = EDGE_LEFT;
        deltaX = -rect.left;
    } else if (rect.right < viewWidth) {
        deltaX = viewWidth - rect.right;
        mScrollEdge = EDGE_RIGHT;
    } else {
        mScrollEdge = EDGE_NONE;
    }

    // Finally actually translate the matrix
    if (!mStable) {
        System.out.println("+++++++++++deltaX:" + deltaX + "/deltaY:" + deltaY + "---" + getScale());
        postTranslate(deltaX, deltaY);
    }
    return true;
}

From source file:org.getlantern.firetweet.view.ShapedImageView.java

private void drawBorder(@NonNull final Canvas canvas, @NonNull final RectF dest) {
    if (mBorderColors == null)
        return;/*from  w ww  . jav  a  2  s.  com*/
    final RectF transitionSrc = mTransitionSource, transitionDst = mTransitionDestination;
    final float strokeWidth;
    if (transitionSrc != null && transitionDst != null) {
        final float progress = 1
                - (dest.width() - transitionDst.width()) / (transitionSrc.width() - transitionDst.width());
        strokeWidth = mStrokeWidth * progress;
        mBorderPaint.setAlpha(Math.round(mBorderAlpha * progress));
        ViewCompat.setTranslationZ(this, -ViewCompat.getElevation(this) * (1 - progress));
    } else {
        strokeWidth = mStrokeWidth;
        mBorderPaint.setAlpha(mBorderAlpha);
        ViewCompat.setTranslationZ(this, 0);
    }
    mBorderPaint.setStrokeWidth(strokeWidth);
    if (getStyle() == SHAPE_CIRCLE) {
        final float circleRadius = Math.min(dest.width(), dest.height()) / 2f - strokeWidth / 2;
        canvas.drawCircle(dest.centerX(), dest.centerY(), circleRadius, mBorderPaint);
    } else {
        final float radius = getCalculatedCornerRadius();
        final float inset = mStrokeWidth / 2;
        dest.inset(inset, inset);
        canvas.drawRoundRect(dest, radius, radius, mBorderPaint);
        dest.inset(-inset, -inset);
    }
}

From source file:com.appeaser.sublimepickerlibrary.datepicker.SimpleMonthView.java

/**
 * Draws the month days.//  www. j  av a  2 s. c o  m
 */
@SuppressWarnings("ConstantConditions")
private void drawDays(Canvas canvas) {
    final TextPaint p = mDayPaint;
    final int headerHeight = mMonthHeight + mDayOfWeekHeight;
    //final int rowHeight = mDayHeight;
    final float rowHeight = mDayHeight;
    //final int colWidth = mCellWidth;
    final float colWidth = mCellWidth;

    // Text is vertically centered within the row height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    //int rowCenter = headerHeight + rowHeight / 2;
    float rowCenter = headerHeight + rowHeight / 2f;

    for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) {
        //final int colCenter = colWidth * col + colWidth / 2;
        final float colCenter = colWidth * col + colWidth / 2f;
        //final int colCenterRtl;
        final float colCenterRtl;
        if (SUtils.isLayoutRtlCompat(this)) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        int stateMask = 0;

        final boolean isDayEnabled = isDayEnabled(day);
        if (isDayEnabled) {
            stateMask |= SUtils.STATE_ENABLED;
        }

        final boolean isDayInActivatedRange = mActivatedDays.isValid() && mActivatedDays.isActivated(day);
        final boolean isSelected = mActivatedDays.isSelected(day);

        if (isSelected) {
            stateMask |= SUtils.STATE_ACTIVATED;
            canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDaySelectorPaint);
        } else if (isDayInActivatedRange) {
            stateMask |= SUtils.STATE_ACTIVATED;

            int bgShape = DRAW_RECT;

            if (mActivatedDays.isSingleDay()) {
                if (mActivatedDays.isStartOfMonth()) {
                    bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT;
                } else {
                    bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT;
                }
            } else if (mActivatedDays.isStartingDayOfRange(day)) {
                bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT;
            } else if (mActivatedDays.isEndingDayOfRange(day)) {
                bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT;
            }

            // Use height to constrain the protrusion of the arc
            boolean constrainProtrusion = colWidth > (rowHeight - (2 * mPaddingRangeIndicator));
            float horDistFromCenter = constrainProtrusion ? rowHeight / 2f - mPaddingRangeIndicator
                    : colWidth / 2f;

            switch (bgShape) {
            case DRAW_RECT_WITH_CURVE_ON_LEFT:
                int leftRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl - horDistFromCenter) + 1
                        : (int) (colCenterRtl - horDistFromCenter);

                int leftRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl + horDistFromCenter) + 1
                        : (int) (colCenterRtl + horDistFromCenter);

                RectF leftArcRect = new RectF(leftRectArcLeft,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, leftRectArcRight,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator);

                canvas.drawArc(leftArcRect, 90, 180, true, mDayRangeSelectorPaint);

                canvas.drawRect(new RectF(leftArcRect.centerX(),
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            case DRAW_RECT_WITH_CURVE_ON_RIGHT:
                int rightRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl - horDistFromCenter) + 1
                        : (int) (colCenterRtl - horDistFromCenter);

                int rightRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl + horDistFromCenter) + 1
                        : (int) (colCenterRtl + horDistFromCenter);

                RectF rightArcRect = new RectF(rightRectArcLeft,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightRectArcRight,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator);

                canvas.drawArc(rightArcRect, 270, 180, true, mDayRangeSelectorPaint);

                canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightArcRect.centerX(),
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            default:
                canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            }
        }

        if (mTouchedItem == day) {
            stateMask |= SUtils.STATE_PRESSED;

            if (isDayEnabled) {
                canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDayHighlightPaint);
            }
        }

        final boolean isDayToday = mToday == day;
        final int dayTextColor;

        if (isDayToday && !isDayInActivatedRange) {
            dayTextColor = mDaySelectorPaint.getColor();
        } else {
            final int[] stateSet = SUtils.resolveStateSet(stateMask);
            dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
        }
        p.setColor(dayTextColor);

        canvas.drawText(mDayFormatter.format(day), colCenterRtl, rowCenter - halfLineHeight, p);

        col++;

        if (col == DAYS_IN_WEEK) {
            col = 0;
            rowCenter += rowHeight;
        }
    }
}

From source file:com.miuhouse.yourcompany.student.view.widget.date.datepicker.SimpleMonthView.java

/**
 * Draws the month days./*from   w  w  w  .  j  av a  2  s .c o m*/
 */
@SuppressWarnings("ConstantConditions")
private void drawDays(Canvas canvas) {
    final TextPaint p = mDayPaint;
    final int headerHeight = mMonthHeight + mDayOfWeekHeight;

    final float rowHeight = mDayHeight;

    final float colWidth = mCellWidth;

    // Text is vertically centered within the row height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    //        int rowCenter = headerHeight + rowHeight / 2;
    float rowCenter = headerHeight + rowHeight / 2f;
    //mDaysInMonth303129
    for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) {
        final float colCenter = colWidth * col + colWidth / 2f;
        //final int colCenterRtl;
        final float colCenterRtl;
        if (SUtils.isLayoutRtlCompat(this)) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        int stateMask = 0;

        final boolean isDayEnabled = isDayEnabled(day);
        if (isDayEnabled) {
            stateMask |= SUtils.STATE_ENABLED;
        }

        final boolean isDayInActivatedRange = mActivatedDays.isValid() && mActivatedDays.isActivated(day);
        final boolean isSelected = mActivatedDays.isSelected(day);
        final boolean isSelectedAndTogether = mActivatedDays.isSelectedAndTogether(day);
        if (isSelectedAndTogether) {
            if ((!disable && !isDisable) || isFill) {
                stateMask |= SUtils.STATE_ACTIVATED;
                canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDaySelectorPaint);
            }
        }
        if (!isSelected && isDayInActivatedRange) {

            stateMask |= SUtils.STATE_ACTIVATED;
            int bgShape = DRAW_RECT;
            if (mActivatedDays.isSingleDay()) {
                if (mActivatedDays.isStartOfMonth()) {
                    bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT;
                } else {
                    bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT;
                }
            } else if (mActivatedDays.isStartingDayOfRange(day)) {
                bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT;
            } else if (mActivatedDays.isEndingDayOfRange(day)) {
                bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT;
            }

            // Use height to constrain the protrusion of the arc
            boolean constrainProtrusion = colWidth > (rowHeight - (2 * mPaddingRangeIndicator));

            float horDistFromCenter = constrainProtrusion ? rowHeight / 2f - mPaddingRangeIndicator
                    : colWidth / 2f;
            switch (bgShape) {
            case DRAW_RECT_WITH_CURVE_ON_LEFT:

                int leftRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl - horDistFromCenter) + 1
                        : (int) (colCenterRtl - horDistFromCenter);

                int leftRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl + horDistFromCenter) + 1
                        : (int) (colCenterRtl + horDistFromCenter);

                RectF leftArcRect = new RectF(leftRectArcLeft,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, leftRectArcRight,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator);

                canvas.drawArc(leftArcRect, 90, 180, true, mDayRangeSelectorPaint);

                canvas.drawRect(new RectF(leftArcRect.centerX(),
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            case DRAW_RECT_WITH_CURVE_ON_RIGHT:
                int rightRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl - horDistFromCenter) + 1
                        : (int) (colCenterRtl - horDistFromCenter);

                int rightRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl + horDistFromCenter) + 1
                        : (int) (colCenterRtl + horDistFromCenter);

                RectF rightArcRect = new RectF(rightRectArcLeft,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightRectArcRight,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator);

                canvas.drawArc(rightArcRect, 270, 180, true, mDayRangeSelectorPaint);

                canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightArcRect.centerX(),
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            default:
                canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            }
        }

        final boolean isDayToday = mToday == day;
        final int dayTextColor;
        if (isDayToday) {
            if (mActivatedDays.isClick && isSelected) {
                final int[] stateSet = SUtils.resolveStateSet(stateMask);
                dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
            } else {
                dayTextColor = mDaySelectorPaint.getColor();
            }
        } else {
            final int[] stateSet = SUtils.resolveStateSet(stateMask);
            if (!mActivatedDays.isSingleDay()) {
                if (stateMask == 1) {
                    dayTextColor = mContext.getResources().getColor(R.color.textDarkfour);
                } else {
                    dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
                }
            } else {
                dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
            }
        }
        p.setColor(dayTextColor);

        canvas.drawText(mDayFormatter.format(day), colCenterRtl, rowCenter - halfLineHeight, p);

        col++;

        if (col == DAYS_IN_WEEK) {
            col = 0;
            rowCenter += rowHeight;
        }
        if (mDaysInMonth == day)
            mOnRangeClickListener.onRangeSelected(calendars, position);
    }
}

From source file:de.vanita5.twittnuker.view.ColorPickerView.java

private void drawAlphaPanel(final Canvas canvas) {

    if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null)
        return;//w w w  . j a  v  a  2 s  . c o m

    final RectF rect = mAlphaRect;

    if (BORDER_WIDTH_PX > 0) {
        mBorderPaint.setColor(mBorderColor);
        canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX,
                rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
    }

    mAlphaPattern.draw(canvas);

    final float[] hsv = new float[] { mHue, mSat, mVal };
    final int color = Color.HSVToColor(hsv);
    final int acolor = Color.HSVToColor(0, hsv);

    mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, color, acolor, TileMode.CLAMP);

    mAlphaPaint.setShader(mAlphaShader);

    canvas.drawRect(rect, mAlphaPaint);

    if (mAlphaSliderText != null && mAlphaSliderText != "") {
        canvas.drawText(mAlphaSliderText, rect.centerX(), rect.centerY() + 4 * mDensity, mAlphaTextPaint);
    }

    final float rectWidth = 4 * mDensity / 2;

    final Point p = alphaToPoint(mAlpha);

    final RectF r = new RectF();
    r.left = p.x - rectWidth;
    r.right = p.x + rectWidth;
    r.top = rect.top - RECTANGLE_TRACKER_OFFSET;
    r.bottom = rect.bottom + RECTANGLE_TRACKER_OFFSET;

    canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint);

}

From source file:org.akop.crosswords.view.CrosswordView.java

private void renderCell(Canvas canvas, Cell cell, RectF cellRect, Paint fillPaint) {
    canvas.drawRect(cellRect, fillPaint);
    canvas.drawRect(cellRect, mCellStrokePaint);

    if (cell.mCircled) {
        canvas.drawCircle(cellRect.centerX(), cellRect.centerY(), mCircleRadius, mCircleStrokePaint);
    }//w ww.  j  ava 2s . c o  m

    if (cell.mNumber != null) {
        mNumberTextPaint.getTextBounds(cell.mNumber, 0, cell.mNumber.length(), mTempRect);

        float numberX = cellRect.left + mNumberTextPadding + (mTempRect.width() / 2);
        float numberY = cellRect.top + mNumberTextPadding + mTempRect.height();

        if (cell.mCircled) {
            canvas.drawText(cell.mNumber, numberX, numberY, mNumberStrokePaint);
        }

        canvas.drawText(cell.mNumber, numberX, numberY, mNumberTextPaint);
    }

    if (cell.mCheated) {
        Path path = new Path();
        path.moveTo(cellRect.right, cellRect.bottom);
        path.lineTo(cellRect.right - mMarkerSideLength, cellRect.bottom);
        path.lineTo(cellRect.right, cellRect.bottom - mMarkerSideLength);
        path.close();

        canvas.drawPath(path, mCheatedCellFillPaint);
        canvas.drawPath(path, mCellStrokePaint);
    }

    if (cell.mError) {
        Path path = new Path();
        path.moveTo(cellRect.left, cellRect.bottom);
        path.lineTo(cellRect.left + mMarkerSideLength, cellRect.bottom);
        path.lineTo(cellRect.left, cellRect.bottom - mMarkerSideLength);
        path.close();

        canvas.drawPath(path, mMistakeCellFillPaint);
        canvas.drawPath(path, mCellStrokePaint);
    }

    if (!cell.isEmpty()) {
        mAnswerTextPaint.getTextBounds(cell.mCharStr, 0, cell.mCharStr.length(), mTempRect);
        canvas.drawText(cell.mCharStr, cellRect.left + mCellSize / 2,
                cellRect.top + mCellSize - mAnswerTextPadding, mAnswerTextPaint);
    }
}

From source file:com.ape.camera2raw.Camera2RawFragment.java

/**
 * Configure the necessary {@link Matrix} transformation to `mTextureView`,
 * and start/restart the preview capture session if necessary.
 * <p/>/*from  w w  w  .  ja  v a 2 s. c  o m*/
 * This method should be called after the camera state has been initialized in
 * setUpCameraOutputs.
 *
 * @param viewWidth  The width of `mTextureView`
 * @param viewHeight The height of `mTextureView`
 */
private void configureTransform(int viewWidth, int viewHeight) {
    Activity activity = getActivity();
    synchronized (mCameraStateLock) {
        if (null == mTextureView || null == activity) {
            return;
        }

        StreamConfigurationMap map = mCharacteristics
                .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);

        // For still image captures, we always use the largest available size.
        Size largestJpeg = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                new CompareSizesByArea());

        // Find the rotation of the device relative to the native device orientation.
        int deviceRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        Point displaySize = new Point();
        activity.getWindowManager().getDefaultDisplay().getSize(displaySize);

        // Find the rotation of the device relative to the camera sensor's orientation.
        int totalRotation = sensorToDeviceRotation(mCharacteristics, deviceRotation);

        // Swap the view dimensions for calculation as needed if they are rotated relative to
        // the sensor.
        boolean swappedDimensions = totalRotation == 90 || totalRotation == 270;
        int rotatedViewWidth = viewWidth;
        int rotatedViewHeight = viewHeight;
        int maxPreviewWidth = displaySize.x;
        int maxPreviewHeight = displaySize.y;

        if (swappedDimensions) {
            rotatedViewWidth = viewHeight;
            rotatedViewHeight = viewWidth;
            maxPreviewWidth = displaySize.y;
            maxPreviewHeight = displaySize.x;
        }

        // Preview should not be larger than display size and 1080p.
        if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
            maxPreviewWidth = MAX_PREVIEW_WIDTH;
        }

        if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
            maxPreviewHeight = MAX_PREVIEW_HEIGHT;
        }

        // Find the best preview size for these view dimensions and configured JPEG size.
        Size previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedViewWidth,
                rotatedViewHeight, maxPreviewWidth, maxPreviewHeight, largestJpeg);

        if (swappedDimensions) {
            mTextureView.setAspectRatio(previewSize.getHeight(), previewSize.getWidth());
        } else {
            mTextureView.setAspectRatio(previewSize.getWidth(), previewSize.getHeight());
        }

        // Find rotation of device in degrees (reverse device orientation for front-facing
        // cameras).
        int rotation = (mCharacteristics
                .get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT)
                        ? (360 + ORIENTATIONS.get(deviceRotation)) % 360
                        : (360 - ORIENTATIONS.get(deviceRotation)) % 360;

        Matrix matrix = new Matrix();
        RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
        RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
        float centerX = viewRect.centerX();
        float centerY = viewRect.centerY();

        // Initially, output stream images from the Camera2 API will be rotated to the native
        // device orientation from the sensor's orientation, and the TextureView will default to
        // scaling these buffers to fill it's view bounds.  If the aspect ratios and relative
        // orientations are correct, this is fine.
        //
        // However, if the device orientation has been rotated relative to its native
        // orientation so that the TextureView's dimensions are swapped relative to the
        // native device orientation, we must do the following to ensure the output stream
        // images are not incorrectly scaled by the TextureView:
        //   - Undo the scale-to-fill from the output buffer's dimensions (i.e. its dimensions
        //     in the native device orientation) to the TextureView's dimension.
        //   - Apply a scale-to-fill from the output buffer's rotated dimensions
        //     (i.e. its dimensions in the current device orientation) to the TextureView's
        //     dimensions.
        //   - Apply the rotation from the native device orientation to the current device
        //     rotation.
        if (Surface.ROTATION_90 == deviceRotation || Surface.ROTATION_270 == deviceRotation) {
            bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
            matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
            float scale = Math.max((float) viewHeight / previewSize.getHeight(),
                    (float) viewWidth / previewSize.getWidth());
            matrix.postScale(scale, scale, centerX, centerY);

        }
        matrix.postRotate(rotation, centerX, centerY);

        mTextureView.setTransform(matrix);

        // Start or restart the active capture session if the preview was initialized or
        // if its aspect ratio changed significantly.
        if (mPreviewSize == null || !checkAspectsEqual(previewSize, mPreviewSize)) {
            mPreviewSize = previewSize;
            if (mState != STATE_CLOSED) {
                createCameraPreviewSessionLocked();
            }
        }
    }
}