package lowmans.test.bitmaptext;


import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Rect;

import android.graphics.Typeface;

import android.os.Bundle;

import android.util.Log;

import android.widget.ImageView;


public class BitmapTextActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);


Bitmap bitmap = makeBitmapWithText("狂~!!", "", 68f, 200, 100);

ImageView v = (ImageView) findViewById(R.id.image);

v.setImageBitmap(bitmap);


}


// String _txt, String _fontName, float _text_size, int __width, int __height


public Bitmap makeBitmapWithText(String _txt, String _fontName, float _text_size, int _width, int _height) {

Bitmap textBitmap = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);

textBitmap.eraseColor(0x88888888);

Canvas canvas = new Canvas(textBitmap);

Typeface typeface = Typeface.create(_fontName, Typeface.NORMAL);

Paint textPaint = new Paint();

textPaint.setTextSize(_text_size);

textPaint.setAntiAlias(true);

textPaint.setColor(Color.WHITE);

// textPaint.setStrokeWidth(5);

// textPaint.setStrokeCap(Paint.Cap.ROUND);

// textPaint.setARGB(255, 0, 0, 0);

textPaint.setTypeface(typeface);

Rect bounds = new Rect();

textPaint.getTextBounds(_txt, 0, _txt.length(), bounds);

canvas.drawText(_txt, 0.0f, Math.abs(bounds.top - bounds.bottom), textPaint);


// size check~!!!

Log.i("#@#", "measureText : " + textPaint.measureText(_txt));

Log.i("#@#", "textPaint Rect .. left : " + bounds.left + " , top : " + bounds.top + " , right : " + bounds.right + " , bottom : "

+ bounds.bottom);

return textBitmap;

}

}






======== 정렬 추가된 소스 =============


private static Bitmap getTextImageWithSizeDetail(String _txt, String _fontName, float _text_size, int _width, int _height,

int _colorCode, int _shadowColorCode, float _shadowOffsetX, float _shadowOffsetY, int align) {


Bitmap textBitmap = Bitmap.createBitmap(_width, _height, Bitmap.Config.ARGB_8888);

// textBitmap.eraseColor(0x8844ff44);

Canvas canvas = new Canvas(textBitmap);

Typeface typeface = Typeface.create(_fontName, Typeface.NORMAL);

Paint textPaint = new Paint();

setAutoTextSize(_text_size, textPaint, _txt, _width);

textPaint.setAntiAlias(true);

textPaint.setColor(_colorCode);

textPaint.setShadowLayer(2f, _shadowOffsetX, _shadowOffsetY, _shadowColorCode);

textPaint.setTypeface(typeface);

Rect bounds = new Rect();

textPaint.getTextBounds(_txt, 0, _txt.length(), bounds);

float measureTxt = textPaint.measureText(_txt);

float w = 0f, h = Math.abs(bounds.top); // 상단정렬

// h = _height - Math.abs(bounds.bottom); //하단정렬

if (align == -1) {

h += (_height - h - Math.abs(bounds.bottom)) / 2;

} else if (align == 0) {

w = (_width - measureTxt) / 2;

h += (_height - h - Math.abs(bounds.bottom)) / 2;

} else if (align == 1) {

w = _width - measureTxt;

h += (_height - h - Math.abs(bounds.bottom)) / 2;

}

canvas.drawText(_txt, w, h, textPaint);


return textBitmap;

}


static void setAutoTextSize(float textSize, Paint paint, String text, int width) {

paint.setTextSize(textSize);

float _measureSize = paint.measureText(text);

if (_measureSize >= width) {

setAutoTextSize(--textSize, paint, text, width);

}

}