Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Typeface;

public class Main {
    private static final Rect junkRectangle = new Rect();

    public static Bitmap generateAvatar(Context context, String username, int avatarDimenResourceID) {
        Resources resources = context.getResources();
        return generateAvatar(username, resources.getDimensionPixelSize(avatarDimenResourceID));
    }

    public static Bitmap generateAvatar(String username, int size) {
        return generateAvatar(username, size, false);
    }

    public static Bitmap generateAvatar(String username, int size, boolean glossy) {
        Bitmap bitmap = bitmapForSize(size);
        if (bitmap == null) {
            return null; // out of memory?
        }
        return decorate(bitmap, labelFor(username), colorFor(username), glossy);
    }

    private static Bitmap bitmapForSize(int size) {
        try {
            return Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError e) {
            return null;
        }
    }

    private static Bitmap decorate(Bitmap bitmap, char label, int color, boolean glossy) {

        Canvas canvas = new Canvas(bitmap);

        canvas.drawColor(color);

        String labelString = String.valueOf(label);
        Paint textPaint = getTextPaint(bitmap.getHeight() * 0.75f);
        float cx = bitmap.getWidth() / 2;
        float cy = bitmap.getHeight() / 2;
        float oy = 0;

        synchronized (junkRectangle) {
            textPaint.getTextBounds(labelString, 0, 1, junkRectangle);
            oy = (junkRectangle.top + junkRectangle.bottom) / 2;
        }

        canvas.drawText(labelString, cx, cy - oy, textPaint);

        if (glossy) {
            canvas.drawPath(gloss(bitmap.getWidth()), getGlossPaint());
        }

        return bitmap;

    }

    private static char labelFor(String username) {
        return Character.toUpperCase(username.charAt(0));
    }

    private static int colorFor(String username) {
        return 0xFFFFFFFF & username.hashCode();
    }

    private static Paint getTextPaint(float size) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setShadowLayer(1, 1, 1, 0xFF000000);
        paint.setColor(0xFFFFFFFF);
        paint.setTextAlign(Align.CENTER);
        paint.setTextSize(size);
        paint.setTypeface(Typeface.DEFAULT);
        return paint;

    }

    private static Path gloss(int size) {
        Path gloss = new Path();
        gloss.moveTo(0, size);
        gloss.cubicTo(0, size, size * 3 / 4, size * 3 / 4, size, 0);
        gloss.lineTo(0, 0);
        gloss.close();
        return gloss;
    }

    private static Paint getGlossPaint() {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(0x22FFFFFF);
        return paint;
    }
}