package com.linju.android_property.viewutils;
import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ImageView;
import java.util.Random;
import com.linju.android_property2.R;
public class LetterImageView extends ImageView {
private char mLetter;
private Paint mTextPaint;
private Paint mBackgroundPaint;
private int mTextColor = Color.WHITE;
private int mTextBgColor = Color.BLACK;
private boolean isOval;
public LetterImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(mTextColor);
mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBackgroundPaint.setStyle(Paint.Style.FILL);
mBackgroundPaint.setColor(randomColor());
}
public char getLetter() {
return mLetter;
}
public void setLetter(char letter) {
mLetter = letter;
invalidate();
}
public int getTextColor() {
return mTextColor;
}
public void setTextColor(int textColor) {
mTextColor = textColor;
invalidate();
}
public void setOval(boolean oval) {
isOval = oval;
}
public boolean isOval() {
return isOval;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (getDrawable() == null) {
// Set a text font size based on the height of the view
mTextPaint.setTextSize(canvas.getHeight() - getTextPadding() * 3.5f);
if (isOval()) {
canvas.drawCircle(canvas.getWidth() / 2f, canvas.getHeight() / 2f, Math.min(canvas.getWidth(), canvas.getHeight()) / 2f,
mBackgroundPaint);
} else {
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mBackgroundPaint);
}
// Measure a text
Rect textBounds = new Rect();
mTextPaint.getTextBounds(String.valueOf(mLetter), 0, 1, textBounds);
float textWidth = mTextPaint.measureText(String.valueOf(mLetter));
float textHeight = textBounds.height();
// Draw the text
canvas.drawText(String.valueOf(mLetter), canvas.getWidth() / 2f - textWidth / 2f,
canvas.getHeight() / 2f + textHeight / 2.5f /*- 10*/, mTextPaint);
}
}
private float getTextPadding() {
// Set a default padding to 8dp
return 8 * getResources().getDisplayMetrics().density;
}
private int randomColor() {
Random random = new Random();
String[] colorsArr = getResources().getStringArray(R.array.colors);
return Color.parseColor(colorsArr[random.nextInt(colorsArr.length)]);
}
}