Search in sources :

Example 1 with ScaleType

use of android.widget.ImageView.ScaleType in project FastDev4Android by jiangqqlmj.

the class ImageRequestTest method parseNetworkResponse_resizing.

@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
    // exactly half
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    // just under half
    verifyResize(jpeg, 511, 249, scalteType, 509, 249);
    // larger
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    // keep same ratio
    verifyResize(jpeg, 500, 500, scalteType, 500, 244);
    // 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);
}
Also used : ScaleType(android.widget.ImageView.ScaleType) NetworkResponse(com.android.volley.NetworkResponse) Test(org.junit.Test)

Example 2 with ScaleType

use of android.widget.ImageView.ScaleType in project android-volley by mcxiaoke.

the class ImageRequestTest method parseNetworkResponse_resizing.

@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
    // exactly half
    verifyResize(jpeg, 512, 250, scalteType, 512, 250);
    // just under half
    verifyResize(jpeg, 511, 249, scalteType, 509, 249);
    // larger
    verifyResize(jpeg, 1080, 500, scalteType, 1024, 500);
    // keep same ratio
    verifyResize(jpeg, 500, 500, scalteType, 500, 244);
    // 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);
}
Also used : ScaleType(android.widget.ImageView.ScaleType) NetworkResponse(com.android.volley.NetworkResponse) Test(org.junit.Test)

Example 3 with ScaleType

use of android.widget.ImageView.ScaleType in project TouchImageView by MikeOrtiz.

the class SwitchScaleTypeExampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_switch_scaletype_example);
    activity = this;
    image = (TouchImageView) findViewById(R.id.img);
    //
    // Set next scaleType with each button click
    //
    image.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            index = ++index % scaleTypes.length;
            ScaleType currScaleType = scaleTypes[index];
            image.setScaleType(currScaleType);
            Toast.makeText(activity, "ScaleType: " + currScaleType, Toast.LENGTH_SHORT).show();
        }
    });
}
Also used : ScaleType(android.widget.ImageView.ScaleType) OnClickListener(android.view.View.OnClickListener) View(android.view.View)

Example 4 with ScaleType

use of android.widget.ImageView.ScaleType in project enroscar by stanfy.

the class ImageViewHiddenMethods method configureBounds.

