下面列出了android.text.TextPaint#setStyle ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void initPaint() {
mTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(getTextSize());
mTextPaint.setColor(mColor);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setTypeface(getTypeface());
mTextPaintOutline = new TextPaint();
mTextPaintOutline.setAntiAlias(true);
mTextPaintOutline.setTextSize(getTextSize());
mTextPaintOutline.setColor(mBorderColor);
mTextPaintOutline.setStyle(Paint.Style.STROKE);
mTextPaintOutline.setTypeface(getTypeface());
mTextPaintOutline.setStrokeWidth(mBorderSize);
}
private void initPaint() {
mTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(getTextSize());
mTextPaint.setColor(mColor);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setTypeface(getTypeface());
mTextPaintOutline = new TextPaint();
mTextPaintOutline.setAntiAlias(true);
mTextPaintOutline.setTextSize(getTextSize());
mTextPaintOutline.setColor(mBorderColor);
mTextPaintOutline.setStyle(Paint.Style.STROKE);
mTextPaintOutline.setTypeface(getTypeface());
mTextPaintOutline.setStrokeWidth(mBorderSize);
}
private IconDrawable(Context context, @NonNull IconState state) {
iconState = state;
paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
// We have already confirmed that a typeface exists for this icon during
// validation, so we can ignore the null pointer warning.
//noinspection ConstantConditions
paint.setTypeface(Iconify.findTypefaceOf(state.icon).getTypeface(context));
paint.setStyle(state.style);
paint.setTextAlign(Paint.Align.CENTER);
paint.setUnderlineText(false);
color = state.colorStateList.getColorForState(StateSet.WILD_CARD, DEFAULT_COLOR);
paint.setColor(color);
updateTintFilter();
setModulatedAlpha();
paint.setDither(iconState.dither);
text = String.valueOf(iconState.icon.character());
if (SDK_INT < LOLLIPOP && iconState.bounds != null) {
setBounds(iconState.bounds);
}
}
private void initPaint() {
mTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(getTextSize());
mTextPaint.setColor(mColor);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setTypeface(getTypeface());
mTextPaintOutline = new TextPaint();
mTextPaintOutline.setAntiAlias(true);
mTextPaintOutline.setTextSize(getTextSize());
mTextPaintOutline.setColor(mBorderColor);
mTextPaintOutline.setStyle(Paint.Style.STROKE);
mTextPaintOutline.setTypeface(getTypeface());
mTextPaintOutline.setStrokeWidth(mBorderSize);
}
/**
* 初始化画笔
*/
private void initDefaultPainters() {
mCanvas = new Canvas();
mPainter = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mPainter.setColor(0xFFFFFFFF);
mPainter.setTextAlign(Paint.Align.LEFT);
mPainter.setTextSize(DimensUtils.sp2pixel(mContext, mTextSize));
mStrokePainter = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mStrokePainter.setColor(0xFF000000);
mStrokePainter.setTextAlign(Paint.Align.LEFT);
mStrokePainter.setStyle(Paint.Style.STROKE);
mStrokePainter.setStrokeWidth(3.0f);
mStrokePainter.setShadowLayer(3, 0, 0, 0xFF000000);
mStrokePainter.setTextSize(DimensUtils.sp2pixel(mContext, mTextSize));
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
ds.setStyle(Paint.Style.FILL_AND_STROKE);
PathEffect effects = new DashPathEffect(new float[]{1, 1}, 1);
ds.setPathEffect(effects);
ds.setStrokeWidth(2);
}
private void preparePaint() {
paint = new Paint(TextPaint.ANTI_ALIAS_FLAG);
textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setStyle(Paint.Style.FILL);
paint.setStyle(Paint.Style.FILL);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(Color.argb(255, 0, 0, 0));
textPaint.density = getResources().getDisplayMetrics().density;
textPaint.setTextSize(keyTextSize);
textPaint.setTextAlign(Paint.Align.CENTER);
}
public PrimitiveDrawer(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
paint = new TextPaint();
paint.setStyle(Paint.Style.FILL);
paint.setAntiAlias(true);
paint.setColor(0xffffffff);
paint.setStrokeWidth(1.f);
paint.setTextSize(35.f);
paint.setTextAlign(Paint.Align.LEFT);
}
@Override
protected void onDraw(Canvas canvas) {
// 只有描边
TextPaint paint = getPaint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(mStrokeWidth);
super.onDraw(canvas);
}
@Override
public void updateDrawState(TextPaint paint) {
final float newStrokeWidth = (mWeight / (UDFontWeight.WEIGHT_NORMAL_INT + 0.0f));
if (paint.getStyle() == Paint.Style.FILL) {
paint.setStyle(Paint.Style.FILL_AND_STROKE);
}
paint.setStrokeWidth(newStrokeWidth);
}
/**
* Create an IconDrawable.
*
* @param font The font to use for this drawable
* @param context Your activity or application context.
* @param icon The icon you want this drawable to display.
*/
public IconicsDrawableOld(Context context, ITypeface font, IIcon icon) {
this.context = context;
this.icon = icon;
paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
paint.setTypeface(font.getTypeface(context));
paint.setStyle(Paint.Style.STROKE);
paint.setTextAlign(Paint.Align.CENTER);
paint.setUnderlineText(false);
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public void updateDrawState(TextPaint paint) {
paint.setStyle(Paint.Style.FILL);
Shader shader = new LinearGradient(0, 0, 0, paint.getTextSize()
* colors.length, colors, null, Shader.TileMode.MIRROR);
Matrix matrix = new Matrix();
matrix.setRotate(90);
shader.setLocalMatrix(matrix);
paint.setShader(shader);
}
@Override
public void updateDrawState(TextPaint paint) {
paint.setStyle(Paint.Style.FILL);
Shader shader = new LinearGradient(0, 0, 0, paint.getTextSize()
* colors.length, colors, null, Shader.TileMode.MIRROR);
Matrix matrix = new Matrix();
matrix.setRotate(90);
shader.setLocalMatrix(matrix);
paint.setShader(shader);
}
public SubtitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mTextBounds = new Rect();
mBottomSubtitles = new SubtitleText[0];
mTopSubtitles = new SubtitleText[0];
mSpecialSubtitles = new SubtitleText[0];
mTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setTextAlign(Paint.Align.CENTER);
mStokePaint = new TextPaint();
mStokePaint.setAntiAlias(true);
mStokePaint.setStyle(Paint.Style.STROKE);
mStokePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mStokePaint.setTextAlign(Paint.Align.CENTER);
setBackgroundColor(Color.TRANSPARENT);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
resetTextPaint();
resetStokePaint();
}
public static Bitmap drawText(String text, Bitmap b, int textColor, int bgColor) {
// Get text dimensions
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
textPaint.setStyle(Paint.Style.FILL);
textPaint.setColor(textColor);
textPaint.setTextSize(b.getHeight() / 16f);
StaticLayout mTextLayout = new StaticLayout(text, textPaint, b.getWidth() / 2, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
// Create bitmap and canvas to draw to
Canvas c = new Canvas(b);
if(bgColor != 0){
// Draw background
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
paint.setStyle(Paint.Style.FILL);
paint.setColor(bgColor);
c.drawPaint(paint);
}
// Draw text
c.save();
c.translate(0, 0);
mTextLayout.draw(c);
c.restore();
return b;
}
/**
* Create an IconDrawable.
*
* @param context Your activity or application context.
* @param icon The icon you want this drawable to display.
*/
public FontIconDrawable(Context context, String icon, Typeface typeface) {
this.context = context;
this.icon = icon;
paint = new TextPaint();
paint.setTypeface(typeface);
paint.setStyle(Paint.Style.STROKE);
paint.setTextAlign(Paint.Align.CENTER);
paint.setUnderlineText(false);
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
}
@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setUnderlineText(true);
//间隔线
ds.setStyle(Paint.Style.STROKE);
PathEffect effects = new DashPathEffect(new float[]{1, 1}, 1);
ds.setPathEffect(effects);
ds.setStrokeWidth(5);
}
/**
* 生成水印文字图片
*/
public BitmapDrawable drawTextToBitmap(Context mContext, String gText) {
try {
TextPaint mTextPaint1 = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint1.density = mContext.getResources().getDisplayMetrics().density;
oneTextPx = Utils.sp2px(mContext, textSize);
mTextPaint1.setTextSize(oneTextPx);
//计算字长
textLength = (int) mTextPaint1.measureText(gText);
int stextLength = (int) mTextPaint1.measureText(sText);
/**拿到字长之后,计算一下斜着显示文字的时候文字所占的长和高*/
int witchPx = 0;
int highPx = 0;
/**默认一段文字的长和高计算*/
if (sWitchPx == 0) {
sWitchPx = measurementWitch(stextLength);
sHigthPx = measurementHigth(stextLength);
}
/**传入的文字的长和高计算*/
witchPx = measurementWitch(textLength);
highPx = measurementHigth(textLength);
/**计算显示完整所需占的画图长宽*/
int bitmapWitch = witchPx + sWitchPx + 2 * oneTextPx + offset;
int bitmaphigth = (highPx + oneTextPx) * 2;
//设置画板的时候 增加一个字的长宽
bitmap = Bitmap.createBitmap(bitmapWitch + right,
bitmaphigth+(2*top), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(ContextCompat.getColor(mContext, R.color.atom_ui_chat_gray_bg));
/**初始化画笔*/
TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.density = mContext.getResources().getDisplayMetrics().density;
mTextPaint.setColor(Color.parseColor("#C4C4C4"));
mTextPaint.setAlpha(90);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setAntiAlias(true);
mTextPaint.setTextAlign(Paint.Align.LEFT);
mTextPaint.setFakeBoldText(false);
mTextPaint.setTextSkewX(0);
mTextPaint.setTextSize(oneTextPx);
/**
* ——————————————————————————————————>
* | 1号绘制区域 | 间 | 2号 |
* | gText | | appName |
* | ①号起点位置 | 隔 | ②起 |
* ———————————————————————————————————
* | 3号 | 间 | 4号绘制区域 |
* | appName | | gText |
* | ③起 | 隔 | ④号起 |
* |———————————————————————————————————
* V
*/
/**方式二利用画布平移和旋转绘制*/
/**先移动到①起点位置*/
canvas.translate(oneTextPx,highPx+oneTextPx+top);
/**旋转一定度数 绘制文字*/
canvas.rotate(-rotate);
canvas.drawText(gText,0,0,mTextPaint);
/**恢复原来的度数 再移动画布原点到②号位置*/
canvas.rotate(rotate);
canvas.translate(witchPx+offset+oneTextPx,0);
/**旋转一定度数 绘制文字*/
canvas.rotate(-rotate);
canvas.drawText(sText,0,0,mTextPaint);
/**恢复原来的度数 再移动画布原点到③号位置*/
canvas.rotate(rotate);
canvas.translate(-(witchPx+offset+oneTextPx),oneTextPx+highPx+top);
/**旋转一定度数 绘制文字*/
canvas.rotate(-rotate);
canvas.drawText(sText,0,0,mTextPaint);
/**恢复原来的度数 再移动画布原点到④号位置*/
canvas.rotate(rotate);
canvas.translate(sWitchPx+offset+oneTextPx,0);
/**旋转一定度数 绘制文字*/
canvas.rotate(-rotate);
canvas.drawText(gText,0,0,mTextPaint);
/**保存画布*/
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
//生成平铺的bitmapDrawable
BitmapDrawable drawable = new BitmapDrawable(mContext.getResources(), bitmap);
drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
drawable.setDither(true);
return drawable;
} catch (Exception e) {
}
return null;
}
private void init(Context context, AttributeSet attrs) {
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.SegmentedBarView,
0, 0);
try {
Resources resources = getResources();
segmentTextSize = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_segment_text_size,
resources.getDimensionPixelSize(R.dimen.sbv_segment_text_size));
valueTextSize = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_value_text_size,
resources.getDimensionPixelSize(R.dimen.sbv_value_text_size));
descriptionTextSize = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_description_text_size,
resources.getDimensionPixelSize(R.dimen.sbv_description_text_size));
barHeight = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_bar_height,
resources.getDimensionPixelSize(R.dimen.sbv_bar_height));
valueSignHeight = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_value_sign_height,
resources.getDimensionPixelSize(R.dimen.sbv_value_sign_height));
valueSignWidth = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_value_sign_width,
resources.getDimensionPixelSize(R.dimen.sbv_value_sign_width));
arrowHeight = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_arrow_height,
resources.getDimensionPixelSize(R.dimen.sbv_arrow_height));
arrowWidth = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_arrow_width,
resources.getDimensionPixelSize(R.dimen.sbv_arrow_width));
gapWidth = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_segment_gap_width,
resources.getDimensionPixelSize(R.dimen.sbv_segment_gap_width));
valueSignRound = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_value_sign_round,
resources.getDimensionPixelSize(R.dimen.sbv_value_sign_round));
descriptionBoxHeight = a.getDimensionPixelSize(R.styleable.SegmentedBarView_sbv_description_box_height,
resources.getDimensionPixelSize(R.dimen.sbv_description_box_height));
showSegmentText = a.getBoolean(R.styleable.SegmentedBarView_sbv_show_segment_text, true);
showDescriptionText = a.getBoolean(R.styleable.SegmentedBarView_sbv_show_description_text, false);
valueSegmentText = a.getString(R.styleable.SegmentedBarView_sbv_value_segment_text);
if (valueSegmentText == null) {
valueSegmentText = resources.getString(R.string.sbv_value_segment);
}
emptySegmentText = a.getString(R.styleable.SegmentedBarView_sbv_empty_segment_text);
if (emptySegmentText == null) {
emptySegmentText = resources.getString(R.string.sbv_empty);
}
valueSignColor = a.getColor(R.styleable.SegmentedBarView_sbv_value_sign_background,
ContextCompat.getColor(context, R.color.sbv_value_sign_background));
emptySegmentColor = a.getColor(R.styleable.SegmentedBarView_sbv_empty_segment_background,
ContextCompat.getColor(context, R.color.sbv_empty_segment_background));
sideStyle = a.getInt(R.styleable.SegmentedBarView_sbv_side_style,
SegmentedBarViewSideStyle.ROUNDED);
sideTextStyle = a.getInt(R.styleable.SegmentedBarView_sbv_side_text_style,
SegmentedBarViewSideTextStyle.ONE_SIDED);
} finally {
a.recycle();
}
formatter = new DecimalFormat("##.####");
segmentTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
segmentTextPaint.setColor(Color.WHITE);
segmentTextPaint.setStyle(Paint.Style.FILL);
valueTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
valueTextPaint.setColor(Color.WHITE);
valueTextPaint.setStyle(Paint.Style.FILL);
valueTextPaint.setTextSize(valueTextSize);
valueTextPaint.setColor(valueTextColor);
descriptionTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
descriptionTextPaint.setColor(Color.DKGRAY);
descriptionTextPaint.setStyle(Paint.Style.FILL);
fillPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
fillPaint.setStyle(Paint.Style.FILL);
rectBounds = new Rect();
roundRectangleBounds = new RectF();
valueSignBounds = new Rect();
segmentRect = new Rect();
trianglePath = new Path();
trianglePath.setFillType(Path.FillType.EVEN_ODD);
point1 = new Point();
point2 = new Point();
point3 = new Point();
}
/**
* build a bitmap from a text.
*
* @return {@link Bitmap} the bitmap return.
*/
public static Bitmap textAsBitmap(Context context, WatermarkText watermarkText) {
TextPaint watermarkPaint = new TextPaint();
watermarkPaint.setColor(watermarkText.getTextColor());
watermarkPaint.setStyle(watermarkText.getTextStyle());
if (watermarkText.getTextAlpha() >= 0 && watermarkText.getTextAlpha() <= 255) {
watermarkPaint.setAlpha(watermarkText.getTextAlpha());
}
float value = (float) watermarkText.getTextSize();
int pixel = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
value, context.getResources().getDisplayMetrics());
watermarkPaint.setTextSize(pixel);
if (watermarkText.getTextShadowBlurRadius() != 0
|| watermarkText.getTextShadowXOffset() != 0
|| watermarkText.getTextShadowYOffset() != 0) {
watermarkPaint.setShadowLayer(watermarkText.getTextShadowBlurRadius(),
watermarkText.getTextShadowXOffset(),
watermarkText.getTextShadowYOffset(),
watermarkText.getTextShadowColor());
}
if (watermarkText.getTextFont() != 0) {
Typeface typeface = ResourcesCompat.getFont(context, watermarkText.getTextFont());
watermarkPaint.setTypeface(typeface);
}
watermarkPaint.setAntiAlias(true);
watermarkPaint.setTextAlign(Paint.Align.LEFT);
watermarkPaint.setStrokeWidth(5);
float baseline = (int) (-watermarkPaint.ascent() + 1f);
Rect bounds = new Rect();
watermarkPaint.getTextBounds(watermarkText.getText(),
0, watermarkText.getText().length(), bounds);
int boundWidth = bounds.width() + 20;
int mTextMaxWidth = (int) watermarkPaint.measureText(watermarkText.getText());
if (boundWidth > mTextMaxWidth) {
boundWidth = mTextMaxWidth;
}
StaticLayout staticLayout = new StaticLayout(watermarkText.getText(),
0, watermarkText.getText().length(),
watermarkPaint, mTextMaxWidth, android.text.Layout.Alignment.ALIGN_NORMAL, 2.0f,
2.0f, false);
int lineCount = staticLayout.getLineCount();
int height = (int) (baseline + watermarkPaint.descent() + 3) * lineCount;
Bitmap image = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
if (boundWidth > 0 && height > 0) {
image = Bitmap.createBitmap(boundWidth, height, Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(image);
canvas.drawColor(watermarkText.getBackgroundColor());
staticLayout.draw(canvas);
return image;
}