- @date 2019-08-06.
- module:图文混排TextView,请使用{@link #setMText(CharSequence)}
- description:
*/
public class MTextView extends android.support.v7.widget.AppCompatTextView {
/**
* 缓存测量过的数据
/
private static HashMap<String, SoftReference> measuredData =
new HashMap<String, SoftReference>();
private static int hashIndex = 0;
/*
* 存储当前文本内容,每个item为一行
/
ArrayList
private Context context;
/
* 用于测量字符宽度
/
private TextPaint paint = new TextPaint();
/*
* 用于测量span高度
/
private Paint.FontMetricsInt mSpanFmInt = new Paint.FontMetricsInt();
/*
* 临时使用,以免在onDraw中反复生产新对象
*/
private FontMetrics mFontMetrics = new FontMetrics();
// private float lineSpacingMult = 0.5f;
private int textColor = Color.BLACK;
// 行距
private float lineSpacing;
private int lineSpacingDP = 5;
/**
* 段间距,-1为默认
*/
private int paragraphSpacing = -1;
/**
* 最大宽度
*/
private int maxWidth;
/**
* 只有一行时的宽度
*/
private int oneLineWidth = -1;
/**
* 已绘的行中最宽的一行的宽度
*/
private float lineWidthMax = -1;
/**
* 存储当前文本内容,每个item为一个字符或者一个SpanObject
*/
private ArrayList<Object> obList = new ArrayList<Object>();
/**
* 是否使用默认{@link #onMeasure(int, int)}和
* {@link #onDraw(android.graphics.Canvas)}
*/
private boolean useDefault = false;
protected CharSequence text = "";
private int minHeight;
/**
* 用以获取屏幕高宽
*/
private DisplayMetrics displayMetrics;
/**
* {@link android.text.style.BackgroundColorSpan}用
*/
private Paint textBgColorPaint = new Paint();
/**
* {@link android.text.style.BackgroundColorSpan}用
*/
private Rect textBgColorRect = new Rect();
public MTextView(Context context) {
super(context);
init(context);
}
public MTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public void init(Context context) {
this.context = context;
paint.setAntiAlias(true);
lineSpacing = dip2px(context, lineSpacingDP);
minHeight = dip2px(context, 30);
displayMetrics = new DisplayMetrics();
}
public static int px2sp(Context context, float pxValue) {
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
@Override
public void setMaxWidth(int maxpixels) {
super.setMaxWidth(maxpixels);
maxWidth = maxpixels;
}
@Override
public void setMinHeight(int minHeight) {
super.setMinHeight(minHeight);
this.minHeight = minHeight;
}