import android.R;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ImageView;
/**
* @author yangshuai
* @version 建立時間:2015-8-25 下午5:58:48
* 類說明 :有文字的imageView
*/
public class DrawTextImageView extends ImageView {
public DrawTextImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public DrawTextImageView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawTextImageView (Context context) {
super(context);
}
/**
* 文字
*/
private String textString = "";
/**
* 文字大小
*/
private float textSize = ;
/**
* 文字位置x
*/
private float x = -;
/**
* 文字位置y
*/
private float y = -;
/**
* 文字顔色
*/
private int textColor = R.color.holo_red_light;
/**
* 文字線條粗細
*/
private int textDrawStrokeWidth = ;
/**
*
*/
public void setDrawText(String string){
textString = string;
drawableStateChanged();
}
/**
* 設定文字
*/
public void setDrawTextSize(float textSize){
this.textSize = textSize;
drawableStateChanged();
}
/**
* 設定文字位置x,y
*/
public void setDrawLocalXY(float x, float y){
this.x = x;
this.y = y;
drawableStateChanged();
}
/**
* 設定文字顔色
*/
public void setDrawTextColorResourse(int textColor){
this.textColor = textColor;
drawableStateChanged();
}
/**
* 設定文字粗細
*/
public void setDrawTextStrokeWidth(int textDrawStrokeWidth){
this.textDrawStrokeWidth = textDrawStrokeWidth;
drawableStateChanged();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!textString.equals("")) {
Paint paint = new Paint();
paint.setColor(getResources().getColor(textColor));
paint.setStrokeWidth(textDrawStrokeWidth);
paint.setStyle(Paint.Style.FILL);
paint.setTextSize(textSize);
canvas.drawText(textString, x==- ? ( canvas.getWidth() - ( textSize * textString.length() ) ) : x, y == - ? ( canvas.getHeight() - ) : y, paint);
}
}
}