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

The following examples show how to use android.widget.ImageView.ScaleType#CENTER_INSIDE . 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: ImageRequest.java    From volley with Apache License 2.0 6 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to the
 * normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(
        String url,
        Response.Listener<Bitmap> listener,
        int maxWidth,
        int maxHeight,
        Config decodeConfig,
        Response.ErrorListener errorListener) {
    this(
            url,
            listener,
            maxWidth,
            maxHeight,
            ScaleType.CENTER_INSIDE,
            decodeConfig,
            errorListener);
}
 
Example 2
Source File: ModNavigationBar.java    From GravityBox with Apache License 2.0 5 votes vote down vote up
private static ScaleType getIconScaleType(int index, int keyId) {
    if (mUseLargerIcons) {
        return ScaleType.FIT_CENTER;
    } else {
        ScaleType origScaleType = mNavbarViewInfo[index] == null ? ScaleType.CENTER :
                mNavbarViewInfo[index].originalScaleType.get(keyId, ScaleType.CENTER);
        if (index == 0) {
            return (mNavbarHeight < 75 ? ScaleType.CENTER_INSIDE : origScaleType);
        } else {
            boolean hasVerticalNavbar = mGbContext.getResources().getBoolean(R.bool.hasVerticalNavbar);
            return (mNavbarWidth < 75 && hasVerticalNavbar ? ScaleType.CENTER_INSIDE :
                    origScaleType);
        }
    }
}
 
Example 3
Source File: ImageRequest.java    From pearl with Apache License 2.0 5 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to
 * the normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
        Config decodeConfig, Response.ErrorListener errorListener) {
    this(url, listener, maxWidth, maxHeight,
            ScaleType.CENTER_INSIDE, decodeConfig, errorListener);
}
 
Example 4
Source File: ImageRequest.java    From AndroidProjects with MIT License 5 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to
 * the normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
        Config decodeConfig, Response.ErrorListener errorListener) {
    this(url, listener, maxWidth, maxHeight,
            ScaleType.CENTER_INSIDE, decodeConfig, errorListener);
}
 
Example 5
Source File: ImageRequest.java    From CrossBow with Apache License 2.0 5 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to
 * the normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
        Config decodeConfig, Response.ErrorListener errorListener) {
    this(url, listener, maxWidth, maxHeight,
            ScaleType.CENTER_INSIDE, decodeConfig, errorListener);
}
 
Example 6
Source File: ImageRequest.java    From DaVinci with Apache License 2.0 5 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to
 * the normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
                    Config decodeConfig, Response.ErrorListener errorListener) {
    this(url, listener, maxWidth, maxHeight,
            ScaleType.CENTER_INSIDE, decodeConfig, errorListener);
}
 
Example 7
Source File: ImageRequest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to
 * the normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
        Config decodeConfig, Response.ErrorListener errorListener) {
    this(url, listener, maxWidth, maxHeight,
            ScaleType.CENTER_INSIDE, decodeConfig, errorListener);
}
 
Example 8
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 9
Source File: ImageRequest.java    From SimplifyReader with Apache License 2.0 5 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to
 * the normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
        Config decodeConfig, Response.ErrorListener errorListener) {
    this(url, listener, maxWidth, maxHeight,
            ScaleType.CENTER_INSIDE, decodeConfig, errorListener);
}
 
Example 10
Source File: ImageRequest.java    From TitanjumNote with Apache License 2.0 5 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to
 * the normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
        Config decodeConfig, Response.ErrorListener errorListener) {
    this(url, listener, maxWidth, maxHeight,
            ScaleType.CENTER_INSIDE, decodeConfig, errorListener);
}
 
Example 11
Source File: ImageRequest.java    From product-emm with Apache License 2.0 5 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to
 * the normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight,
        Config decodeConfig, Response.ErrorListener errorListener) {
    this(url, listener, maxWidth, maxHeight,
            ScaleType.CENTER_INSIDE, decodeConfig, errorListener);
}
 
