Android Open Source - Android-Bootstrap Image Utils






From Project

Back to project page Android-Bootstrap.

License

The source code is released under:

MIT License

If you think the Android project Android-Bootstrap listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.beardedhen.androidbootstrap.utils;
//from w  w w  . j ava  2s . co  m

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;

public class ImageUtils
{

    public static Bitmap getCircleBitmap(Bitmap bitmap)
    {
        return getCircleBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight());
    }

    public static Bitmap getCircleBitmap(Bitmap bitmap, int width, int height)
    {
        Bitmap croppedBitmap = scaleCenterCrop(bitmap, width, height);
        Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();

        final Rect rect = new Rect(0, 0, width, height);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);

        int radius =  (width > height) ? height : width;
        radius /= 2;

        canvas.drawCircle(width / 2, height / 2, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(croppedBitmap, rect, rect, paint);

        return output;
    }

    public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth)
    {
        int sourceWidth = source.getWidth();
        int sourceHeight = source.getHeight();

        float xScale = (float) newWidth / sourceWidth;
        float yScale = (float) newHeight / sourceHeight;
        float scale = Math.max(xScale, yScale);

        float scaledWidth = scale * sourceWidth;
        float scaledHeight = scale * sourceHeight;

        float left = (newWidth - scaledWidth) / 2;
        float top = (newHeight - scaledHeight) / 2;

        RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

        Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
        Canvas canvas = new Canvas(dest);
        canvas.drawBitmap(source, null, targetRect, null);

        return dest;
    }
}




Java Source Code List

com.beardedhen.androidbootstrap.BootstrapButton.java
com.beardedhen.androidbootstrap.BootstrapCircleThumbnail.java
com.beardedhen.androidbootstrap.BootstrapEditText.java
com.beardedhen.androidbootstrap.BootstrapThumbnail.java
com.beardedhen.androidbootstrap.FontAwesomeText.java
com.beardedhen.androidbootstrap.FontAwesome.java
com.beardedhen.androidbootstrap.utils.AutoResizeTextView.java
com.beardedhen.androidbootstrap.utils.ImageUtils.java
com.beardedhen.androidbootstraptest.MainActivity.java