2017-02-18 135 views
10

我試圖找出當縮放圖像時,android正在做什麼,特別是「centercrop」類型。所以要找到答案,我搜索了ImageView源代碼並找到它hereImageview縮放方法「centercrop」作爲代碼

所以我試着爲這段代碼:

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; 
} 

但它不給我相同的結果。我究竟做錯了什麼 ?

下面有一個例子:

原始圖片

enter image description here

原單ImageView的Centercrop

enter image description here

試過代碼

enter image description here

編輯: 的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> 

所以我的ImageView的XML是全屏。那就是爲什麼我使用screenSize來處理它。

碼,我如何應用它

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

詳細說明: 我只是試圖得到儘可能ImageView.setScaleType(ScaleType.CENTER_CROP)同樣的效果。所以我的代碼應該像原來的setScaleType方法一樣。爲什麼我需要它作爲代碼?因爲在我的情況下,我無法獲得ImageView的繪圖緩存,但我必須以某種方式處理&。

+0

後完全繪製的代碼?你如何在ImageView上應用它? –

+0

1)您正在從'ImageView'獲取位圖,其中drawable可能已經從原始位置被更改; 2)您正在使用屏幕的大小而不是目標視圖的大小; 3)您正在創建縮放的位圖,而不是僅使用'scaleType =「matrix」'。我需要看到更多的代碼來理解你想要完成的事情,但這應該是一個簡單的修復。將您的活動代碼與具有目標視圖的XML佈局一起發佈,以及您正試圖解決的確切問題的更詳細的描述。 –

+0

我使用一些代碼編輯了我的帖子,並更好地描述了我的問題。希望這一次很明白要理解。感謝您的幫助 –

回答

1

我改編自來源。 它將與您一起改變您向Matrix應用的回報。

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; 
} 

然後用:

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