Example 12
Source File: ImageRequestTest.java    From SaveVolley with Apache License 2.0 4 votes vote down vote up
@Test public void parseNetworkResponse_resizing() throws Exception {
    // This is a horrible hack but Robolectric doesn't have a way to provide
    // width and height hints for decodeByteArray. It works because the byte array
    // "file:fake" is ASCII encodable and thus the name in Robolectric's fake
    // bitmap creator survives as-is, and provideWidthAndHeightHints puts
    // "file:" + name in its lookaside map. I write all this because it will
    // probably break mysteriously at some point and I feel terrible about your
    // having to debug it.
    byte[] jpegBytes = "file:fake".getBytes();
    ShadowBitmapFactory.provideWidthAndHeightHints("fake", 1024, 500);
    NetworkResponse jpeg = new NetworkResponse(jpegBytes);

    // Scale the image uniformly (maintain the image's aspect ratio) so that
    // both dimensions (width and height) of the image will be equal to or
    // less than the corresponding dimension of the view.
    ScaleType scalteType = ScaleType.CENTER_INSIDE;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250); // exactly half
    verifyResize(jpeg, 511, 249, scalteType, 509, 249); // just under half
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500); // larger
    verifyResize(jpeg, 500, 500, scalteType, 500, 244); // keep same ratio

    // Specify only width, preserve aspect ratio
    verifyResize(jpeg, 512, 0, scalteType, 512, 250);
    verifyResize(jpeg, 800, 0, scalteType, 800, 390);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height, preserve aspect ratio
    verifyResize(jpeg, 0, 250, scalteType, 512, 250);
    verifyResize(jpeg, 0, 391, scalteType, 800, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);

    // Scale the image uniformly (maintain the image's aspect ratio) so that
    // both dimensions (width and height) of the image will be equal to or
    // larger than the corresponding dimension of the view.
    scalteType = ScaleType.CENTER_CROP;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    verifyResize(jpeg, 511, 249, scalteType, 511, 249);
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    verifyResize(jpeg, 500, 500, scalteType, 1024, 500);

    // Specify only width
    verifyResize(jpeg, 512, 0, scalteType, 512, 250);
    verifyResize(jpeg, 800, 0, scalteType, 800, 390);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height
    verifyResize(jpeg, 0, 250, scalteType, 512, 250);
    verifyResize(jpeg, 0, 391, scalteType, 800, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);

    // Scale in X and Y independently, so that src matches dst exactly. This
    // may change the aspect ratio of the src.
    scalteType = ScaleType.FIT_XY;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    verifyResize(jpeg, 511, 249, scalteType, 511, 249);
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    verifyResize(jpeg, 500, 500, scalteType, 500, 500);

    // Specify only width
    verifyResize(jpeg, 512, 0, scalteType, 512, 500);
    verifyResize(jpeg, 800, 0, scalteType, 800, 500);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height
    verifyResize(jpeg, 0, 250, scalteType, 1024, 250);
    verifyResize(jpeg, 0, 391, scalteType, 1024, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
}
 
