Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of ImageView scaleType 'centerCrop' to bitmap drawable gravity

I have created an activity with a header image. This header image is originally created int the Activity's layout xml using an ImageView where the scaleType is set to centerCrop. This does what I want, it centers the image, clipping it left and right in portrait mode, showing all in landscape mode.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingTop="30dp"     android:paddingBottom="6dp"     android:paddingLeft="6dp"     android:paddingRight="6dp"     android:background="#919EAC"     android:orientation="vertical" >  <ImageView     android:id="@+id/header_image"     android:layout_width="match_parent"     android:layout_height="120dp"     android:contentDescription="@string/header_description"     android:scaleType="centerCrop"     android:src="@drawable/header" />      .... <other views> ... 

I'd like to replace this with a background drawable so that I can use the header image space to display data, and it saves me repeating the same layout in the different activities.

For this reason I have created a drawable that I can refer to in the android:background attribute:

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >         <item>             <shape android:shape="rectangle" >                 <solid android:color="#919EAC" />             </shape>         </item>         <item>             <bitmap                  android:antialias="true"                 android:gravity="top|center_horizontal"                 android:src="@drawable/header" />         </item>     </layer-list> 

This drawable defines the colored background that otherwise the layout would define and it defines a header image that otherwise would be included in the layout as an ImageView.

However now my image is scaled, while I would like it to be cropped.

Checking the Android documentation I tried other gravity modes such as

    android:gravity="top|clip_horizontal" 

But it still seems to display/scale differently than the image view.

What would be the correct definition of the background drawable?

like image 561
M. le Rutte Avatar asked Mar 13 '13 09:03

M. le Rutte


People also ask

What is a bitmap drawable?

android.graphics.drawable.BitmapDrawable. A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a BitmapDrawable from a file path, an input stream, through XML inflation, or from a Bitmap object. It can be defined in an XML file with the <bitmap> element.

What is SRC in ImageView?

android:scaleType. Controls how the image should be resized or moved to match the size of this ImageView. android:src. Sets a drawable as the content of this ImageView. android:tint.


2 Answers

To center crop a bitmap in code do this:

public static Bitmap cropCenter(Bitmap bmp) {     int dimension = Math.min(bmp.getWidth(), bmp.getHeight());     return ThumbnailUtils.extractThumbnail(bmp, dimension, dimension); } 

I don't know of any other way to centerCrop a bitmap xml wise.

Hope it helps anyone.

I found this solution from this post: Android Crop Center of Bitmap

like image 139
Arnold Balliu Avatar answered Sep 19 '22 20:09

Arnold Balliu


I solved this problem with a decorator which maintains the aspect ratio of the underlying drawable: https://gist.github.com/rudchenkos/e33dc0d6669a61dde9d6548f6c3e0e7e

Unfortunately, there is no way to apply it from XML so I do that at the very beginning of my splash Activity:

public class SplashActivity extends AppCompatActivity {     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);          final Drawable bg = getResources().getDrawable(R.drawable.screen);         getWindow().setBackgroundDrawable(new CenterCropDrawable(bg));          ...     } } 

That is roughly equivalent to specifying the drawable as android:windowBackground in the Activity theme

like image 28
Sergii Rudchenko Avatar answered Sep 20 '22 20:09

Sergii Rudchenko