9

Im trying to find out what android is doing when it scales an Image, specifically the "centercrop" type. So to find an answer I searched the ImageView sourcecode and find it here.

So what I tried is this code :

public Bitmap buildBluredBoxBackground () {
        int [] screenSize = Utilities.getScreenSize(mainActivityContext); //screensize[0] = x and [1] is y
        Matrix mDrawMatrix = new Matrix();

        Bitmap bitmap = ((BitmapDrawable)fullscreenViewHolder.imageViewArt.getDrawable()).getBitmap();

        float scale;
        float dx = 0, dy = 0;

        if (bitmap.getWidth() * screenSize[1] > screenSize[0] * bitmap.getHeight()) {
            scale = (float) screenSize[1] / (float) bitmap.getHeight();
            dx = (screenSize[0] - bitmap.getWidth() * scale) * 0.5f;
        } else {
            scale = (float) screenSize[0] / (float) bitmap.getWidth();
            dy = (screenSize[1] - bitmap.getHeight() * scale) * 0.5f;
        }

        mDrawMatrix.setScale(scale, scale);
        mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy));

        result = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),mDrawMatrix,true);

        ... //Some processing work

        return result;
}

But it is not giving me the same result. What am I doing wrong ?

Heres an example:

Original Picture

enter image description here

Orginal ImageView Centercrop

enter image description here

Tried Code

enter image description here

Edit: XML of the ImageView

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/imageViewFullscreenArt"/>
            <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/imageViewFullscreenArtBluredBox"/>
</FrameLayout>

So my ImageView is fullscreened. Thats why Im using the screenSize to process it.

Code how I'm applying it

Bitmap bluredBoxBackground  = buildBluredBoxBackground();
imageViewBluredBox.setImageDrawable(new BitmapDrawable(getResources(),bluredBoxBackground));

Detailed Description: Im just trying to get the same effect as ImageView.setScaleType(ScaleType.CENTER_CROP). So my code should do the same like the original setScaleType method. Why do I need it as code ? Because in my situation I can't get the drawingcache of my ImageView but I have to process & edit it somehow.

Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
Ahmet K
  • 713
  • 18
  • 42
  • Post the code for complete drawable? How are you applying it on ImageView? – Anurag Singh Feb 21 '17 at 08:22
  • 1) You are getting the bitmap from the `ImageView` where the drawable may have been altered from the original; 2) You are using the size of the screen instead of the size of the target view; 3) You are creating a scaled bitmap rather than just using `scaleType="matrix"`. I need to see more code to understand what you are trying to accomplish, but this should be a simple fix. Post the code for your activity along with the XML layout that has the target view, along with a little more detailed description of the exact problem you are trying to solve. – kris larson Feb 21 '17 at 09:48
  • I edited my post with some code and better description of my problem. Hope this time it is clear to understand. Thanks for your help – Ahmet K Feb 21 '17 at 10:46
  • @AhmetKazaman DId you solved this or you need solution? – Anurag Singh Feb 24 '17 at 08:47
  • You set drawingcache true and get drawingcache of – Qamar Feb 27 '17 at 09:33

1 Answers1

0

I adapted from teh source. It will work with you change the return to Matrix how you apply.

public Matrix buildBluredBoxBackground () {

    int dwidth = imageView.getDrawable().getIntrinsicWidth();
    int dheight = imageView.getDrawable().getIntrinsicHeight();

    int vwidth = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
    int vheight = imageView.getHeight() - imageView.getPaddingTop() - imageView.getPaddingBottom();

    Matrix mDrawMatrix = imageView.getImageMatrix();
    Bitmap bMap = imageView.getDrawingCache();

    float scale;
    float dx = 0, dy = 0;

    if (dwidth * vheight > vwidth * dheight) {
        scale = (float) vheight / (float) dheight;
        dx = (vwidth - dwidth * scale) * 0.5f;
    } else {
        scale = (float) vwidth / (float) dwidth;
        dy = (vheight - dheight * scale) * 0.5f;
    }
    mDrawMatrix.setScale(scale, scale);
    mDrawMatrix.postTranslate(Math.round(dx), Math.round(dy));
    return mDrawMatrix;
}

And then use:

Matrix bluredBoxBackground = buildBluredBoxBackground();
imageViewBluredBox.setImageMatrix(bluredBoxBackground));
imageViewBluredBox.invalidate();