Example 13
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 14
Source File: ImageRequestTest.java    From product-emm with Apache License 2.0 4 votes vote down vote up
@Test public void parseNetworkResponse_resizing() throws Exception {
    // This is a horrible hack but Robolectric doesn't have a way to provide
    // width and height hints for decodeByteArray. It works because the byte array
    // "file:fake" is ASCII encodable and thus the name in Robolectric's fake
    // bitmap creator survives as-is, and provideWidthAndHeightHints puts
    // "file:" + name in its lookaside map. I write all this because it will
    // probably break mysteriously at some point and I feel terrible about your
    // having to debug it.
    byte[] jpegBytes = "file:fake".getBytes();
    ShadowBitmapFactory.provideWidthAndHeightHints("fake", 1024, 500);
    NetworkResponse jpeg = new NetworkResponse(jpegBytes);

    // Scale the image uniformly (maintain the image's aspect ratio) so that
    // both dimensions (width and height) of the image will be equal to or
    // less than the corresponding dimension of the view.
    ScaleType scalteType = ScaleType.CENTER_INSIDE;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250); // exactly half
    verifyResize(jpeg, 511, 249, scalteType, 509, 249); // just under half
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500); // larger
    verifyResize(jpeg, 500, 500, scalteType, 500, 244); // keep same ratio

    // Specify only width, preserve aspect ratio
    verifyResize(jpeg, 512, 0, scalteType, 512, 250);
    verifyResize(jpeg, 800, 0, scalteType, 800, 390);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height, preserve aspect ratio
    verifyResize(jpeg, 0, 250, scalteType, 512, 250);
    verifyResize(jpeg, 0, 391, scalteType, 800, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);


    // Scale the image uniformly (maintain the image's aspect ratio) so that
    // both dimensions (width and height) of the image will be equal to or
    // larger than the corresponding dimension of the view.
    scalteType = ScaleType.CENTER_CROP;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    verifyResize(jpeg, 511, 249, scalteType, 511, 249);
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    verifyResize(jpeg, 500, 500, scalteType, 1024, 500);

    // Specify only width
    verifyResize(jpeg, 512, 0, scalteType, 512, 250);
    verifyResize(jpeg, 800, 0, scalteType, 800, 390);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height
    verifyResize(jpeg, 0, 250, scalteType, 512, 250);
    verifyResize(jpeg, 0, 391, scalteType, 800, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);


    // Scale in X and Y independently, so that src matches dst exactly. This
    // may change the aspect ratio of the src.
    scalteType = ScaleType.FIT_XY;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    verifyResize(jpeg, 511, 249, scalteType, 511, 249);
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    verifyResize(jpeg, 500, 500, scalteType, 500, 500);

    // Specify only width
    verifyResize(jpeg, 512, 0, scalteType, 512, 500);
    verifyResize(jpeg, 800, 0, scalteType, 800, 500);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height
    verifyResize(jpeg, 0, 250, scalteType, 1024, 250);
    verifyResize(jpeg, 0, 391, scalteType, 1024, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
}
 
Example 15
Source File: ImageRequestTest.java    From product-emm with Apache License 2.0 4 votes vote down vote up
@Test public void parseNetworkResponse_resizing() throws Exception {
    // This is a horrible hack but Robolectric doesn't have a way to provide
    // width and height hints for decodeByteArray. It works because the byte array
    // "file:fake" is ASCII encodable and thus the name in Robolectric's fake
    // bitmap creator survives as-is, and provideWidthAndHeightHints puts
    // "file:" + name in its lookaside map. I write all this because it will
    // probably break mysteriously at some point and I feel terrible about your
    // having to debug it.
    byte[] jpegBytes = "file:fake".getBytes();
    ShadowBitmapFactory.provideWidthAndHeightHints("fake", 1024, 500);
    NetworkResponse jpeg = new NetworkResponse(jpegBytes);

    // Scale the image uniformly (maintain the image's aspect ratio) so that
    // both dimensions (width and height) of the image will be equal to or
    // less than the corresponding dimension of the view.
    ScaleType scalteType = ScaleType.CENTER_INSIDE;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250); // exactly half
    verifyResize(jpeg, 511, 249, scalteType, 509, 249); // just under half
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500); // larger
    verifyResize(jpeg, 500, 500, scalteType, 500, 244); // keep same ratio

    // Specify only width, preserve aspect ratio
    verifyResize(jpeg, 512, 0, scalteType, 512, 250);
    verifyResize(jpeg, 800, 0, scalteType, 800, 390);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height, preserve aspect ratio
    verifyResize(jpeg, 0, 250, scalteType, 512, 250);
    verifyResize(jpeg, 0, 391, scalteType, 800, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);


    // Scale the image uniformly (maintain the image's aspect ratio) so that
    // both dimensions (width and height) of the image will be equal to or
    // larger than the corresponding dimension of the view.
    scalteType = ScaleType.CENTER_CROP;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    verifyResize(jpeg, 511, 249, scalteType, 511, 249);
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    verifyResize(jpeg, 500, 500, scalteType, 1024, 500);

    // Specify only width
    verifyResize(jpeg, 512, 0, scalteType, 512, 250);
    verifyResize(jpeg, 800, 0, scalteType, 800, 390);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height
    verifyResize(jpeg, 0, 250, scalteType, 512, 250);
    verifyResize(jpeg, 0, 391, scalteType, 800, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);


    // Scale in X and Y independently, so that src matches dst exactly. This
    // may change the aspect ratio of the src.
    scalteType = ScaleType.FIT_XY;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    verifyResize(jpeg, 511, 249, scalteType, 511, 249);
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    verifyResize(jpeg, 500, 500, scalteType, 500, 500);

    // Specify only width
    verifyResize(jpeg, 512, 0, scalteType, 512, 500);
    verifyResize(jpeg, 800, 0, scalteType, 800, 500);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height
    verifyResize(jpeg, 0, 250, scalteType, 1024, 250);
    verifyResize(jpeg, 0, 391, scalteType, 1024, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
}
 
