下面列出了android.text.TextPaint#setTypeface ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void init() {
selectBar = new SelectBar(this);
editBar = new EditBar(this);
setBackgroundColor(Color.WHITE);
code = new StringBuilder();
undoStack = new UndoStack();
scroller = new Scroller(getContext());
inputEvent = new InputEvent();
selectModel = new SelectModel();
codeInputConnection = new CodeInputConnection(this, false);
setFocusable(true);
setFocusableInTouchMode(true);
setLongClickable(true);
paint = new Paint();
textPaint = new TextPaint();
paint.setAntiAlias(true);
textPaint.setAntiAlias(true);
textPaint.setTypeface(Typeface.MONOSPACE);
setTextSize(textSize);
gestureDetector = new GestureDetector(getContext(), new GestureListener());
}
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 initPaintObjects()
{
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextSize(hintTextSize);
if (typeface != null)
{
textPaint.setTypeface(typeface);
}
textPaint.setColor(baseColor);
baseAlpha = textPaint.getAlpha();
selectorPath = new Path();
selectorPath.setFillType(Path.FillType.EVEN_ODD);
selectorPoints = new Point[3];
for (int i = 0; i < 3; i++)
{
selectorPoints[i] = new Point();
}
}
private void init() {
mTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
if (mTextSizePx <= 0) {
mTextSizePx = getDefaultTextSizeInPixels();
}
mTextPaint.setTextSize(mTextSizePx);
mTextPaint.setColor(mTextColor);
mTypeface = MongolFont.get(MongolFont.QAGAN, mContext);
mTextPaint.setTypeface(mTypeface);
mRenderer = MongolCode.INSTANCE;
if (mUnicodeText == null) {
mUnicodeText = "";
}
mGlyphText = mRenderer.unicodeToMenksoft(mUnicodeText);
}
@Override
public void updateDrawState(TextPaint tp) {
if (typeface != null) {
tp.setTypeface(typeface);
}
if (textSize != 0) {
tp.setTextSize(textSize);
}
if (color != 0) {
tp.setColor(color);
}
tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
public static Bitmap getAlphabetImage(Context context, String content) {
Resources res = context.getResources();
Bitmap mDefaultBitmap = BitmapFactory.decodeResource(res, android.R.drawable.sym_def_app_icon);
int width = mDefaultBitmap.getWidth();
int height = mDefaultBitmap.getHeight();
TextPaint mPaint = new TextPaint();
mPaint.setTypeface(OControlHelper.boldFont());
mPaint.setColor(Color.WHITE);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setAntiAlias(true);
int textSize = res.getDimensionPixelSize(R.dimen.text_size_xxlarge);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas();
Rect mBounds = new Rect();
canvas.setBitmap(bitmap);
canvas.drawColor(OStringColorUtil.getStringColor(context, content));
if (content == null || content.trim().length() == 0) {
content = "?";
}
char[] alphabet = {Character.toUpperCase(content.trim().charAt(0))};
mPaint.setTextSize(textSize);
mPaint.getTextBounds(alphabet, 0, 1, mBounds);
canvas.drawText(alphabet, 0, 1, 0 + width / 2,
0 + height / 2 + (mBounds.bottom - mBounds.top) / 2, mPaint);
return bitmap;
}
private TabSwitcherDrawable(Resources resources, boolean useLight, Bitmap bitmap) {
super(resources, bitmap);
setTint(ApiCompatibilityUtils.getColorStateList(resources,
useLight ? R.color.light_mode_tint : R.color.dark_mode_tint));
mSingleDigitTextSize =
resources.getDimension(R.dimen.toolbar_tab_count_text_size_1_digit);
mDoubleDigitTextSize =
resources.getDimension(R.dimen.toolbar_tab_count_text_size_2_digit);
mTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextAlign(Align.CENTER);
mTextPaint.setTypeface(Typeface.create("sans-serif-condensed", Typeface.BOLD));
mTextPaint.setColor(getColorForState());
}
private void init(AttributeSet attrs, int defStyle) {
final TypedArray a = getContext().obtainStyledAttributes(
attrs, R.styleable.RoundedLetterView, defStyle, 0);
if(a.hasValue(R.styleable.RoundedLetterView_rlv_titleText)){
mTitleText = a.getString(R.styleable.RoundedLetterView_rlv_titleText);
}
mTitleColor = a.getColor(R.styleable.RoundedLetterView_rlv_titleColor,DEFAULT_TITLE_COLOR);
mBackgroundColor = a.getColor(R.styleable.RoundedLetterView_rlv_backgroundColorValue,DEFAULT_BACKGROUND_COLOR);
mTitleSize = a.getDimension(R.styleable.RoundedLetterView_rlv_titleSize,DEFAULT_TITLE_SIZE);
a.recycle();
//Title TextPaint
mTitleTextPaint = new TextPaint();
mTitleTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mTitleTextPaint.setTypeface(mFont);
mTitleTextPaint.setTextAlign(Paint.Align.CENTER);
mTitleTextPaint.setLinearText(true);
mTitleTextPaint.setColor(mTitleColor);
mTitleTextPaint.setTextSize(mTitleSize);
//Background Paint
mBackgroundPaint = new Paint();
mBackgroundPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mBackgroundPaint.setStyle(Paint.Style.FILL);
mBackgroundPaint.setColor(mBackgroundColor);
mInnerRectF = new RectF();
}
private void drawMinutesValues(Canvas canvas) {
if (!showMinutesValues)
return;
Rect rect = new Rect();
Typeface typeface = ResourcesCompat.getFont(getContext(), R.font.proxima_nova_thin);
TextPaint textPaint = new TextPaint();
textPaint.setColor(minutesProgressColor);
textPaint.setTypeface(typeface);
textPaint.setTextSize(size * MINUTES_TEXT_SIZE);
int rText = (int) (centerX - ((1 - minutesValuesFactor - (2 * DEFAULT_BORDER_THICKNESS) - MINUTES_TEXT_SIZE) * radius));
for (int i = 0; i < FULL_ANGLE; i = i + QUARTER_DEGREE_STEPS) {
int value = i / 6;
String formatted;
switch (valueType) {
case arabic:
formatted = ClockUtils.toArabic(value);
break;
case roman:
formatted = ClockUtils.toRoman(value);
break;
default:
formatted = String.format(Locale.getDefault(), "%02d", value);
break;
}
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);
}
}
public static float getFontCapHeight (Typeface font, float fontSize)
{
TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTypeface(font);
textPaint.setTextSize(fontSize);
Rect tempRect = new Rect();
textPaint.getTextBounds("H", 0, 1, tempRect);
return tempRect.height();
}
public PieChartView(Context context) {
super(context);
for (int i = 1; i <= 100; i++) {
lookupTable[i] = i + "%";
}
textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setColor(Color.WHITE);
textPaint.setTypeface(Typeface.create("sans-serif-medium", Typeface.NORMAL));
canCaptureChartSelection = true;
}
private void init() {
mRingPaint = new Paint();
mRingPaint.setAntiAlias(true);
mRingPaint.setColor(Color.WHITE);
mRingPaint.setStyle(Paint.Style.STROKE);
mFocusPaint = new Paint();
mFocusPaint.setAntiAlias(true);
mFocusPaint.setStyle(Paint.Style.STROKE);
mFocusPaint.setStrokeWidth(3f);
mFocusPaint.setColor(Color.BLACK);
mBitmapPaint = new Paint();
mBitmapPaint.setFilterBitmap(false);
mGridPaint = new Paint();
//设置线宽。单位为1像素
mGridPaint.setStrokeWidth(1f);
mGridPaint.setStyle(Paint.Style.STROKE);
//画笔颜色
mGridPaint.setColor(-3355444);
mGridShadowPaint = new Paint(mGridPaint);
mGridShadowPaint.setColor(-12303292);
mTextPaint = new TextPaint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextAlign(Paint.Align.CENTER);
mTextPaint.setTypeface(Typeface.MONOSPACE);
mTextPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.dk_font_size_12));
}
private HtmlObject getStyledObject(StyleSpan span, String content, int start, int end, float thisXOffset) {
TextPaint paint = getPaintFromHeap();
paint.setTypeface(Typeface.defaultFromStyle(span.getStyle()));
paint.setTextSize(mTextsize);
paint.setColor(mColor);
span.updateDrawState(paint);
span.updateMeasureState(paint);
HtmlObject obj = new HtmlObject(content, start, end, thisXOffset, paint);
obj.recycle = true;
return obj;
}
/**
* Share Button Click.
* get hadith text and create bitmap with hadith text and share it.
*/
private void onShareButtonClicked(String subject, String body) {
// check if app grant write external storage permission.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// check permission for marshmellow.
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Camera permission has not been granted.
// Camera permission has not been granted yet. Request it directly.
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return;
}
}
// create image from hadith and try share it
Resources resources = getResources();
// Create background bitmap
Bitmap backgroundBitmap = BitmapFactory.decodeResource(resources, R.drawable.backgroundtile);
Bitmap.Config config = backgroundBitmap.getConfig();
if (config == null) {
config = Bitmap.Config.ARGB_8888;
}
int width = 600 + (body.length() / 512) * 50;//backgroundBitmap.getWidth();
// Create logo bitmap
Bitmap logoBitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher);
logoBitmap = Bitmap.createScaledBitmap(logoBitmap, 128, 128, false);
logoBitmap = logoBitmap.copy(config, false);
int padding = 15;
// Initiate text paint objects
TextPaint titleTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
titleTextPaint.setStyle(Paint.Style.FILL);
titleTextPaint.setTextSize(28);
titleTextPaint.setColor(Color.rgb(64, 0, 0));
titleTextPaint.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/simple.otf"));
StaticLayout titleStaticLayout = new StaticLayout("منظم المسلم" + "\n" + subject, titleTextPaint, width - 3 * padding - logoBitmap.getWidth(), Layout.Alignment.ALIGN_NORMAL, 1.4f, 0.1f, false);
TextPaint matnTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.LINEAR_TEXT_FLAG);
matnTextPaint.setStyle(Paint.Style.FILL);
matnTextPaint.setTextSize(30);
matnTextPaint.setColor(Color.BLACK);
matnTextPaint.setTypeface(Typeface.createFromAsset(getActivity().getAssets(), "fonts/simple.otf"));
StaticLayout matnStaticLayout = new StaticLayout(body + "\n", matnTextPaint, width - 2 * padding, Layout.Alignment.ALIGN_CENTER, 1.4f, 0.1f, false);
int height = padding + Math.max(titleStaticLayout.getHeight(), logoBitmap.getHeight()) + padding + matnStaticLayout.getHeight() + padding;
Bitmap bitmap = backgroundBitmap.copy(config, true);
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
// create canvas and draw text on image.
Canvas canvas = new Canvas(bitmap);
canvas.save();
tileBitmap(canvas, backgroundBitmap);
canvas.drawBitmap(logoBitmap, width - padding - logoBitmap.getWidth(), padding, null);
canvas.translate(padding, 2 * padding);
titleStaticLayout.draw(canvas);
canvas.translate(0, padding + logoBitmap.getHeight());
matnStaticLayout.draw(canvas);
canvas.restore();
// share bitmap.
shareImage(bitmap);
}
public CollapsingTitleLayout(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setWillNotDraw(false);
paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CollapsingTitleLayout);
final boolean isRtl = ViewCompat.getLayoutDirection(this)
== ViewCompat.LAYOUT_DIRECTION_RTL;
// first check if all insets set the same
titleInsetStart = titleInsetEnd = titleInsetBottom =
a.getDimensionPixelSize(R.styleable.CollapsingTitleLayout_titleInset, 0);
titleInsetTop = titleInsetStart;
if (a.hasValue(R.styleable.CollapsingTitleLayout_titleInsetStart)) {
final int insetStart = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_titleInsetStart, 0);
if (isRtl) {
titleInsetEnd = insetStart;
} else {
titleInsetStart = insetStart;
}
}
if (a.hasValue(R.styleable.CollapsingTitleLayout_titleInsetTop)) {
titleInsetTop = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_titleInsetTop, 0);
}
if (a.hasValue(R.styleable.CollapsingTitleLayout_titleInsetEnd)) {
final int insetEnd = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_titleInsetEnd, 0);
if (isRtl) {
titleInsetStart = insetEnd;
} else {
titleInsetEnd = insetEnd;
}
}
if (a.hasValue(R.styleable.CollapsingTitleLayout_titleInsetBottom)) {
titleInsetBottom = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_titleInsetBottom, 0);
}
final int textAppearance = a.getResourceId(
R.styleable.CollapsingTitleLayout_android_textAppearance,
android.R.style.TextAppearance);
TypedArray atp = getContext().obtainStyledAttributes(textAppearance,
R.styleable.CollapsingTextAppearance);
paint.setColor(atp.getColor(R.styleable.CollapsingTextAppearance_android_textColor,
Color.WHITE));
collapsedTextSize = atp.getDimensionPixelSize(
R.styleable.CollapsingTextAppearance_android_textSize, 0);
if (atp.hasValue(R.styleable.CollapsingTextAppearance_font)) {
paint.setTypeface(FontUtil.get(getContext(),
atp.getString(R.styleable.CollapsingTextAppearance_font)));
}
atp.recycle();
if (a.hasValue(R.styleable.CollapsingTitleLayout_collapsedTextSize)) {
collapsedTextSize = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_collapsedTextSize, 0);
paint.setTextSize(collapsedTextSize);
}
maxExpandedTextSize = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_maxExpandedTextSize, Integer.MAX_VALUE);
lineHeightHint =
a.getDimensionPixelSize(R.styleable.CollapsingTitleLayout_lineHeightHint, 0);
maxLines = a.getInteger(R.styleable.CollapsingTitleLayout_android_maxLines, 5);
a.recycle();
}
@Override
public void updateMeasureState(TextPaint textPaint) {
textPaint.setTypeface(typeface);
}
@Override
public void updateDrawState(TextPaint tp) {
tp.setTextSize(tp.getTextSize() * FONT_SIZE);
tp.setTypeface(Typeface.MONOSPACE);
}
@Override
public void updateMeasureState(TextPaint p) {
p.setColor(textPaint.getColor());
p.setTypeface(textPaint.getTypeface());
p.setFlags(textPaint.getFlags());
}
/**
* 作用:设置与文字相关的参数
*/
private void setTextParams() {
// 文字大小
mTextSize = ScreenUtils.spToPx(mSettingManager.getTextSize());
// 行间距
mTextInterval = ScreenUtils.dpToPx(mSettingManager.getLineSpacing());
// 段落间距
mTextPara = ScreenUtils.dpToPx(mSettingManager.getParagraphSpacing());
Typeface typeface;
try {
if (mSettingManager.getFontPath() != null) {
typeface = Typeface.createFromFile(mSettingManager.getFontPath());
} else {
typeface = Typeface.SANS_SERIF;
}
} catch (Exception e) {
ToastUtils.toast(mContext, "字体文件未找,到恢复默认字体");
mSettingManager.setReadBookFont(null);
typeface = Typeface.SANS_SERIF;
}
// 绘制提示的画笔
mTipPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTipPaint.setColor(mTextColor);
mTipPaint.setTextAlign(Paint.Align.LEFT); // 绘制的起始点
mTipPaint.setTextSize(ScreenUtils.spToPx(DEFAULT_TIP_SIZE)); // Tip默认的字体大小
mTipPaint.setTypeface(typeface);
mTipPaint.setFakeBoldText(mSettingManager.getTextBold());
mTipPaint.setSubpixelText(true);
mTipPaint.setDither(true);
// 绘制标题的画笔
mTitlePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTitlePaint.setColor(mTextColor);
mTitlePaint.setTextSize(mTextSize * 1.25f);
mTitlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
mTitlePaint.setTypeface(typeface);
mTitlePaint.setFakeBoldText(true);
mTitlePaint.setSubpixelText(true);
mTitlePaint.setDither(true);
// 绘制页面内容的画笔
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(mTextColor);
mTextPaint.setTextSize(mTextSize);
mTextPaint.setTypeface(typeface);
mTextPaint.setFakeBoldText(mSettingManager.getTextBold());
mTextPaint.setSubpixelText(true);
mTextPaint.setDither(true);
}
@Override
public void updateMeasureState(TextPaint p) {
p.setTypeface(mTypeface);
// Note: This flag is required for proper typeface rendering
p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}