private void updateBitmapShader ( ) { Drawable drawable = getDrawable ( ) ; Bitmap bitmap = null ; if ( ( null != drawable ) && ( drawable instanceof BitmapDrawable ) ) { bitmap = ( ( BitmapDrawable ) drawable ) . getBitmap ( ) ; } // Clear the shader & abort is the new bitmap is null if ( null == bitmap ) { mBitmapPaint . setShader ( null ) ; return ; } int bitmapWidth = bitmap . getWidth ( ) ; int bitmapHeight = bitmap . getHeight ( ) ; float x = 0 , y = 0 ; int diameter = mRadius * 2 ; // Offset takes the border width in to account when calculating the the scale int borderWidth = shouldDrawBorder ( ) ? mBorderWidth : 0 ; int offset = ( borderWidth > 0 ) ? ( borderWidth * 2 ) : 0 ; // Consider shadow too offset += mShadowRadius * 1.5f ; float scale = ( float ) ( diameter - offset ) / ( float ) Math . min ( bitmapHeight , bitmapWidth ) ; x = ( mWidth - bitmapWidth * scale ) * 0.5f ; y = ( mHeight - bitmapHeight * scale ) * 0.5f ; // Apply scale and translation to matrix Matrix matrix = new Matrix ( ) ; matrix . setScale ( scale , scale ) ; matrix . postTranslate ( Math . round ( x ) , Math . round ( y ) ) ; // Create the BitmapShader and apply the Matrix BitmapShader shader = new BitmapShader ( bitmap , Shader . TileMode . CLAMP , Shader . TileMode . CLAMP ) ; shader . setLocalMatrix ( matrix ) ; mBitmapPaint . setShader ( shader ) ; }
Updates BitmapShader to draw the bitmap in CENTER_CROP mode .