Java Code Examples for android.widget.ImageView.ScaleType#FIT_START

The following examples show how to use android.widget.ImageView.ScaleType#FIT_START . 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: ViewManager.java    From react-native-image-zoom with MIT License 5 votes vote down vote up
@ReactProp(name = "scaleType")
public void setScaleType(PhotoViewWrapper view, String scaleType) {
    ScaleType value = ScaleType.CENTER;

    switch (scaleType) {
        case "center":
            value = ScaleType.CENTER;
            break;
        case "centerCrop":
            value = ScaleType.CENTER_CROP;
            break;
        case "centerInside":
            value = ScaleType.CENTER_INSIDE;
            break;
        case "fitCenter":
            value = ScaleType.FIT_CENTER;
            break;
        case "fitStart":
            value = ScaleType.FIT_START;
            break;
        case "fitEnd":
            value = ScaleType.FIT_END;
            break;
        case "fitXY":
            value = ScaleType.FIT_XY;
            break;
        case "matrix":
            value = ScaleType.MATRIX;
            break;
    }

    view.setScaleType(value);
}
 
Example 2
Source File: ScaleDragHelper.java    From sketch with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("ConstantConditions")
private void resetBaseMatrix() {
    baseMatrix.reset();

    Size viewSize = imageZoomer.getViewSize();
    Size imageSize = imageZoomer.getImageSize();
    Size drawableSize = imageZoomer.getDrawableSize();
    boolean readMode = imageZoomer.isReadMode();
    ScaleType scaleType = imageZoomer.getScaleType();

    final int drawableWidth = imageZoomer.getRotateDegrees() % 180 == 0 ? drawableSize.getWidth() : drawableSize.getHeight();
    final int drawableHeight = imageZoomer.getRotateDegrees() % 180 == 0 ? drawableSize.getHeight() : drawableSize.getWidth();
    final int imageWidth = imageZoomer.getRotateDegrees() % 180 == 0 ? imageSize.getWidth() : imageSize.getHeight();
    final int imageHeight = imageZoomer.getRotateDegrees() % 180 == 0 ? imageSize.getHeight() : imageSize.getWidth();
    boolean imageThanViewLarge = drawableWidth > viewSize.getWidth() || drawableHeight > viewSize.getHeight();

    final ScaleType finalScaleType;
    if (scaleType == ScaleType.MATRIX) {
        finalScaleType = ScaleType.FIT_CENTER;
    } else if (scaleType == ScaleType.CENTER_INSIDE) {
        finalScaleType = imageThanViewLarge ? ScaleType.FIT_CENTER : ScaleType.CENTER;
    } else {
        finalScaleType = scaleType;
    }

    float initScale = imageZoomer.getZoomScales().getInitZoomScale();

    ImageSizeCalculator sizeCalculator = Sketch.with(imageZoomer.getImageView().getContext()).getConfiguration().getSizeCalculator();
    if (readMode && sizeCalculator.canUseReadModeByHeight(imageWidth, imageHeight)) {
        baseMatrix.postScale(initScale, initScale);
    } else if (readMode && sizeCalculator.canUseReadModeByWidth(imageWidth, imageHeight)) {
        baseMatrix.postScale(initScale, initScale);
    } else if (finalScaleType == ScaleType.CENTER) {
        baseMatrix.postScale(initScale, initScale);
        baseMatrix.postTranslate((viewSize.getWidth() - drawableWidth) / 2F, (viewSize.getHeight() - drawableHeight) / 2F);
    } else if (finalScaleType == ScaleType.CENTER_CROP) {
        baseMatrix.postScale(initScale, initScale);
        baseMatrix.postTranslate((viewSize.getWidth() - drawableWidth * initScale) / 2F, (viewSize.getHeight() - drawableHeight * initScale) / 2F);
    } else if (finalScaleType == ScaleType.FIT_START) {
        baseMatrix.postScale(initScale, initScale);
        baseMatrix.postTranslate(0, 0);
    } else if (finalScaleType == ScaleType.FIT_END) {
        baseMatrix.postScale(initScale, initScale);
        baseMatrix.postTranslate(0, (viewSize.getHeight() - drawableHeight * initScale));
    } else if (finalScaleType == ScaleType.FIT_CENTER) {
        baseMatrix.postScale(initScale, initScale);
        baseMatrix.postTranslate(0, (viewSize.getHeight() - drawableHeight * initScale) / 2F);
    } else if (finalScaleType == ScaleType.FIT_XY) {
        RectF mTempSrc = new RectF(0, 0, drawableWidth, drawableHeight);
        RectF mTempDst = new RectF(0, 0, viewSize.getWidth(), viewSize.getHeight());
        baseMatrix.setRectToRect(mTempSrc, mTempDst, Matrix.ScaleToFit.FILL);
    }
}
 
