package com.nostra13.universalimageloader.core.download.handlers;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.image.SimpleTextImage;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * Renderer for text on a coloured background. Nice and simple image to mimic the way
 * that Gmail shows initials of people in a list without an image
 *
 * @author Matt Allen
 * @project UniversalImageLoader
 */
public class TextSchemeHandler extends SchemeHandler
{
	@Override
	public InputStream getStreamForPath(Context context, String path, Object optionForDownloader, int connectTimeout, int readTimeout)
	{
		SimpleTextImage image = new SimpleTextImage();
		image.fromUrl(path);
		TextView textView = getTextView(context);
		String text = image.getText();
		textView.setText(image.isUsingInitials() ? image.getInitials() : text);
		textView.setBackgroundColor(image.getColour());
		switch (image.getTypeFace())
		{
			case SimpleTextImage.TYPEFACE_MONOSPACE:
				textView.setTypeface(Typeface.MONOSPACE);
				break;

			case SimpleTextImage.TYPEFACE_CONDENSED:
				textView.setTypeface(Typeface.create("sans-serif-condensed", Typeface.NORMAL));
				break;

			case SimpleTextImage.TYPEFACE_THIN:
				textView.setTypeface(Typeface.create("sans-serif-thin", Typeface.SANS_SERIF.getStyle()));
				break;
		}
		Drawable bgDrawable = textView.getBackground();
		Bitmap bitmap = Bitmap.createBitmap(384, 384, Bitmap.Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		bgDrawable.draw(canvas);
		textView.draw(canvas);
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
		byte[] bitmapdata = bos.toByteArray();
		return new ByteArrayInputStream(bitmapdata);
	}

	private TextView getTextView(Context context)
	{
		TextView textView = new TextView(context);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
		params.gravity = Gravity.CENTER;
		textView.setLayoutParams(params);
		textView.setGravity(Gravity.CENTER);
		textView.setPadding(16, 46, 16, 16);
		textView.layout(0, 0, 384, 384);
		textView.requestLayout();
		textView.setTextSize(68);
		textView.setTextColor(Color.WHITE);
		return textView;
	}
}

@@ -264,16 +264,48 @@
 awable;%0A
+import android.util.TypedValue;%0A
 import a
@@ -2307,25 +2307,25 @@
 tParams.
-MATCH_PAR
+WRAP_CONT
 ENT, Lin
@@ -2351,17 +2351,17 @@
 ams.
-MATCH_PAR
+WRAP_CONT
 ENT)
@@ -2487,68 +2487,86 @@
 iew.
-setPadding(16, 46, 16, 16);%0A%09%09textView.layout(0, 0, 384, 384
+layout(0, 0, 384, 384);%0A%09%09textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, 220
 );%0A%09
@@ -2610,19 +2610,30 @@
 .set
-TextSize(68
+Padding(16, 46, 16, 16
 );%0A%09

Fix for text scaling in simple text image