Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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();

    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 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;
    }
}