Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

android - 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?

question from:https://stackoverflow.com/questions/15381571/equivalent-of-imageview-scaletype-centercrop-to-bitmap-drawable-gravity

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...