public static void configureBounds(final ImageView imageView) {
    final Drawable mDrawable = imageView.getDrawable();
    if (mDrawable == null || !imageView.isHaveFrame()) {
        return;
    }
    Matrix mDrawMatrix = null;
    final int mPaddingLeft = imageView.getPaddingLeft();
    final int mPaddingTop = imageView.getPaddingTop();
    final int mPaddingRight = imageView.getPaddingRight();
    final int mPaddingBottom = imageView.getPaddingBottom();
    final ScaleType mScaleType = imageView.getScaleType();
    final Matrix mMatrix = imageView.getImageMatrix();
    final int dwidth = mDrawable.getIntrinsicWidth();
    final int dheight = mDrawable.getIntrinsicHeight();
    final int vwidth = imageView.getWidth() - mPaddingLeft - mPaddingRight;
    final int vheight = imageView.getHeight() - mPaddingTop - mPaddingBottom;
    final boolean fits = (dwidth < 0 || vwidth == dwidth) && (dheight < 0 || vheight == dheight);
    final float half = 0.5f;
    final RectF mTempSrc = SRC_RECT, mTempDst = DST_RECT;
    if (dwidth <= 0 || dheight <= 0 || ScaleType.FIT_XY == mScaleType) {
        /* If the drawable has no intrinsic size, or we're told to
            scaletofit, then we just fill our entire view.
       */
        mDrawable.setBounds(0, 0, vwidth, vheight);
        mDrawMatrix = null;
    } else {
        // We need to do the scaling ourself, so have the drawable
        // use its native size.
        mDrawable.setBounds(0, 0, dwidth, dheight);
        if (ScaleType.MATRIX == mScaleType) {
            // Use the specified matrix as-is.
            if (mMatrix.isIdentity()) {
                mDrawMatrix = null;
            } else {
                mDrawMatrix = mMatrix;
            }
        } else if (fits) {
            // The bitmap fits exactly, no transform needed.
            mDrawMatrix = null;
        } else if (ScaleType.CENTER == mScaleType) {
            // Center bitmap in view, no scaling.
            mDrawMatrix = mMatrix;
            mDrawMatrix.setTranslate((int) ((vwidth - dwidth) * half + half), (int) ((vheight - dheight) * half + half));
        } else if (ScaleType.CENTER_CROP == mScaleType) {
            mDrawMatrix = mMatrix;
            float scale;
            float dx = 0, dy = 0;
            if (dwidth * vheight > vwidth * dheight) {
                scale = (float) vheight / (float) dheight;
                dx = (vwidth - dwidth * scale) * half;
            } else {
                scale = (float) vwidth / (float) dwidth;
                dy = (vheight - dheight * scale) * half;
            }
            mDrawMatrix.setScale(scale, scale);
            mDrawMatrix.postTranslate((int) (dx + half), (int) (dy + half));
        } else if (ScaleType.CENTER_INSIDE == mScaleType) {
            mDrawMatrix = mMatrix;
            float scale;
            float dx;
            float dy;
            if (dwidth <= vwidth && dheight <= vheight) {
                scale = 1.0f;
            } else {
                scale = Math.min((float) vwidth / (float) dwidth, (float) vheight / (float) dheight);
            }
            dx = (int) ((vwidth - dwidth * scale) * half + half);
            dy = (int) ((vheight - dheight * scale) * half + half);
            mDrawMatrix.setScale(scale, scale);
            mDrawMatrix.postTranslate(dx, dy);
        } else {
            // Generate the required transform.
            mTempSrc.set(0, 0, dwidth, dheight);
            mTempDst.set(0, 0, vwidth, vheight);
            mDrawMatrix = mMatrix;
            mDrawMatrix.setRectToRect(mTempSrc, mTempDst, scaleTypeToScaleToFit(mScaleType));
        }
    }
    //    final boolean matrixChanged = mDrawMatrix == null && mMatrix != null
    //        || !mDrawMatrix.equals(mMatrix);
    //    if (matrixChanged) {
    imageView.setDrawMatrix(mDrawMatrix);
//    }
}
Also used : RectF(android.graphics.RectF) Matrix(android.graphics.Matrix) ScaleType(android.widget.ImageView.ScaleType) Drawable(android.graphics.drawable.Drawable)

Example 5 with ScaleType

use of android.widget.ImageView.ScaleType in project android_packages_apps_DUI by DirtyUnicorns.

the class SmartBarEditor method swapConfigs.

private void swapConfigs(OpaLayout v1, OpaLayout v2) {
    ButtonConfig config1 = v1.getButton().getButtonConfig();
    ButtonConfig config2 = v2.getButton().getButtonConfig();
    int[] padding1 = SmartBarHelper.getViewPadding(v1);
    int[] padding2 = SmartBarHelper.getViewPadding(v2);
    ScaleType scale1 = v1.getButton().getScaleType();
    ScaleType scale2 = v2.getButton().getScaleType();
    v1.getButton().setButtonConfig(config2);
    v2.getButton().setButtonConfig(config1);
    mHost.setButtonDrawable(v1.getButton());
    mHost.setButtonDrawable(v2.getButton());
    SmartBarHelper.applyPaddingToView(v1, padding2);
    SmartBarHelper.applyPaddingToView(v2, padding1);
    v1.getButton().setScaleType(scale2);
    v2.getButton().setScaleType(scale1);
}
Also used : ButtonConfig(com.android.internal.utils.du.Config.ButtonConfig) ScaleType(android.widget.ImageView.ScaleType)

Aggregations

ScaleType (android.widget.ImageView.ScaleType)6 NetworkResponse (com.android.volley.NetworkResponse)3 Test (org.junit.Test)3 Matrix (android.graphics.Matrix)1 RectF (android.graphics.RectF)1 Drawable (android.graphics.drawable.Drawable)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 ButtonConfig (com.android.internal.utils.du.Config.ButtonConfig)1