下面列出了android.graphics.Canvas#drawText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void draw(Canvas canvas, Paint paint) {
if (curIndex < 1 || curIndex > word.length) {
return;
}
for (int i = 0; i < curIndex; i++) {
paint.setTextSize(texts[i].size);
if (i == curIndex - 1) {
paint.setTextAlign(Paint.Align.CENTER);
// 绘制中间的字母
canvas.drawText(texts[i].content, texts[i].x, mBaseLine, paint);
} else {
// 由于文字绘制的原点影响了文字的间距,因为文字的宽度都是通过align.right进行一个间距的计算的,所以
// 当以中心为绘制原点的时候,相同的间距会变成原来的一半,这样就会导致间距缩小,尤其是小字体像i等,所以通过
// 设置不同的绘制原点,加上不同的位移来解决这个问题.
paint.setTextAlign(Paint.Align.LEFT);
if (texts[i].direction == Text.DIRECTION_RIGHT) {
canvas.drawText(texts[i].content,
texts[i].x + paint.measureText(word[curIndex - 1]) / 2 + texts[i].extraX, mBaseLine, paint);
} else if (texts[i].direction == Text.DIRECTION_LEFT) {
canvas.drawText(texts[i].content,
texts[i].x - paint.measureText(word[i]) - paint.measureText(word[curIndex - 1]) / 2 + texts[i].extraX, mBaseLine, paint);
}
}
}
}
protected Bitmap doInBackground(Object task, int index, long position) {
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
// Thread might be interrupted by cancel() call.
}
if (isCancelled(task)) {
return null;
}
String path = String.format(mPathPattern, (index + 1));
if (new File(path).exists()) {
return BitmapFactory.decodeFile(path);
} else {
Bitmap bmp = Bitmap.createBitmap(160, 160, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.drawColor(Color.YELLOW);
canvas.drawText(path, 10, 80, mPaint);
canvas.drawText(Integer.toString(index), 10, 150, mPaint);
return bmp;
}
}
@Override
protected void onDraw(Canvas canvas) {
mInnerRectF.set(0, 0, mViewSize, mViewSize);
mInnerRectF.offset((getWidth() - mViewSize) / 2, (getHeight() - mViewSize) / 2);
float centerX = mInnerRectF.centerX();
float centerY = mInnerRectF.centerY();
int xPos = (int) centerX;
int yPos = (int) (centerY - (mTitleTextPaint.descent() + mTitleTextPaint.ascent()) / 2);
canvas.drawOval(mInnerRectF, mBackgroundPaint);
canvas.drawText(mTitleText,
xPos,
yPos,
mTitleTextPaint);
}
@Override
public void drawText(@NonNull Canvas canvas, @NonNull BaseKChartView view, int position, float x, float y) {
y = y - BaseKChartView.mChildTextPaddingY;
x = BaseKChartView.mChildTextPaddingX;
String text = "";
IRSI point = (IRSI) view.getItem(position);
text = "RSI1:" + view.formatValue(point.getRsi1()) + " ";
canvas.drawText(text, x, y, mRSI1Paint);
x += mRSI1Paint.measureText(text);
x += BaseKChartView.mTextPaddingLeft;
text = "RSI2:" + view.formatValue(point.getRsi2()) + " ";
canvas.drawText(text, x, y, mRSI2Paint);
x += mRSI2Paint.measureText(text);
x += BaseKChartView.mTextPaddingLeft;
text = "RSI3:" + view.formatValue(point.getRsi3()) + " ";
canvas.drawText(text, x, y, mRSI3Paint);
}
/**
* Override this method
*/
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Get the focus change the background color.
int height = getHeight();// Get the corresponding height
int width = getWidth(); // Get the corresponding width
float singleHeight = (height * 1f) / b.length;// Gets the height of each letter
singleHeight = (height * 1f - singleHeight / 2) / b.length;
for (int i = 0; i < b.length; i++) {
paint.setColor(Color.rgb(86, 86, 86));
paint.setTypeface(Typeface.DEFAULT);
paint.setAntiAlias(true);
paint.setTextSize(23);
// x-coordinate of the middle - half the width of the string.
float xPos = width / 2 - paint.measureText(b[i]) / 2;
float yPos = singleHeight * i + singleHeight;
canvas.drawText(b[i], xPos, yPos, paint);
paint.reset();// Reset Brushes
}
}
/**
* Create a bitmap from a text
*
* @param text
* Text which the bitmap is created for
* @param textSize
* Text size
* @param textColor
* Text color
* @param typeface
* Typeface of text
*
* @return a bitmap on which is text is drawn
*/
public static Bitmap createBitmapFromText(String text, float textSize, int textColor,
Typeface typeface) {
Paint paint = new Paint(ANTI_ALIAS_FLAG);
paint.setTextSize(textSize);
paint.setColor(textColor);
paint.setTypeface(typeface);
paint.setTextAlign(Paint.Align.LEFT);
float baseline = -paint.ascent();
int width = (int) (paint.measureText(text) + 0.5f);
int height = (int) (baseline + paint.descent() + 0.5f);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, 0, baseline, paint);
return bitmap;
}
private void drawPathAContentBitmap(Bitmap bitmap, Paint pathPaint) {
Canvas mCanvas = new Canvas(bitmap);
//下面开始绘制区域内的内容...
mCanvas.drawPath(getPathDefault(), pathPaint);
mCanvas.drawText("这是在A区域的内容...AAAA", viewWidth - 260, viewHeight - 100, textPaint);
//结束绘制区域内的内容...
}
@Override
public void drawMonthDay(Canvas canvas, int year, int month, int day,
int x, int y, int startX, int stopX, int startY, int stopY) {
if (mSelectedDay == day) {
canvas.drawCircle(x , y - (MINI_DAY_NUMBER_TEXT_SIZE / 3), DAY_SELECTED_CIRCLE_SIZE,
mSelectedCirclePaint);
}
if (mHasToday && mToday == day) {
mMonthNumPaint.setColor(mTodayNumberColor);
} else {
mMonthNumPaint.setColor(mDayTextColor);
}
canvas.drawText(String.format("%d", day), x, y, mMonthNumPaint);
}
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
int centre = getWidth() / 2;
int radius = (int) (((float) centre) - (this.roundWidth / 2.0f));
this.paint.setColor(this.roundColor);
this.paint.setStyle(Style.STROKE);
this.paint.setStrokeWidth(this.roundWidth);
this.paint.setAntiAlias(true);
canvas.drawCircle((float) centre, (float) centre, (float) radius, this.paint);
this.paint.setStrokeWidth(0.0f);
this.paint.setColor(this.textColor);
this.paint.setTextSize(this.textSize);
this.paint.setStrokeCap(Cap.ROUND);
this.paint.setTypeface(Typeface.DEFAULT_BOLD);
int percent = (int) ((((float) this.progress) / ((float) this.max)) * 100.0f);
float textWidth = this.paint.measureText(percent + "%");
if (this.textIsDisplayable && this.style == 0) {
canvas.drawText(percent + "%", ((float) centre) - (textWidth / 2.0f), ((float) centre) + (this.textSize / 2.0f), this.paint);
}
this.paint.setStrokeWidth(this.roundWidthPlan);
this.paint.setColor(this.roundProgressColor);
RectF oval = new RectF((float) (centre - radius), (float) (centre - radius), (float) (centre + radius), (float) (centre + radius));
switch (this.style) {
case 0:
this.paint.setStyle(Style.STROKE);
canvas.drawArc(oval, 270.0f, (float) ((this.progress * 360) / this.max), false, this.paint);
return;
case 1:
this.paint.setStyle(Style.FILL_AND_STROKE);
if (this.progress != 0) {
canvas.drawArc(oval, 270.0f, (float) ((this.progress * 360) / this.max), true, this.paint);
return;
}
return;
default:
return;
}
}
static Bitmap createRoundIconWithText(Context context, String letter) {
//calculate dimensions
//-1 to take into account the shadow layer
int w = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
int h = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
int r = w / 2 - 1;
//create bitmap
Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
//draw a circle of the same dimensions
Canvas canvas = new Canvas(b);
Paint paint = new Paint();
int color = ContextCompat.getColor(context, getRandom(SolidWallpaperUtils.material_colors));
paint.setColor(color);
final int SHADOW_COLOR = 0x80000000;
paint.setShadowLayer(0.5f, 1, 1, SHADOW_COLOR);
paint.setAntiAlias(true);
canvas.drawCircle(r, r, r, paint);
Paint textPaint = new Paint();
textPaint.setColor(Utilities.getComplementaryColor(color));
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(w / 2);
textPaint.setAntiAlias(true);
textPaint.setTypeface(Typeface.SANS_SERIF);
int xPos = (canvas.getWidth() / 2);
int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2));
canvas.drawText(letter, xPos, yPos, textPaint);
return b;
}
@Override
protected void onDrawText(Canvas canvas, Calendar calendar, int x, boolean hasScheme, boolean isSelected) {
float baselineY = mTextBaseLine - dipToPx(getContext(), 1);
int cx = x + mItemWidth / 2;
if (isSelected) {
canvas.drawText(calendar.isCurrentDay() ? "今" : "选",
cx,
baselineY,
mSelectTextPaint);
} else if (hasScheme) {
canvas.drawText(calendar.isCurrentDay() ? "今" : String.valueOf(calendar.getDay()),
cx,
baselineY,
calendar.isCurrentDay() ? mCurDayTextPaint :
calendar.isCurrentMonth() ? mSchemeTextPaint : mOtherMonthTextPaint);
} else {
canvas.drawText(calendar.isCurrentDay() ? "今" : String.valueOf(calendar.getDay()),
cx,
baselineY,
calendar.isCurrentDay() ? mCurDayTextPaint :
calendar.isCurrentMonth() ? mCurMonthTextPaint : mOtherMonthTextPaint);
}
//日期是否可用?拦截
if (onCalendarIntercept(calendar)) {
canvas.drawLine(x + mH, mH, x + mItemWidth - mH, mItemHeight - mH, mDisablePaint);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制圆
canvas.drawCircle(mCircleXY, mCircleXY, mRadius, mCirclePaint);
// 绘制弧线
canvas.drawArc(mArcRectF, 270, mSweepAngle, false,mArcPaint);
// 绘制文字
canvas.drawText(mShowText, 0, mShowText.length(),mCircleXY, mCircleXY + (mShowTextSize / 4),mTextPaint);
}
public static boolean isLangSupported(Context context, String text) {
int sdk = android.os.Build.VERSION.SDK_INT;
int w = 200, h = 80;
Resources resources = context.getResources();
float scale = resources.getDisplayMetrics().density;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bitmap = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
Bitmap orig = bitmap.copy(conf, false);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.rgb(0, 0, 0));
paint.setTextSize((int) (14 * scale));
// draw text to the Canvas center
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int x = (bitmap.getWidth() - bounds.width()) / 2;
int y = (bitmap.getHeight() + bounds.height()) / 2;
canvas.drawText(text, x, y, paint);
boolean res = false;
if (sdk < android.os.Build.VERSION_CODES.HONEYCOMB) {
res = !(orig == bitmap);
} else {
res = !orig.sameAs(bitmap);
}
orig.recycle();
bitmap.recycle();
return res;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//这里的矩阵是相对于Paint将要绘制的区域的(还是View?)
//一定要reset或setscale
mWaveShaderMatrix.reset();
//根据用户的设置的波长以及振幅比例缩放
mWaveShaderMatrix.postScale(mWaveLengthRatio, mWaveAmplitudeRatio, 0, mDefaultWaveHeight);
//平移Shader产生动画效果
mWaveShaderMatrix.postTranslate(mAwayLeftRatio * getMeasuredWidth() * mWaveLengthRatio
, (0.5f - mWaveHeightRatio) * getHeight());
//确定最终效果
mWaveShader.setLocalMatrix(mWaveShaderMatrix);
float borderWidth = mBorderPaint.getStrokeWidth();
/*CIRCLE,SQUARE,RECTANGLE*/
switch (mType){
//圆
case 0:
//绘制
int radius = Math.min(getWidth(), getHeight()) / 2;
if(borderWidth > 0){
mBorderPaint.setColor(mWaveColor);
canvas.drawCircle(radius, radius, (2 * radius - borderWidth) / 2f - 1f, mBorderPaint);
}
canvas.drawCircle(radius, radius, radius, mWavePaint);
break;
//正方形
case 1:
if(borderWidth > 0){
canvas.drawRect(
borderWidth / 2f,
borderWidth / 2f,
Math.min(getWidth(), getHeight()) - borderWidth / 2f - 0.5f,
Math.min(getWidth(), getHeight()) - borderWidth / 2f - 0.5f,
mBorderPaint);
}
canvas.drawRect(borderWidth
, borderWidth
, Math.min(getWidth(), getHeight()) - borderWidth
, Math.min(getWidth(), getHeight()) - borderWidth
, mWavePaint);
break;
//长方形
//不太好看
/*case 2:
if(borderWidth > 0){
canvas.drawRect(borderWidth / 2f,
borderWidth / 2f,
getWidth() - borderWidth / 2f - 0.5f,
getHeight() - borderWidth / 2f - 0.5f,
mBorderPaint);
}
canvas.drawRect(borderWidth
, borderWidth
, getWidth() - borderWidth
, getHeight() - borderWidth
, mWavePaint);
break;*/
}
float textWidth = mTitlePaint.measureText(mTitletext);
canvas.drawText(mTitletext
, (getWidth() - textWidth) / 2
, getHeight() / 2 - (mTitlePaint.descent() + mTitlePaint.ascent()) / 2
, mTitlePaint);
}
@Override
public void draw(@NonNull Canvas canvas) {
mPaint.setColor(Color.BLACK);
canvas.drawText("IMAGE", 30f, 30f, mPaint);
}
@Override
public void onDraw(Canvas canvas) {
int viewWidth = getWidth();
if (viewWidth == 0 || !mIsInitialized) {
return;
}
if (!mDrawValuesReady) {
int layoutXCenter = getWidth() / 2;
int layoutYCenter = getHeight() / 2;
int circleRadius =
(int) (Math.min(layoutXCenter, layoutYCenter) * mCircleRadiusMultiplier);
mAmPmCircleRadius = (int) (circleRadius * mAmPmCircleRadiusMultiplier);
layoutYCenter += mAmPmCircleRadius*0.75;
int textSize = mAmPmCircleRadius * 3 / 4;
mPaint.setTextSize(textSize);
// Line up the vertical center of the AM/PM circles with the bottom of the main circle.
mAmPmYCenter = layoutYCenter - mAmPmCircleRadius / 2 + circleRadius;
// Line up the horizontal edges of the AM/PM circles with the horizontal edges
// of the main circle.
mAmXCenter = layoutXCenter - circleRadius + mAmPmCircleRadius;
mPmXCenter = layoutXCenter + circleRadius - mAmPmCircleRadius;
mDrawValuesReady = true;
}
// We'll need to draw either a lighter blue (for selection), a darker blue (for touching)
// or white (for not selected).
int amColor = mUnselectedColor;
int amAlpha = 255;
int amTextColor = mAmPmTextColor;
int pmColor = mUnselectedColor;
int pmAlpha = 255;
int pmTextColor = mAmPmTextColor;
if (mAmOrPm == AM) {
amColor = mSelectedColor;
amAlpha = mSelectedAlpha;
amTextColor = mAmPmSelectedTextColor;
} else if (mAmOrPm == PM) {
pmColor = mSelectedColor;
pmAlpha = mSelectedAlpha;
pmTextColor = mAmPmSelectedTextColor;
}
if (mAmOrPmPressed == AM) {
amColor = mTouchedColor;
amAlpha = mSelectedAlpha;
} else if (mAmOrPmPressed == PM) {
pmColor = mTouchedColor;
pmAlpha = mSelectedAlpha;
}
if (mAmDisabled) {
amColor = mUnselectedColor;
amTextColor = mAmPmDisabledTextColor;
}
if (mPmDisabled) {
pmColor = mUnselectedColor;
pmTextColor = mAmPmDisabledTextColor;
}
// Draw the two circles.
mPaint.setColor(amColor);
mPaint.setAlpha(amAlpha);
canvas.drawCircle(mAmXCenter, mAmPmYCenter, mAmPmCircleRadius, mPaint);
mPaint.setColor(pmColor);
mPaint.setAlpha(pmAlpha);
canvas.drawCircle(mPmXCenter, mAmPmYCenter, mAmPmCircleRadius, mPaint);
// Draw the AM/PM texts on top.
mPaint.setColor(amTextColor);
int textYCenter = mAmPmYCenter - (int) (mPaint.descent() + mPaint.ascent()) / 2;
canvas.drawText(mAmText, mAmXCenter, textYCenter, mPaint);
mPaint.setColor(pmTextColor);
canvas.drawText(mPmText, mPmXCenter, textYCenter, mPaint);
}
public void draw(Canvas canvas) {
if (mState == STATE_HIDDEN)
return;
// mAlphaRate determines the rate of opacity
mIndexBarPaint.setColor(Color.BLACK);
mIndexBarPaint.setAlpha((int) (64 * mAlphaRate));
mIndexBarPaint.setAntiAlias(true);
canvas.drawRoundRect(mIndexbarRect, 5 * mDensity, 5 * mDensity, mIndexBarPaint);
if (mSections != null && mSections.size() > 0) {
// Preview is shown when mCurrentSection is set
if (mCurrentSection >= 0) {
mPreviewPaint.setColor(Color.BLACK);
mPreviewPaint.setAlpha(96);
mPreviewPaint.setAntiAlias(true);
mPreviewPaint.setShadowLayer(3, 0, 0, Color.argb(64, 0, 0, 0));
mPreviewTextPaint.setColor(Color.WHITE);
mPreviewTextPaint.setAntiAlias(true);
mPreviewTextPaint.setTextSize(50 * mScaledDensity);
float previewTextWidth = mPreviewTextPaint.measureText(mSections.get(mCurrentSection));
float previewSize = 2 * mPreviewPadding + mPreviewTextPaint.descent() - mPreviewTextPaint.ascent();
mPreviewRect.set((mListViewWidth - previewSize) / 2
, (mListViewHeight - previewSize) / 2
, (mListViewWidth - previewSize) / 2 + previewSize
, (mListViewHeight - previewSize) / 2 + previewSize);
canvas.drawRoundRect(mPreviewRect, 5 * mDensity, 5 * mDensity, mPreviewPaint);
canvas.drawText(mSections.get(mCurrentSection), mPreviewRect.left + (previewSize - previewTextWidth) / 2 - 1
, mPreviewRect.top + mPreviewPadding - mPreviewTextPaint.ascent() + 1, mPreviewTextPaint);
}
mIndexPaint.setColor(Color.WHITE);
mIndexPaint.setAlpha((int) (255 * mAlphaRate));
mIndexPaint.setAntiAlias(true);
mIndexPaint.setTextSize(12 * mScaledDensity);
float sectionHeight = (mIndexbarRect.height() - 2 * mIndexbarMargin) / mSections.size();
float paddingTop = (sectionHeight - (mIndexPaint.descent() - mIndexPaint.ascent())) / 2;
for (int i = 0; i < mSections.size(); i++) {
float paddingLeft = (mIndexbarWidth - mIndexPaint.measureText(mSections.get(i))) / 2;
canvas.drawText(mSections.get(i), mIndexbarRect.left + paddingLeft
, mIndexbarRect.top + mIndexbarMargin + sectionHeight * i + paddingTop - mIndexPaint.ascent(), mIndexPaint);
}
}
}
@Override
public void invalidate() {
super.invalidate();
final SurfaceHolder holder = mHolder;
if (holder == null) {
return;
}
final Canvas canvas = holder.lockCanvas();
if (canvas == null) {
return;
}
// Clear the canvas.
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
if (getVisibility() != View.VISIBLE) {
holder.unlockCanvasAndPost(canvas);
return;
}
final int width = getWidth();
final int height = getHeight();
// Draw the pretty gradient background.
mGradientBackground.setBounds(0, 0, width, height);
mGradientBackground.draw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(mContext.getResources().getDimensionPixelSize(
R.dimen.search_text_font_size));
canvas.drawText(
mContext.getString(R.string.search_dialog_label, mQueryText),
width / 2.0f,
height / 2.0f,
paint);
holder.unlockCanvasAndPost(canvas);
}
public void draw(Canvas canvas) {
final int saveCount = canvas.save();
if (mTextToDraw != null && mDrawTitle) {
float x = mCurrentDrawX;
float y = mCurrentDrawY;
final boolean drawTexture = mUseTexture && mExpandedTitleTexture != null;
final float ascent;
final float descent;
if (drawTexture) {
ascent = mTextureAscent * mScale;
descent = mTextureDescent * mScale;
} else {
ascent = mTextPaint.ascent() * mScale;
descent = mTextPaint.descent() * mScale;
}
if (DEBUG_DRAW) {
// Just a debug tool, which drawn a magenta rect in the text bounds
canvas.drawRect(mCurrentBounds.left, y + ascent, mCurrentBounds.right, y + descent,
DEBUG_DRAW_PAINT);
}
if (drawTexture) {
y += ascent;
}
if (mScale != 1f) {
canvas.scale(mScale, mScale, x, y);
}
if (drawTexture) {
// If we should use a texture, draw it instead of text
canvas.drawBitmap(mExpandedTitleTexture, x, y, mTexturePaint);
} else {
canvas.drawText(mTextToDraw, 0, mTextToDraw.length(), x, y, mTextPaint);
}
}
canvas.restoreToCount(saveCount);
}
/**
* Draws the provided label at the given position.
*
* @param c canvas to draw with
* @param x
* @param y
* @param label the label to draw
*/
protected void drawLabel(Canvas c, float x, float y, String label) {
c.drawText(label, x, y, mLegendLabelPaint);
}