Example 16
Source File: ImageRequestTest.java    From CrossBow with Apache License 2.0 4 votes vote down vote up
@Test public void parseNetworkResponse_resizing() throws Exception {
    // This is a horrible hack but Robolectric doesn't have a way to provide
    // width and height hints for decodeByteArray. It works because the byte array
    // "file:fake" is ASCII encodable and thus the name in Robolectric's fake
    // bitmap creator survives as-is, and provideWidthAndHeightHints puts
    // "file:" + name in its lookaside map. I write all this because it will
    // probably break mysteriously at some point and I feel terrible about your
    // having to debug it.
    byte[] jpegBytes = "file:fake".getBytes();
    ShadowBitmapFactory.provideWidthAndHeightHints("fake", 1024, 500);
    NetworkResponse jpeg = new NetworkResponse(jpegBytes);

    // Scale the image uniformly (maintain the image's aspect ratio) so that
    // both dimensions (width and height) of the image will be equal to or
    // less than the corresponding dimension of the view.
    ScaleType scalteType = ScaleType.CENTER_INSIDE;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250); // exactly half
    verifyResize(jpeg, 511, 249, scalteType, 509, 249); // just under half
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500); // larger
    verifyResize(jpeg, 500, 500, scalteType, 500, 244); // keep same ratio

    // Specify only width, preserve aspect ratio
    verifyResize(jpeg, 512, 0, scalteType, 512, 250);
    verifyResize(jpeg, 800, 0, scalteType, 800, 390);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height, preserve aspect ratio
    verifyResize(jpeg, 0, 250, scalteType, 512, 250);
    verifyResize(jpeg, 0, 391, scalteType, 800, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);


    // Scale the image uniformly (maintain the image's aspect ratio) so that
    // both dimensions (width and height) of the image will be equal to or
    // larger than the corresponding dimension of the view.
    scalteType = ScaleType.CENTER_CROP;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    verifyResize(jpeg, 511, 249, scalteType, 511, 249);
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    verifyResize(jpeg, 500, 500, scalteType, 1024, 500);

    // Specify only width
    verifyResize(jpeg, 512, 0, scalteType, 512, 250);
    verifyResize(jpeg, 800, 0, scalteType, 800, 390);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height
    verifyResize(jpeg, 0, 250, scalteType, 512, 250);
    verifyResize(jpeg, 0, 391, scalteType, 800, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);


    // Scale in X and Y independently, so that src matches dst exactly. This
    // may change the aspect ratio of the src.
    scalteType = ScaleType.FIT_XY;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    verifyResize(jpeg, 511, 249, scalteType, 511, 249);
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    verifyResize(jpeg, 500, 500, scalteType, 500, 500);

    // Specify only width
    verifyResize(jpeg, 512, 0, scalteType, 512, 500);
    verifyResize(jpeg, 800, 0, scalteType, 800, 500);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height
    verifyResize(jpeg, 0, 250, scalteType, 1024, 250);
    verifyResize(jpeg, 0, 391, scalteType, 1024, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
}
 
Example 17
Source File: ImageRequest.java    From SaveVolley with Apache License 2.0 4 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to
 * the normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight, Config decodeConfig, Response.ErrorListener errorListener) {
    this(url, listener, maxWidth, maxHeight, ScaleType.CENTER_INSIDE, decodeConfig,
            errorListener);
}
 
