package com.sogouchat.widget;
import java.lang.ref.WeakReference;
import com.sogouchat.bean.TelNode;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.style.ImageSpan;
public class ContactSpan extends ImageSpan {
private Paint mPaint;
private TelNode mTel;
public ContactSpan(TelNode tel, Drawable d, int alignBaseline) {
// TODO Auto-generated constructor stub
super(d, alignBaseline);
mTel = tel;
mPaint=new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(18);
}
@Override
public void draw(Canvas canvas, CharSequence text,
int start, int end, float x,
int top, int y, int bottom, Paint paint) {
Drawable b = getCachedDrawable();
canvas.save();
int transY = bottom - b.getBounds().bottom;
if (mVerticalAlignment == ALIGN_BASELINE) {
transY -= paint.getFontMetricsInt().descent;
}
canvas.translate(x, transY);
b.draw(canvas);
float w = getStrWidth();
float posX = (b.getBounds().width() - w)/2;
float posY = (b.getBounds().height() - mPaint.getFontMetricsInt().top)/2;
canvas.drawText(mTel.mName,posX, posY,mPaint);
canvas.restore();
}
private Drawable getCachedDrawable() {
WeakReference<Drawable> wr = mDrawableRef;
Drawable d = null;
if (wr != null)
d = wr.get();
if (d == null) {
d = getDrawable();
mDrawableRef = new WeakReference<Drawable>(d);
}
return d;
}
private WeakReference<Drawable> mDrawableRef;
private float getStrWidth(){
float[] widths = new float[mTel.mName.length()];
mPaint.getTextWidths(mTel.mName, widths);
float w=0;
for(int i=0;i < mTel.mName.length();i++){
w +=widths[i];
}
return w;
}
}