Example 3
Source File: AdaptiveTwoLevelScales.java    From sketch with Apache License 2.0 4 votes vote down vote up
@Override
public void reset(@NonNull final Context context, @NonNull final Sizes sizes, @Nullable final ScaleType scaleType, final float rotateDegrees, final boolean readMode) {
    final int drawableWidth = rotateDegrees % 180 == 0 ? sizes.drawableSize.getWidth() : sizes.drawableSize.getHeight();
    final int drawableHeight = rotateDegrees % 180 == 0 ? sizes.drawableSize.getHeight() : sizes.drawableSize.getWidth();
    final int imageWidth = rotateDegrees % 180 == 0 ? sizes.imageSize.getWidth() : sizes.imageSize.getHeight();
    final int imageHeight = rotateDegrees % 180 == 0 ? sizes.imageSize.getHeight() : sizes.imageSize.getWidth();

    final float widthScale = (float) sizes.viewSize.getWidth() / drawableWidth;
    final float heightScale = (float) sizes.viewSize.getHeight() / drawableHeight;
    final boolean imageThanViewLarge = drawableWidth > sizes.viewSize.getWidth() || drawableHeight > sizes.viewSize.getHeight();

    final ScaleType finalScaleType;
    if (scaleType == ScaleType.MATRIX) {
        finalScaleType = ScaleType.FIT_CENTER;
    } else if (scaleType == ScaleType.CENTER_INSIDE) {
        finalScaleType = imageThanViewLarge ? ScaleType.FIT_CENTER : ScaleType.CENTER;
    } else {
        finalScaleType = scaleType;
    }

    // 小的是完整显示比例,大的是充满比例
    fullZoomScale = Math.min(widthScale, heightScale);
    fillZoomScale = Math.max(widthScale, heightScale);
    originZoomScale = Math.max((float) imageWidth / drawableWidth, (float) imageHeight / drawableHeight);
    initZoomScale = getInitScale(context, sizes, finalScaleType, rotateDegrees, readMode);

    ImageSizeCalculator sizeCalculator = Sketch.with(context).getConfiguration().getSizeCalculator();
    if (readMode && sizeCalculator.canUseReadModeByHeight(imageWidth, imageHeight)) {
        // 阅读模式下保证阅读效果最重要
        minZoomScale = fullZoomScale;
        maxZoomScale = Math.max(originZoomScale, fillZoomScale);
    } else if (readMode && sizeCalculator.canUseReadModeByWidth(imageWidth, imageHeight)) {
        // 阅读模式下保证阅读效果最重要
        minZoomScale = fullZoomScale;
        maxZoomScale = Math.max(originZoomScale, fillZoomScale);
    } else if (finalScaleType == ScaleType.CENTER) {
        minZoomScale = 1.0f;
        maxZoomScale = Math.max(originZoomScale, fillZoomScale);
    } else if (finalScaleType == ScaleType.CENTER_CROP) {
        minZoomScale = fillZoomScale;
        // 由于CENTER_CROP的时候最小缩放比例就是充满比例,所以最大缩放比例一定要比充满比例大的多
        maxZoomScale = Math.max(originZoomScale, fillZoomScale * 1.5f);
    } else if (finalScaleType == ScaleType.FIT_START || finalScaleType == ScaleType.FIT_CENTER || finalScaleType == ScaleType.FIT_END) {
        minZoomScale = fullZoomScale;

        // 如果原始比例仅仅比充满比例大一点点,还是用充满比例作为最大缩放比例比较好,否则谁大用谁
        if (originZoomScale > fillZoomScale && (fillZoomScale * 1.2f) >= originZoomScale) {
            maxZoomScale = fillZoomScale;
        } else {
            maxZoomScale = Math.max(originZoomScale, fillZoomScale);
        }

        // 最大缩放比例和最小缩放比例的差距不能太小,最小得是最小缩放比例的1.5倍
        maxZoomScale = Math.max(maxZoomScale, minZoomScale * 1.5f);
    } else if (finalScaleType == ScaleType.FIT_XY) {
        minZoomScale = fullZoomScale;
        maxZoomScale = fullZoomScale;
    } else {
        // 基本不会走到这儿
        minZoomScale = fullZoomScale;
        maxZoomScale = fullZoomScale;
    }

    // 这样的情况基本不会出现,不过还是加层保险
    if (minZoomScale > maxZoomScale) {
        minZoomScale = minZoomScale + maxZoomScale;
        maxZoomScale = minZoomScale - maxZoomScale;
        minZoomScale = minZoomScale - maxZoomScale;
    }

    // 双击缩放比例始终由最小缩放比例和最大缩放比例组成
    doubleClickZoomScales = new float[]{minZoomScale, maxZoomScale};
}
 