Example 18
Source File: ImageRequest.java    From SaveVolley with Apache License 2.0 4 votes vote down vote up
/**
 * For API compatibility with the pre-ScaleType variant of the constructor. Equivalent to
 * the normal constructor with {@code ScaleType.CENTER_INSIDE}.
 */
@Deprecated
public ImageRequest(String url, Response.Listener<Bitmap> listener, int maxWidth, int maxHeight, Config decodeConfig, Response.ErrorListener errorListener) {
    this(url, listener, maxWidth, maxHeight, ScaleType.CENTER_INSIDE, decodeConfig,
            errorListener);
}
 
Example 19
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 20
Source File: ImageRequestTest.java    From volley with Apache License 2.0 4 votes vote down vote up
@Test
public void parseNetworkResponse_resizing() throws Exception {
    // This is a horrible hack but Robolectric doesn't have a way to provide
    // width and height hints for decodeByteArray. It works because the byte array
    // "file:fake" is ASCII encodable and thus the name in Robolectric's fake
    // bitmap creator survives as-is, and provideWidthAndHeightHints puts
    // "file:" + name in its lookaside map. I write all this because it will
    // probably break mysteriously at some point and I feel terrible about your
    // having to debug it.
    byte[] jpegBytes = "file:fake".getBytes(StandardCharsets.UTF_8);
    ShadowBitmapFactory.provideWidthAndHeightHints("fake", 1024, 500);
    NetworkResponse jpeg = new NetworkResponse(jpegBytes);

    // Scale the image uniformly (maintain the image's aspect ratio) so that
    // both dimensions (width and height) of the image will be equal to or
    // less than the corresponding dimension of the view.
    ScaleType scalteType = ScaleType.CENTER_INSIDE;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250); // exactly half
    verifyResize(jpeg, 511, 249, scalteType, 509, 249); // just under half
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500); // larger
    verifyResize(jpeg, 500, 500, scalteType, 500, 244); // keep same ratio

    // Specify only width, preserve aspect ratio
    verifyResize(jpeg, 512, 0, scalteType, 512, 250);
    verifyResize(jpeg, 800, 0, scalteType, 800, 390);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height, preserve aspect ratio
    verifyResize(jpeg, 0, 250, scalteType, 512, 250);
    verifyResize(jpeg, 0, 391, scalteType, 800, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);

    // Scale the image uniformly (maintain the image's aspect ratio) so that
    // both dimensions (width and height) of the image will be equal to or
    // larger than the corresponding dimension of the view.
    scalteType = ScaleType.CENTER_CROP;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    verifyResize(jpeg, 511, 249, scalteType, 511, 249);
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    verifyResize(jpeg, 500, 500, scalteType, 1024, 500);

    // Specify only width
    verifyResize(jpeg, 512, 0, scalteType, 512, 250);
    verifyResize(jpeg, 800, 0, scalteType, 800, 390);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height
    verifyResize(jpeg, 0, 250, scalteType, 512, 250);
    verifyResize(jpeg, 0, 391, scalteType, 800, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);

    // Scale in X and Y independently, so that src matches dst exactly. This
    // may change the aspect ratio of the src.
    scalteType = ScaleType.FIT_XY;

    // Exact sizes
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    verifyResize(jpeg, 511, 249, scalteType, 511, 249);
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    verifyResize(jpeg, 500, 500, scalteType, 500, 500);

    // Specify only width
    verifyResize(jpeg, 512, 0, scalteType, 512, 500);
    verifyResize(jpeg, 800, 0, scalteType, 800, 500);
    verifyResize(jpeg, 1024, 0, scalteType, 1024, 500);

    // Specify only height
    verifyResize(jpeg, 0, 250, scalteType, 1024, 250);
    verifyResize(jpeg, 0, 391, scalteType, 1024, 391);
    verifyResize(jpeg, 0, 500, scalteType, 1024, 500);

    // No resize
    verifyResize(jpeg, 0, 0, scalteType, 1024, 500);
}