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.Rect;

public class Main {
    public static Bitmap drawTextToBitmap(Context gContext, String gText, int frontColor, int backColor) {
        Resources resources = gContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        int w = 1536, h = 1280;
        Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
        Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
        android.graphics.Bitmap.Config bitmapConfig = bmp.getConfig();
        Canvas canvas = new Canvas(bmp);

        // new antialised Paint
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        // text color - #3D3D3D
        paint.setColor(frontColor);
        // text size in pixels
        paint.setTextSize((int) (400 * scale));
        // text shadow
        //paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

        // draw text to the Canvas center
        if (backColor != -1) {
            canvas.drawColor(backColor);
        }
        Rect bounds = new Rect();
        paint.getTextBounds(gText, 0, gText.length(), bounds);
        int x = (bmp.getWidth() - bounds.width()) / 2;
        int y = (bmp.getHeight() + bounds.height()) / 2;
        canvas.drawText(gText, x, y, paint);
        return bmp;
    }
}