下面列出了android.text.TextPaint#setAlpha ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public SlidingLocaleDrawable(Drawable background, int width, int height) {
mBackground = background;
setDefaultBounds(mBackground);
mWidth = width;
mHeight = height;
mTextPaint = new TextPaint();
mTextPaint.setTextSize(getTextSizeFromTheme(android.R.style.TextAppearance_Medium, 18));
mTextPaint.setColor(mRes.getColor(R.color.latinkeyboard_transparent));
mTextPaint.setTextAlign(Align.CENTER);
mTextPaint.setAlpha(OPACITY_FULLY_OPAQUE);
mTextPaint.setAntiAlias(true);
mMiddleX = (mWidth - mBackground.getIntrinsicWidth()) / 2;
mLeftDrawable =
mRes.getDrawable(R.drawable.sym_keyboard_feedback_language_arrows_left);
mRightDrawable =
mRes.getDrawable(R.drawable.sym_keyboard_feedback_language_arrows_right);
mThreshold = ViewConfiguration.get(mContext).getScaledTouchSlop();
}
public void setProperties(){
paint = new TextPaint();
if (theme < 0) {
getThemeIconColor();
}
try {
paint.setTypeface(MaterialIcon.getTypeface(context));
} catch (Exception e) {
e.printStackTrace();
}
paint.setStyle(Paint.Style.STROKE);
paint.setTextAlign(Paint.Align.CENTER);
paint.setUnderlineText(false);
paint.setColor(color);
paint.setAlpha(alpha);
paint.setAntiAlias(true);
}
@Override
public void updateDrawState(@NonNull TextPaint paint)
{
paint.setAlpha((int) (paint.getAlpha() * mValue));
paint.bgColor = Color.argb((int) (Color.alpha(paint.bgColor) * mValue),
Color.red(paint.bgColor), Color.green(paint.bgColor), Color.blue(paint.bgColor));
}
protected void drawText(BaseDanmaku danmaku, String lineText, Canvas canvas, float left, float top, TextPaint paint, boolean fromWorkerThread) {
if (fromWorkerThread && danmaku instanceof SpecialDanmaku) {
paint.setAlpha(255);
}
if (lineText != null) {
canvas.drawText(lineText, left, top, paint);
} else {
canvas.drawText(danmaku.text.toString(), left, top, paint);
}
}
@Override
public void updateDrawState(@NonNull TextPaint paint)
{
paint.setAlpha((int) (paint.getAlpha() * mValue));
paint.bgColor = Color.argb((int) (Color.alpha(paint.bgColor) * mValue),
Color.red(paint.bgColor), Color.green(paint.bgColor), Color.blue(paint.bgColor));
}
@Test
public void testAlphaSpan()
{
final AlphaSpan alphaSpan = new AlphaSpan(0.5f);
final TextPaint textPaint = new TextPaint();
textPaint.setAlpha(200);
textPaint.bgColor = Color.argb(100, 100, 100, 100);
alphaSpan.updateDrawState(textPaint);
assertEquals(100, textPaint.getAlpha());
assertEquals(Color.argb(50, 100, 100, 100), textPaint.bgColor);
}
/**
* Progress through the morph: 0 = character, 1 = •
*/
void draw(Canvas canvas, TextPaint paint, CharSequence password,
int index, float charWidth, float progress) {
int alpha = paint.getAlpha();
float x = charWidth * index;
// draw the character
canvas.save();
float textScale = lerp(1f, textToMaskScale, progress);
// scale character: shrinks to/grows from the mask's height
canvas.scale(textScale, textScale, x + bounds.exactCenterX(), bounds.exactCenterY());
// cross fade between the character/mask
paint.setAlpha((int) lerp(alpha, 0, progress));
// vertically move the character center toward/from the mask center
canvas.drawText(password, index, index + 1,
x, lerp(0f, textOffsetY, progress) / textScale, paint);
canvas.restore();
// draw the mask
canvas.save();
float maskScale = lerp(maskToTextScale, 1f, progress);
// scale the mask: down from/up to the character width
canvas.scale(maskScale, maskScale, x + bounds.exactCenterX(), bounds.exactCenterY());
// cross fade between the mask/character
paint.setAlpha((int) AnimUtils.lerp(0, alpha, progress));
// vertically move the mask center from/toward the character center
canvas.drawText(PASSWORD_MASK, 0, 1, x, -lerp(textOffsetY, 0f, progress), paint);
canvas.restore();
// restore the paint to how we found it
paint.setAlpha(alpha);
}
@Override
public void updateDrawState(TextPaint tp){
tp.setAlpha(alpha);
}
/**
* 生成水印文字图片
*/
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 drawHoursValues(Canvas canvas) {
if (!showHoursValues)
return;
Rect rect = new Rect();
TextPaint textPaint = new TextPaint();
textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
textPaint.setAntiAlias(true);
textPaint.setColor(this.valuesColor);
textPaint.setTypeface(this.valuesFont);
textPaint.setTextSize(size * DEFAULT_HOURS_VALUES_TEXT_SIZE);
float degreeSpace = 0f;
if (showDegrees)
degreeSpace = DEFAULT_DEGREE_STROKE_WIDTH + 0.06f;
int rText = (int) (centerX - (size * DEFAULT_HOURS_VALUES_TEXT_SIZE) - (size * degreeSpace));
for (int i = FULL_ANGLE; i > 0; i = i - valueStep.getId()) {
int value = i / 30;
String formatted;
switch (valueType) {
case roman:
formatted = ClockUtils.toRoman(value);
break;
case arabic:
formatted = ClockUtils.toArabic(value);
break;
default:
formatted = String.format(Locale.getDefault(), "%02d", value);
break;
}
if (valueDisposition.getId() == 0) {
if ((i % REGULAR_ANGLE) == 0) {
textPaint.setTextSize(size * DEFAULT_HOURS_VALUES_TEXT_SIZE);
textPaint.setAlpha(FULL_ALPHA);
} else {
textPaint.setTextSize(size * (DEFAULT_HOURS_VALUES_TEXT_SIZE - 0.03f));
textPaint.setAlpha(CUSTOM_ALPHA);
}
} else {
textPaint.setTextSize(size * DEFAULT_HOURS_VALUES_TEXT_SIZE);
textPaint.setAlpha(FULL_ALPHA);
}
int textX = (int) (centerX + rText * Math.cos(Math.toRadians(REGULAR_ANGLE - i)));
int textY = (int) (centerX - rText * Math.sin(Math.toRadians(REGULAR_ANGLE - i)));
textPaint.getTextBounds(formatted, 0, formatted.length(), rect);
canvas.drawText(formatted, textX - rect.width() / formatted.length(), textY + rect.height() / formatted.length(), textPaint);
}
}
@Override
public void updateDrawState(TextPaint tp){
tp.setAlpha(alpha);
}
/**
* 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;
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setAlpha(alpha);
}
@Override
public void updateDrawState(TextPaint ds) {
ds.setAlpha(mAlpha);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setAlpha(alpha);
}