Example 4
Source File: AdaptiveTwoLevelScales.java    From sketch with Apache License 2.0 4 votes vote down vote up
private float getInitScale(@NonNull final Context context, @NonNull final Sizes sizes, @NonNull final ScaleType scaleType, final float rotateDegrees, final boolean readMode) {
    final int drawableWidth = rotateDegrees % 180 == 0 ? sizes.drawableSize.getWidth() : sizes.drawableSize.getHeight();
    final int drawableHeight = rotateDegrees % 180 == 0 ? sizes.drawableSize.getHeight() : sizes.drawableSize.getWidth();
    final int imageWidth = rotateDegrees % 180 == 0 ? sizes.imageSize.getWidth() : sizes.imageSize.getHeight();
    final int imageHeight = rotateDegrees % 180 == 0 ? sizes.imageSize.getHeight() : sizes.imageSize.getWidth();

    final float widthScale = (float) sizes.viewSize.getWidth() / drawableWidth;
    final float heightScale = (float) sizes.viewSize.getHeight() / drawableHeight;
    boolean imageThanViewLarge = drawableWidth > sizes.viewSize.getWidth() || drawableHeight > sizes.viewSize.getHeight();

    ImageSizeCalculator sizeCalculator = Sketch.with(context).getConfiguration().getSizeCalculator();

    final ScaleType finalScaleType;
    if (scaleType == ScaleType.MATRIX) {
        finalScaleType = ScaleType.FIT_CENTER;
    } else if (scaleType == ScaleType.CENTER_INSIDE) {
        finalScaleType = imageThanViewLarge ? ScaleType.FIT_CENTER : ScaleType.CENTER;
    } else {
        finalScaleType = scaleType;
    }

    if (readMode && sizeCalculator.canUseReadModeByHeight(imageWidth, imageHeight)) {
        return widthScale;
    } else if (readMode && sizeCalculator.canUseReadModeByWidth(imageWidth, imageHeight)) {
        return heightScale;
    } else if (finalScaleType == ScaleType.CENTER) {
        return 1.0f;
    } else if (finalScaleType == ScaleType.CENTER_CROP) {
        return Math.max(widthScale, heightScale);
    } else if (finalScaleType == ScaleType.FIT_START) {
        return Math.min(widthScale, heightScale);
    } else if (finalScaleType == ScaleType.FIT_END) {
        return Math.min(widthScale, heightScale);
    } else if (finalScaleType == ScaleType.FIT_CENTER) {
        return Math.min(widthScale, heightScale);
    } else if (finalScaleType == ScaleType.FIT_XY) {
        return 1.0f;
    } else {
        return 1.0f;
    }
}