在本文中,我们将详细介绍Java-没有图形的FontMetrics的各个方面,并为您提供关于java没有图标的相关解答,同时,我们也将为您带来关于.NoClassDefFoundError:com/y
在本文中,我们将详细介绍Java-没有图形的FontMetrics的各个方面,并为您提供关于java没有图标的相关解答,同时,我们也将为您带来关于.NoClassDefFoundError: com/yammer/metrics/Metrics、android fontMetrics 使用说明、android.graphics.Paint.FontMetricsInt的实例源码、android.graphics.Paint.FontMetrics的实例源码的有用知识。
本文目录一览:- Java-没有图形的FontMetrics(java没有图标)
- .NoClassDefFoundError: com/yammer/metrics/Metrics
- android fontMetrics 使用说明
- android.graphics.Paint.FontMetricsInt的实例源码
- android.graphics.Paint.FontMetrics的实例源码
Java-没有图形的FontMetrics(java没有图标)
如何在不使用Graphics的情况下获取FontMetrics?我想在构造函数中获取FontMetrics,现在我这样做:
BufferedImage bi = new BufferedImage(5, 5, BufferedImage.TYPE_INT_RGB);FontMetrics fm = bi.getGraphics().getFontMetrics(font);int width = fm.stringWidth(pattern);int height = fm.getHeight();
答案1
小编典典嗯…需要图形来获取FontMetrics是很合逻辑的。字体高度,宽度等在各种显示器上可能有所不同。
如果您有一些组件,则可以使用它来获取FontMetrics:
component.getFontMetrics(font);
.NoClassDefFoundError: com/yammer/metrics/Metrics
localhost, executor driver): java.lang.NoClassDefFoundError: com/yammer/metrics/Metrics
at kafka.metrics.KafkaMetricsGroup$class.newMeter(KafkaMetricsGroup.scala:79)
at kafka.consumer.ZookeeperConsumerConnector.newMeter(ZookeeperConsumerConnector.scala:83)
at kafka.consumer.ZookeeperConsumerConnector.<init>(ZookeeperConsumerConnector.scala:107)
at kafka.consumer.ZookeeperConsumerConnector.<init>(ZookeeperConsumerConnector.scala:143)
at kafka.consumer.Consumer$.create(ConsumerConnector.scala:94)
at org.apache.spark.streaming.kafka.KafkaReceiver.onStart(KafkaInputDStream.scala:100)
at org.apache.spark.streaming.receiver.ReceiverSupervisor.startReceiver(ReceiverSupervisor.scala:149)
at org.apache.spark.streaming.receiver.ReceiverSupervisor.start(ReceiverSupervisor.scala:131)
at org.apache.spark.streaming.scheduler.ReceiverTracker$ReceiverTrackerEndpoint
at org.apache.spark.SparkContext
at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:87)
at org.apache.spark.scheduler.Task.run(Task.scala:99)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:282)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
android fontMetrics 使用说明
FontMetrics对象 它以四个基本坐标为基准,分别为:
・FontMetrics.top ・FontMetrics.ascent ・FontMetrics.descent ・FontMetrics.bottom
实例JAVA代码如下: Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG); textPaint.setTextSize( 35); textPaint.setColor( Color.WHITE);
// FontMetrics对象 FontMetrics fontMetrics = textPaint.getFontMetrics(); String text = "abcdefghijklmnopqrstu";
// 计算每一个坐标 float baseX = 0; float baseY = 100; float topY = baseY + fontMetrics.top; float ascentY = baseY + fontMetrics.ascent; float descentY = baseY + fontMetrics.descent; float bottomY = baseY + fontMetrics.bottom;
// 绘制文本 canvas.drawText( text, baseX, baseY, textPaint);
// BaseLine描画 Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); baseLinePaint.setColor( Color.RED); canvas.drawLine(0, baseY, getWidth(), baseY, baseLinePaint);
// Base描画 canvas.drawCircle( baseX, baseY, 5, baseLinePaint);
// TopLine描画 Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); topLinePaint.setColor( Color.LTGRAY); canvas.drawLine(0, topY, getWidth(), topY, topLinePaint);
// AscentLine描画 Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); ascentLinePaint.setColor( Color.GREEN); canvas.drawLine(0, ascentY, getWidth(), ascentY, ascentLinePaint);
// DescentLine描画 Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); descentLinePaint.setColor( Color.YELLOW); canvas.drawLine(0, descentY, getWidth(), descentY, descentLinePaint);
// ButtomLine描画 Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); bottomLinePaint.setColor( Color.MAGENTA); canvas.drawLine(0, bottomY, getWidth(), bottomY, bottomLinePaint);
android.graphics.Paint.FontMetricsInt的实例源码
private void drawData(Canvas canvas) { // Draw the selected text first,and then draw up the rest of the text. float scale = parabola(mViewHeight / 4.0f,mMoveLen); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); // Text center drawing,pay attention to the calculation of baseline to reach the center,y value is text central coordinates. float x = (float) (mViewWidth / 2.0); float y = (float) (mViewHeight / 2.0 + mMoveLen); FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); canvas.drawText(mDataList.get(mCurrentSelected),x,baseline,mPaint); // Draw the top data. for (int i = 1; (mCurrentSelected - i) >= 0; i++) { drawOtherText(canvas,i,-1); } // Draw below data. for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) { drawOtherText(canvas,1); } }
@Override protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) { final int size = MeasureSpec.getSize(widthMeasureSpec); final int mode = MeasureSpec.getMode(widthMeasureSpec); if (!useSystemEmoji() && getEllipsize() == TruncateAt.END && !TextUtils.isEmpty(source) && (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.EXACTLY) && getPaint().breakText(source,source.length()-1,true,size,null) != source.length()) { needsEllipsizing = true; FontMetricsInt font = getPaint().getFontMetricsInt(); super.onMeasure(MeasureSpec.makeMeasureSpec(size,MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(Math.abs(font.top - font.bottom),MeasureSpec.EXACTLY)); } else { needsEllipsizing = false; super.onMeasure(widthMeasureSpec,heightMeasureSpec); } }
@Override public int getSize(Paint paint,CharSequence text,int start,int end,FontMetricsInt fm) { Rect rect = this.getDrawable().getBounds(); if (fm != null) { FontMetricsInt fontMetricsInt = paint.getFontMetricsInt(); end = fontMetricsInt.bottom - fontMetricsInt.top; int var = rect.bottom - rect.top; start = var / 2 - end / 4; end = var / 2 + end / 4; fm.ascent = -end; fm.top = -end; fm.bottom = start; fm.descent = start; } return rect.right; }
private void drawData(Canvas canvas) { // 先绘制选中的text再往上往下绘制其余的text float scale = parabola(mViewHeight / 4.0f,mMoveLen); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); // text居中绘制,注意baseline的计算才能达到居中,y值是text中心坐标 float x = (float) (mViewWidth / 2.0); float y = (float) (mViewHeight / 2.0 + mMoveLen); FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); if (mDataList.size() > 0) { canvas.drawText(mDataList.get(mCurrentSelected),mPaint); } // 绘制上方data for (int i = 1; (mCurrentSelected - i) >= 0; i++) { drawOtherText(canvas,-1); } // 绘制下方data for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) { drawOtherText(canvas,1); } }
private void drawData(Canvas canvas) { // 先绘制选中的text再往上往下绘制其余的text float scale = parabola(mViewHeight / 4.0f,mMoveLen); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); // text居中绘制,注意baseline的计算才能达到居中,y值是text中心坐标 float x = (float) (mViewWidth / 2.0); float y = (float) (mViewHeight / 2.0 + mMoveLen); FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); if (null != mItemProvider) { String itemData = mItemProvider.getItem(mDataList.get(mCurrentSelected),mCurrentSelected); canvas.drawText(itemData,1); } }
/** * @param canvas * @param position 距离mCurrentSelected的差值 * @param type 1表示向下绘制,-1表示向上绘制 */ private void drawOtherText(Canvas canvas,int position,int type) { float d = (float) (MARGIN_ALPHA * mMinTextSize * position + type * mMoveLen); float scale = parabola(mViewHeight / 4.0f,d); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); float y = (float) (mViewHeight / 2.0 + type * d); FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); if (null != mItemProvider) { int index = mCurrentSelected + type * position; String itemData = mItemProvider.getItem(mDataList.get(index),index); canvas.drawText(itemData,(float) (mViewWidth / 2.0),mPaint); } }
private void drawProgress(Canvas canvas) { Paint bgPaint = getProgresspaint(); bgPaint.setColor(this.mIndicatorBgColor); canvas.drawLine(14.0f,(float) (this.mHeight / 2),(float) (this.mWidth - 14),(float) (this.mHeight / 2),bgPaint); if (this.mProgress != 0.0f) { Paint progresspaint = getProgresspaint(); progresspaint.setColor(this.mIndicatorProgressColor); int stopX = (int) (((float) (this.mWidth - 28)) * this.mProgress); canvas.drawLine(14.0f,(float) stopX,(float) (this .mHeight / 2),progresspaint); Paint textPain = getAlertPaint(); textPain.setTextSize((float) this.mIndicatorSize); textPain.setColor(this.mIndicatorTextColor); int textWidth = ViewUtils.getTextWidth(textPain,this.mAlert); Paint alertPaint = getProgresspaint(); alertPaint.setColor(this.mIndicatorProgressColor); alertPaint.setstrokeWidth(78.4f); canvas.drawLine((float) (stopX - (textWidth / 2)),(float) ((textWidth / 2) + stopX),alertPaint); FontMetricsInt fmi = textPain.getFontMetricsInt(); Canvas canvas2 = canvas; canvas2.drawText(this.mAlert,(float) (stopX - (textWidth / 2)),(float) ((this .mHeight / 2) + (Math.abs(fmi.bottom + fmi.top) / 2)),textPain); } }
@Override protected void onMeasure(int widthMeasureSpec,heightMeasureSpec); } }
private void drawData(Canvas canvas) { // 先绘制选中的text再往上往下绘制其余的text float scale = parabola(mViewHeight / 4.0f,mMoveLen); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); // text居中绘制,注意baseline的计算才能达到居中,y值是text中心坐标 float x = (float) (mViewWidth / 2.0); float y = (float) (mViewHeight / 2.0 + mMoveLen); FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); canvas.drawText(mDataList.get(mCurrentSelected),mPaint); // 绘制上方data for (int i = 1; (mCurrentSelected - i) >= 0; i++) { drawOtherText(canvas,1); } }
/** * 代码跟父类代码相似,就是getCachedDrawable()替换成getDrawable(),因为前者里面的图片是WeakReference, * 容易被gc回收,所以这里要避免这个问题 */ @Override public int getSize(Paint paint,FontMetricsInt fm) { Drawable d = getDrawable(); if (lineHeight > 0) { return (int) (d.getIntrinsicWidth() * scale); } else { Rect rect = d.getBounds(); if (fm != null) { fm.ascent = -rect.bottom; fm.descent = 0; fm.top = fm.ascent; fm.bottom = 0; } return rect.right; } }
private void drawData(Canvas canvas) { // 先绘制选中的text再往上往下绘制其余的text float scale = parabola(mViewHeight / 4.0f,1); } }
private void drawData(Canvas canvas) { // 先绘制选中的text再往上往下绘制其余的text float scale = parabola(mViewHeight / 3.2f,1); } }
private void drawData(Canvas canvas) { // 先绘制选中的text再往上往下绘制其余的text float scale = parabola(mViewHeight / 4.0f,mMoveLen); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); // text居中绘制,注意baseline的计算才能达到居中,y值是text中心坐标 float x = (float) (mViewWidth / 2.0); float y = (float) (mViewHeight / 2.0 + mMoveLen); FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); canvas.drawText(mDataList.get(mCurrentSelected).getPickName(),1); } }
private void drawData(Canvas canvas) { // 先绘制选中的text再往上往下绘制其余的text float scale = parabola(mViewHeight / 4.0f,1); } }
private void drawData(Canvas canvas) { // 先绘制选中的text再往上往下绘制其余的text float scale = parabola(mViewHeight / 4.0f,1); } }
@Override protected void onMeasure(int widthMeasureSpec,heightMeasureSpec); } }
private void drawData(Canvas canvas) { // 先绘制选中的text再往上往下绘制其余的text float scale = parabola(mViewHeight / 4.0f,mPaint); //暴力法则,上下都最多绘制10条数据 int count = Math.min(10,mDataList.size()); // 绘制上方data for (int i = 1; i <= count; i++) { drawOtherText(canvas,-1); } // 绘制下方data for (int i = 1; i <= count; i++) { drawOtherText(canvas,1); } }
/** * @param canvas * @param position * 距离mCurrentSelected的差 * @param type * 1表示向下绘制,-1表示向上绘制 */ private void drawOtherText(Canvas canvas,int type) { float d = (float) (MARGIN_ALPHA * mMinTextSize * position + type * mMoveLen); float scale = parabola(mViewHeight / 4.0f,d); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); float y = (float) (mViewHeight / 2.0 + type * d); FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); int valuePosition = mCurrentSelected + type * position; //调整位置偏差在 合法范围 int dataSize = mDataList.size(); valuePosition = (valuePosition + dataSize) % dataSize; //if(valuePosition >= 0 & valuePosition < dataSize) {//以防万一 canvas.drawText(mDataList.get(valuePosition),mPaint); //} }
private static TextProperty computeTextProperty(final String string,final int width,final int height,final Paint paint) { final FontMetricsInt fm = paint.getFontMetricsInt(); final int h = (int) Math.ceil(fm.bottom - fm.top); int maxContentWidth = 0; final String[] lines = Cocos2dxBitmap.splitString(string,width,height,paint); if (width != 0) { maxContentWidth = width; } else { /* Compute the max width. */ int temp = 0; for (final String line : lines) { temp = (int) FloatMath.ceil(paint.measureText(line,line.length())); if (temp > maxContentWidth) { maxContentWidth = temp; } } } return new TextProperty(maxContentWidth,h,lines); }
private static int computeY(final FontMetricsInt fontMetricsInt,final int constrainHeight,final int totalHeight,final int verticalAlignment) { int y = -fontMetricsInt.top; if (constrainHeight > totalHeight) { switch (verticalAlignment) { case VERTICALALIGN_TOP: y = -fontMetricsInt.top; break; case VERTICALALIGN_CENTER: y = -fontMetricsInt.top + (constrainHeight - totalHeight) / 2; break; case VERTICALALIGN_BottOM: y = -fontMetricsInt.top + (constrainHeight - totalHeight); break; default: break; } } return y; }
private static TextProperty computeTextProperty(final String string,lines); }
private static int computeY(final FontMetricsInt fontMetricsInt,final int verticalAlignment) { int y = -fontMetricsInt.top; if (constrainHeight > totalHeight) { switch (verticalAlignment) { case VERTICALALIGN_TOP: y = -fontMetricsInt.top; break; case VERTICALALIGN_CENTER: y = -fontMetricsInt.top + (constrainHeight - totalHeight) / 2; break; case VERTICALALIGN_BottOM: y = -fontMetricsInt.top + (constrainHeight - totalHeight); break; default: break; } } return y; }
private static TextProperty computeTextProperty(final String pString,final int pWidth,final int pHeight,final Paint pPaint) { final FontMetricsInt fm = pPaint.getFontMetricsInt(); final int h = (int) Math.ceil(fm.bottom - fm.top); int maxContentWidth = 0; final String[] lines = Cocos2dxBitmap.splitString(pString,pWidth,pHeight,pPaint); if (pWidth != 0) { maxContentWidth = pWidth; } else { /* Compute the max width. */ int temp = 0; for (final String line : lines) { temp = (int) FloatMath.ceil(pPaint.measureText(line,lines); }
private static int computeY(final FontMetricsInt pFontMetricsInt,final int pConstrainHeight,final int pTotalHeight,final int pVerticalAlignment) { int y = -pFontMetricsInt.top; if (pConstrainHeight > pTotalHeight) { switch (pVerticalAlignment) { case VERTICALALIGN_TOP: y = -pFontMetricsInt.top; break; case VERTICALALIGN_CENTER: y = -pFontMetricsInt.top + (pConstrainHeight - pTotalHeight) / 2; break; case VERTICALALIGN_BottOM: y = -pFontMetricsInt.top + (pConstrainHeight - pTotalHeight); break; default: break; } } return y; }
@Override public int getSize(Paint paint,FontMetricsInt fm) { fm = fm == null ? paint.getFontMetricsInt() : fm; int iconSize = fm.bottom - fm.top; mDrawable.setBounds(0,iconSize,iconSize); return super.getSize(paint,text,start,end,fm); }
/** * @param position The difference between the distance mCurrentSelected. * @param type The difference from mCurrentSelected 1 means downward drawing,and -1 indicates upward rendering. */ private void drawOtherText(Canvas canvas,int type) { float d = MARGIN_ALPHA * mMinTextSize * position + type * mMoveLen; float scale = parabola(mViewHeight / 4.0f,d); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; nPaint.setTextSize(size); nPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); float y = (float) (mViewHeight / 2.0 + type * d); FontMetricsInt fmi = nPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); canvas.drawText(mDataList.get(mCurrentSelected + type * position),nPaint); }
@Override public int getSize(Paint paint,FontMetricsInt fm) { if (fm != null && this.fm != null) { fm.ascent = this.fm.ascent; fm.descent = this.fm.descent; fm.top = this.fm.top; fm.bottom = this.fm.bottom; return size; } else { return super.getSize(paint,fm); } }
/** * @param canvas * @param position 距离mCurrentSelected的差值 * @param type 1表示向下绘制,-1表示向上绘制 */ private void drawOtherText(Canvas canvas,int type) { float d = (float) (MARGIN_ALPHA * mMinTextSize * position + type * mMoveLen); float scale = parabola(mViewHeight / 4.0f,d); float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize; mPaint.setTextSize(size); mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha)); float y = (float) (mViewHeight / 2.0 + type * d); FontMetricsInt fmi = mPaint.getFontMetricsInt(); float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0)); canvas.drawText(mDataList.get(mCurrentSelected + type * position),mPaint); }
@Override public int getSize(Paint paint,FontMetricsInt fm) { if (fm != null && this.fm != null) { fm.ascent = this.fm.ascent; fm.descent = this.fm.descent; fm.top = this.fm.top; fm.bottom = this.fm.bottom; return size; } else { return super.getSize(paint,fm); } }
private String getFileAddedWatermark(String path,String text,String voice) { if (!new File(path).exists()) { return null; } Options options = new Options(); options.inJustDecodeBounds = false; Bitmap bitmap0 = BitmapFactory.decodeFile(path,options); int width = options.outWidth; int height = options.outHeight; Bitmap bitmapPic = Bitmap.createBitmap(width,Config.ARGB_8888); Canvas canvas = new Canvas(bitmapPic); canvas.drawBitmap(bitmap0,null,new Rect(0,height),null); Paint textPaint = new Paint(); textPaint.setColor(-1); textPaint.setTextSize((float) getTextSize()); textPaint.setFlags(2); canvas.drawText(text,text.length(),30.0f,50.0f,textPaint); Rect rect = new Rect(0,height - getTextBgHeight(),height); if (!isOrigPicMode) { Paint voiceBgPaint = new Paint(); voiceBgPaint.setColor(getResources().getColor(2131493164)); voiceBgPaint.setStyle(Style.FILL); voiceBgPaint.setFlags(2); canvas.drawRect(rect,voiceBgPaint); voiceBgPaint.setColor(-1); voiceBgPaint.setTextSize((float) getTextSize()); voiceBgPaint.setTextAlign(Align.CENTER); FontMetricsInt fontMetrics = voiceBgPaint.getFontMetricsInt(); int baseline = (rect.top + ((((rect.bottom - rect.top) - fontMetrics.bottom) + fontMetrics.top) / 2)) - fontMetrics.top; canvas.drawText(voice,(float) rect.centerX(),(float) baseline,voiceBgPaint); } return saveBitmap(bitmapPic); }
private boolean saveFileAddedVoice(String path,String voice,String savePath) { LogInfo.log("fornia","voice:" + voice); if (!new File(path).exists()) { return false; } Options options = new Options(); options.inJustDecodeBounds = false; Bitmap bitmap0 = BitmapFactory.decodeFile(path,null); Rect targetBgRect = new Rect(0,height); if (!isOrigPicMode) { Paint voiceBgPaint = new Paint(); voiceBgPaint.setColor(getResources().getColor(2131493164)); voiceBgPaint.setStyle(Style.FILL); voiceBgPaint.setFlags(2); canvas.drawRect(targetBgRect,voiceBgPaint); voiceBgPaint.setColor(-1); voiceBgPaint.setTextSize((float) getTextSize()); voiceBgPaint.setTextAlign(Align.CENTER); FontMetricsInt fontMetrics = voiceBgPaint.getFontMetricsInt(); String str = voice; canvas.drawText(str,(float) targetBgRect.centerX(),(float) ((targetBgRect.top + ((((targetBgRect.bottom - targetBgRect.top) - fontMetrics.bottom) + fontMetrics.top) / 2)) - fontMetrics.top),voiceBgPaint); } return FileUtils.saveBitmapByUser(this.mContext,bitmapPic); }
@Override public int getSize(@NonNull Paint paint,FontMetricsInt fm) { setupFontMetrics(text,fm,paint); if (fm != null) { final int padding = dims.getPadding(); final int margin = dims.getMarginTop(); fm.ascent = Math.min(fm.top,fm.ascent - padding) - margin; fm.descent = Math.max(fm.bottom,padding); fm.top = fm.ascent; fm.bottom = fm.descent; } return measureWidth(txtPaint,dims.isRtl()); }
private void setupFontMetrics(CharSequence text,FontMetricsInt fm,Paint p) { txtPaint.set(p); final CharacterStyle[] otherSpans = ((Spanned) text).getSpans(start,CharacterStyle.class); for (CharacterStyle otherSpan : otherSpans) { otherSpan.updateDrawState(txtPaint); } txtPaint.setTextSize(p.getTextSize()); if (fm != null) { txtPaint.getFontMetricsInt(fm); } }
/** * @param wp */ private static void expandMetricsFromPaint(FontMetricsInt fmi,TextPaint wp) { final int prevIoUsTop = fmi.top; final int prevIoUsAscent = fmi.ascent; final int prevIoUsDescent = fmi.descent; final int prevIoUsBottom = fmi.bottom; final int prevIoUsLeading = fmi.leading; wp.getFontMetricsInt(fmi); updateMetrics(fmi,prevIoUsTop,prevIoUsAscent,prevIoUsDescent,prevIoUsBottom,prevIoUsLeading); }
android.graphics.Paint.FontMetrics的实例源码
private int fontSizeCompare(float sizeDp,int cols,int rows,int width,int height) { // read new metrics to get exact pixel dimensions defaultPaint.setTextSize((int) (sizeDp * this.displayDensity + 0.5f)); FontMetrics fm = defaultPaint.getFontMetrics(); float[] widths = new float[1]; defaultPaint.getTextWidths("X",widths); int termWidth = (int) widths[0] * cols; int termHeight = (int) Math.ceil(fm.descent - fm.top) * rows; Log.d("fontsize",String.format("font size %fdp resulted in %d x %d",sizeDp,termWidth,termHeight)); // Check to see if it fits in resolution specified. if (termWidth > width || termHeight > height) return 1; if (termWidth == width || termHeight == height) return 0; return -1; }
/** * Constructs the generator and initializes the common members ignoring display density. * * @param iconWidthPx The width of the generated icon in pixels. * @param iconHeightPx The height of the generated icon in pixels. * @param cornerRadiusPx The radius of the corners in the icon in pixels. * @param backgroundColor Color at which the rounded rectangle should be drawn. * @param textSizePx Size at which the text should be drawn in pixels. */ public RoundedIconGenerator(int iconWidthPx,int iconHeightPx,int cornerRadiusPx,int backgroundColor,float textSizePx) { mIconWidthPx = iconWidthPx; mIconHeightPx = iconHeightPx; mCornerRadiusPx = cornerRadiusPx; mBackgroundRect = new RectF(0,mIconWidthPx,mIconHeightPx); mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mBackgroundPaint.setColor(backgroundColor); mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(Color.WHITE); mTextPaint.setFakeBoldText(true); mTextPaint.setTextSize(textSizePx); FontMetrics textFontMetrics = mTextPaint.getFontMetrics(); mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top); mTextYOffset = -textFontMetrics.top; }
private void init(){ mWidth = (int) getPaint().measureText(getText().toString()); FontMetrics fm = getPaint().getFontMetrics(); mFontHeiht = (float)Math.ceil(fm.descent - fm.ascent); int horizontalPadding = getHorizontalPaddint(); int verticalPadding = getVerticalPaddint(); mPath.reset(); mPath.moveto(0,0); mPath.lineto(mWidth + horizontalPadding,mFontHeiht + verticalPadding); mPath.lineto(25,mFontHeiht + verticalPadding); mPath.lineto(0,mFontHeiht+verticalPadding+25); mPath.close(); mPaint.setAntiAlias(true); mPaint.setColor(Color.RED); mPaint.setStyle(Style.FILL); getPaint().setColor(Color.WHITE);// 不知道什么原因,xml设置无效。暂时先在代码中设置。 }
/** * Constructs the generator and initializes the common members ignoring display density. * * @param iconWidthPx The width of the generated icon in pixels. * @param iconHeightPx The height of the generated icon in pixels. * @param cornerRadiusPx The radius of the corners in the icon in pixels. * @param backgroundColor Color at which the rounded rectangle should be drawn. * @param textSizePx Size at which the text should be drawn in pixels. */ public RoundedIconGenerator(int iconWidthPx,mIconHeightPx); mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mBackgroundPaint.setColor(backgroundColor); mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(Color.WHITE); mTextPaint.setFakeBoldText(true); mTextPaint.setTextSize(textSizePx); FontMetrics textFontMetrics = mTextPaint.getFontMetrics(); mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top); mTextYOffset = -textFontMetrics.top; }
public TitleBar(Context context) { super(context); BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(),R.drawable.sys_title_bg_vertical,opts); height = opts.outHeight; setBackgroundResource(R.drawable.sys_title_bg_vertical); paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.WHITE); paint.setTextSize(24); FontMetrics fm = paint.getFontMetrics(); yPostion = (height - fm.descent + fm.ascent) / 2 - fm.ascent; mBusyIndicator = new ProgressBar(getContext()); mBusyIndicator.setIndeterminate(true); //mBusyIndicator.setBackgroundResource(R.drawable.busy); addView(mBusyIndicator); mBusyIndicator.setVisibility(GONE); }
private int fontSizeCompare(float size,int height) { // read new metrics to get exact pixel dimensions defaultPaint.setTextSize(size); FontMetrics fm = defaultPaint.getFontMetrics(); float[] widths = new float[1]; defaultPaint.getTextWidths("X",widths); int termWidth = (int)widths[0] * cols; int termHeight = (int)Math.ceil(fm.descent - fm.top) * rows; Log.d("fontsize",String.format("font size %f resulted in %d x %d",size,termHeight)); // Check to see if it fits in resolution specified. if (termWidth > width || termHeight > height) return 1; if (termWidth == width || termHeight == height) return 0; return -1; }
/** * 描述:绘制文本,支持换行. * * @param canvas the canvas * @param text the text * @param maxWPix the max w pix * @param paint the paint * @param left the left * @param top the top * @return the int */ public static int drawText(Canvas canvas,String text,int maxWPix,TextPaint paint,int left,int top) { if(AbStrUtil.isEmpty(text)){ return 1; } //需要根据文字长度控制换行 //测量文字的长度 List<String> mStrList = getDrawRowStr(text,maxWPix,paint); FontMetrics fm = paint.getFontMetrics(); int hSize = (int)Math.ceil(fm.descent - fm.ascent)+2; for(int i=0;i<mStrList.size();i++){ //计算坐标 int x = left; int y = top+hSize/2+hSize*i; String textLine = mStrList.get(i); canvas.drawText(textLine,x,y,paint); } return mStrList.size(); }
/** * Constructs the generator and initializes the common members ignoring display density. * * @param iconWidthPx The width of the generated icon in pixels. * @param iconHeightPx The height of the generated icon in pixels. * @param cornerRadiusPx The radius of the corners in the icon in pixels. * @param backgroundColor Color at which the rounded rectangle should be drawn. * @param textSizePx Size at which the text should be drawn in pixels. */ public RoundedIconGenerator(int iconWidthPx,mIconHeightPx); mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mBackgroundPaint.setColor(backgroundColor); mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(Color.WHITE); mTextPaint.setFakeBoldText(true); mTextPaint.setTextSize(textSizePx); FontMetrics textFontMetrics = mTextPaint.getFontMetrics(); mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top); mTextYOffset = -textFontMetrics.top; }
private void init(){ mWidth = (int) getPaint().measureText(getText().toString()); FontMetrics fm = getPaint().getFontMetrics(); mFontHeiht = (float)Math.ceil(fm.descent - fm.ascent); int horizontalPadding = getHorizontalPaddint(); int verticalPadding = getVerticalPaddint(); mPath.reset(); mPath.moveto(0,mFontHeiht+verticalPadding+25); mPath.close(); mPaint.setAntiAlias(true); mPaint.setColor(ResFinder.getColor("umeng_comm_topic_tip_bg")); mPaint.setStyle(Style.FILL); getPaint().setColor(Color.WHITE);// 不知道什么原因,xml设置无效。暂时先在代码中设置。 }
/** * Draw text. * * @param canvas the canvas * @param text the text * @param maxWPix the max w pix * @param paint the paint * @return the int */ public int drawText(Canvas canvas,TextPaint paint) { if (TextUtils.isEmpty(text)) { return 1; } // 需要根据文字长度控制换行 // 测量文字的长度 List<String> mStrList = getDrawRowStr(text,paint); FontMetrics fm = paint.getFontMetrics(); int hSize = (int)Math.ceil(fm.descent - fm.ascent); for (int i = 0; i < mStrList.size(); i++) { // 计算坐标 float x = leftPadding; float y = topPadding+hSize/2+i*(hSize+linespacing)+bottomPadding; String textLine = mStrList.get(i); if(i < maxLines){ canvas.drawText(textLine,paint); } } return mStrList.size(); }
private void init(){ mWidth = (int) getPaint().measureText(getText().toString()); FontMetrics fm = getPaint().getFontMetrics(); mFontHeiht = (float)Math.ceil(fm.descent - fm.ascent); int horizontalPadding = getHorizontalPaddint(); int verticalPadding = getVerticalPaddint(); mPath.reset(); mPath.moveto(0,mFontHeiht+verticalPadding+25); mPath.close(); mPaint.setAntiAlias(true); mPaint.setColor(ResFinder.getColor("umeng_comm_topic_tip_bg")); mPaint.setStyle(Style.FILL); getPaint().setColor(Color.WHITE);// 不知道什么原因,xml设置无效。暂时先在代码中设置。 }
protected void drawLabel(Canvas canvas,String labelText,List<RectF> prevLabelsBounds,int centerX,int centerY,float shorTradius,float longRadius,float currentAngle,float angle,int right,int color,Paint paint,boolean line,boolean showMoreLabel) { LabelPoint labelPoint = drawLabelHandler(canvas,labelText,prevLabelsBounds,centerX,centerY,shorTradius,longRadius,currentAngle,angle,left,right,color,paint,line); String[] texts = labelText.split("/"); for (int index = 0; index < texts.length; index++) { canvas.drawText(texts[index],labelPoint.xLabel,labelPoint.yLabel,paint); FontMetrics fm = paint.getFontMetrics(); labelPoint.yLabel = labelPoint.yLabel + Math.round(fm.descent - fm.top) + 2; } if (line) { prevLabelsBounds.add(new RectF(labelPoint.xLabel,labelPoint.xLabel + labelPoint.widthLabel,labelPoint.yLabel + labelPoint.size)); } }
/** * Constructs the generator and initializes the common members ignoring display density. * * @param iconWidthPx The width of the generated icon in pixels. * @param iconHeightPx The height of the generated icon in pixels. * @param cornerRadiusPx The radius of the corners in the icon in pixels. * @param backgroundColor Color at which the rounded rectangle should be drawn. * @param textSizePx Size at which the text should be drawn in pixels. */ public RoundedIconGenerator(int iconWidthPx,mIconHeightPx); mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mBackgroundPaint.setColor(backgroundColor); mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(Color.WHITE); mTextPaint.setFakeBoldText(true); mTextPaint.setTextSize(textSizePx); FontMetrics textFontMetrics = mTextPaint.getFontMetrics(); mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top); mTextYOffset = -textFontMetrics.top; }
/** * @see android.view.ViewGroup#dispatchDraw(Canvas) */ @Override protected void dispatchDraw(Canvas canvas) { if (mState == STATE_REFRESHING) { // // float centerX = mArrowImageView.getMeasuredWidth() / 2; // float centerY = mArrowImageView.getMeasuredHeight() / 2; // Matrix matrix = mArrowImageView.getimageMatrix(); // mArrowImageView.setScaleType(ScaleType.MATRIX); // matrix.postRotate(25,centerY); // mArrowImageView.setimageMatrix(matrix); } super.dispatchDraw(canvas); if (isInEditMode()) { return; } FontMetrics fontMetrics = mPaint.getFontMetrics(); // 计算文字高度 float fontHeight = fontMetrics.bottom - fontMetrics.top; // 计算文字baseline float textBaseY = getHeight() - (getHeight() - fontHeight) / 2 - fontMetrics.bottom; canvas.drawText(mstrTitle,getWidth() / 2 + mArrowImageView.getMeasuredWidth(),textBaseY,mPaint); }
/** * 画左右的Label * * @param canvas */ private void drawLabel(Canvas canvas) { canvas.save(); int vh = getHeight(); FontMetrics fontMetrics = paint.getFontMetrics(); // 计算文字高度 float fontHeight = fontMetrics.bottom - fontMetrics.top; // 计算文字baseline float y = PADDING + vh - (vh - fontHeight) / 2 - fontMetrics.bottom; if (!TextUtils.isEmpty(mRightLabel)) { canvas.drawText(mRightLabel,getWidth() - rightLabelLen / 2 - PADDING,labelPaint); } if (!TextUtils.isEmpty(mLeftLabel)) { canvas.drawText(mLeftLabel,PADDING + leftLabelLen / 2,labelPaint); } canvas.restore(); }
private void initvariable() { mCirclePaint = new Paint(); mCirclePaint.setAntiAlias(true); mCirclePaint.setColor(mCircleColor); mCirclePaint.setStyle(Paint.Style.FILL); mRingPaint = new Paint(); mRingPaint.setAntiAlias(true); mRingPaint.setColor(mRingColor); mRingPaint.setStyle(Paint.Style.stroke); mRingPaint.setstrokeWidth(mstrokeWidth); mTextPaint = new Paint(); mTextPaint.setAntiAlias(true); mTextPaint.setStyle(Paint.Style.FILL); mTextPaint.setARGB(255,255,255); mTextPaint.setTextSize(mRadius / 2); FontMetrics fm = mTextPaint.getFontMetrics(); mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent); }
/** * Draw text. * * @param canvas the canvas * @param text the text * @param maxWPix the max w pix * @param paint the paint * @return the int */ public int drawText(Canvas canvas,paint); } } return mStrList.size(); }
protected void onDraw(Canvas canvas){ Paint paint = new Paint(); paint.setStyle(Style.stroke); paint.setAntiAlias(true); paint.setColor(0xffe9e9e9); paint.setTextSize(Function.getFitPx(getContext(),30)); FontMetrics fm = paint.getFontMetrics(); // canvas.drawRect(drawRect,paint); canvas.drawLine(drawRect.left,drawRect.top,drawRect.right,drawRect.bottom,paint); float h = (maxValue - minValue) / 2; float y = Function.getYCoordinate(drawRect,minValue+h,maxValue,minValue); paint.setColor(0xffbbbbbc); canvas.drawLine(drawRect.left,paint); paint.setTextAlign(Align.RIGHT); int baseline = (int) (drawRect.top + (drawRect.bottom - drawRect.top - fm.bottom + fm.top) / 2 - fm.top); canvas.drawText("0%",drawRect.left-Function.getFitPx(getContext(),20),baseline,paint); paint.setColor(getContext().getResources().getColor(R.color.font_de3031)); canvas.drawText("3.4%",drawRect.top-(fm.top-fm.bottom)/2 + 2,paint); paint.setColor(getContext().getResources().getColor(R.color.font_32a632)); canvas.drawText("-3.4%",paint); }
@Override protected void onDraw(Canvas canvas) { Paint mPaint = getPaint(); //Get font's height & leading: FontMetrics fm = mPaint.getFontMetrics(); float height = fm.descent - fm.ascent + fm.leading; float x = 0; float y = height; String[] textLines = autoSplit(getText().toString(),mPaint,getWidth()); for (String textLine : textLines) { canvas.drawText(textLine,mPaint); y += height; } }
/** * Request a different font size. Will make call to parentChanged() to make * sure we resize PTY if needed. * * @param sizeDp Size of font in dp */ private final void setFontSize(float sizeDp) { if (sizeDp <= 0.0) { return; } final int fontSizePx = (int) (sizeDp * this.displayDensity + 0.5f); defaultPaint.setTextSize(fontSizePx); fontSizeDp = sizeDp; // read new metrics to get exact pixel dimensions FontMetrics fm = defaultPaint.getFontMetrics(); charTop = (int) Math.ceil(fm.top); float[] widths = new float[1]; defaultPaint.getTextWidths("X",widths); charWidth = (int) Math.ceil(widths[0]); charHeight = (int) Math.ceil(fm.descent - fm.top); // refresh any bitmap with new font size if (parent != null) { parentChanged(parent); } for (FontSizeChangedListener ofscl : fontSizeChangedListeners) { ofscl.onFontSizeChanged(sizeDp); } host.setFontSize((int) sizeDp); manager.hostdb.saveHost(host); forcedSize = false; }
private boolean initOverlayParams(Matrix matrix) { if (overlayParams == null || pageBounds == null) { return false; } // Overlay params prevIoUsly initiated; skip if (overlayParams.init) { return true; } int overlayColor = overlayTextColor; if (isNightMode) { overlayColor = Color.rgb(nightModeTextBrightness,nightModeTextBrightness,nightModeTextBrightness); } overlayParams.paint.setColor(overlayColor); // Use font metrics to calculate the maximum possible height of the text FontMetrics fm = overlayParams.paint.getFontMetrics(); final RectF mappedRect = new RectF(); matrix.mapRect(mappedRect,pageBounds); // Calculate where the text's baseline should be // (for top text and bottom text) // (p.s. parts of the glyphs will be below the baseline such as a // 'y' or 'ي') overlayParams.topBaseline = -fm.top; overlayParams.bottomBaseline = getHeight() - fm.bottom; // Calculate the horizontal margins off the edge of screen overlayParams.offsetX = Math.min( mappedRect.left,getWidth() - mappedRect.right); overlayParams.init = true; return true; }
/** * 获取字体的高度 * @param fontSize * @return */ private int getFontHeight(float fontSize){ Paint paint = new Paint(); paint.setTextSize(fontSize); FontMetrics fm = paint.getFontMetrics(); return (int) Math.ceil(fm.descent - fm.top) + 2; }
/** * @param context The current Android's context. * @param incognito Whether the title are for incognito mode. * @param nullFaviconResourceId A drawable resource id of a default favicon. */ public TitleBitmapFactory(Context context,boolean incognito,int nullFaviconResourceId) { mNullFaviconResourceId = nullFaviconResourceId; Resources res = context.getResources(); int textColor = ApiCompatibilityUtils.getColor(res,incognito ? R.color.compositor_tab_title_bar_text_incognito : R.color.compositor_tab_title_bar_text); float textSize = res.getDimension(R.dimen.compositor_tab_title_text_size); boolean fakeBoldText = res.getBoolean(R.bool.compositor_tab_title_fake_bold_text); mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(textColor); mTextPaint.setTextSize(textSize); mTextPaint.setFakeBoldText(fakeBoldText); mTextPaint.density = res.getdisplayMetrics().density; FontMetrics textFontMetrics = mTextPaint.getFontMetrics(); mTextHeight = (float) Math.ceil(textFontMetrics.bottom - textFontMetrics.top); mTextYOffset = -textFontMetrics.top; mFaviconDimension = res.getDimensionPixelSize(R.dimen.compositor_tab_title_favicon_size); mViewHeight = (int) Math.max(mFaviconDimension,mTextHeight); int width = res.getdisplayMetrics().widthPixels; int height = res.getdisplayMetrics().heightPixels; mMaxWidth = (int) (TITLE_WIDTH_PERCENTAGE * Math.max(width,height)); // Set the favicon dimension here. mFaviconDimension = Math.min(mMaxWidth,mFaviconDimension); }
@Override public View onCreateView(ViewGroup parent) { View view = super.onCreateView(parent); if (mNoBottomSpacing) { ApiCompatibilityUtils.setPaddingrelative( view,ApiCompatibilityUtils.getPaddingStart(view),view.getPaddingTop(),ApiCompatibilityUtils.getPaddingEnd(view),0); } ((TextView) view.findViewById(android.R.id.summary)).setMovementMethod( LinkMovementMethod.getInstance()); // The icon is aligned to the top of the text view,which can be higher than the // ascender line of the text,and makes it look aligned improperly. TextView textView = (TextView) view.findViewById( getTitle() != null ? android.R.id.title : android.R.id.summary); FontMetrics metrics = textView.getPaint().getFontMetrics(); ImageView icon = (ImageView) view.findViewById(android.R.id.icon); ApiCompatibilityUtils.setPaddingrelative( icon,(int) java.lang.Math.ceil(metrics.ascent - metrics.top),0); return view; }
protected Float getCacheHeight(BaseDanmaku danmaku,Paint paint) { Float textSize = Float.valueOf(paint.getTextSize()); Float textHeight = (Float) sTextHeightCache.get(textSize); if (textHeight != null) { return textHeight; } FontMetrics fontMetrics = paint.getFontMetrics(); textHeight = Float.valueOf((fontMetrics.descent - fontMetrics.ascent) + fontMetrics.leading); sTextHeightCache.put(textSize,textHeight); return textHeight; }
/** * text height * * @param fontSize fontsize * @return fontsize `s height */ public int getFontHeight(float fontSize) { Paint paint = new Paint(); paint.setTextSize(fontSize); FontMetrics fm = paint.getFontMetrics(); return (int)Math.ceil(fm.descent - fm.ascent); }
/** * 返回字体高度 * * @param textSize * @return */ public static int getFontHeight(float textSize) { Paint paint = new Paint(); paint.setTextSize(textSize); FontMetrics fm = paint.getFontMetrics(); return (int) Math.ceil(fm.descent - fm.top) + 2; }
/** * 返回字体高度 * * @param textSize * @return */ public int getFontHeight(float textSize) { Paint paint = new Paint(); paint.setTextSize(textSize); FontMetrics fm = paint.getFontMetrics(); return (int) Math.ceil(fm.descent - fm.top) + 2; }
/** * get Font height */ public static int getFontHeight(TextView view) { Paint paint = new Paint(); paint.setTextSize(view.getTextSize()); FontMetrics fm = paint.getFontMetrics(); return (int) (Math.ceil(fm.descent - fm.ascent)); }
private void updateIconSize(float scale,int drawablePadding,Resources res,displayMetrics dm) { iconSizePx = (int) (Utilities.pxFromDp(inv.iconSize,dm) * scale); iconTextSizePx = (int) (Utilities.pxFromSp(inv.iconTextSize,dm) * scale); iconDrawablePaddingPx = drawablePadding; hotseatIconSizePx = (int) (Utilities.pxFromDp(inv.hotseatIconSize,dm) * scale); // Search Bar searchBarSpaceWidthPx = Math.min(widthPx,res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_max_width)); searchBarSpaceHeightPx = getSearchBarTopOffset() + res.getDimensionPixelSize(R.dimen.dynamic_grid_search_bar_height); // Calculate the actual text height Paint textPaint = new Paint(); textPaint.setTextSize(iconTextSizePx); FontMetrics fm = textPaint.getFontMetrics(); cellWidthPx = iconSizePx; cellHeightPx = iconSizePx + iconDrawablePaddingPx + (int) Math.ceil(fm.bottom - fm.top); final float scaleDps = res.getDimensionPixelSize(R.dimen.dragViewScale); dragViewScale = (iconSizePx + scaleDps) / iconSizePx; // Hotseat hotseatBarHeightPx = iconSizePx + 4 * edgeMarginPx; hotseatCellWidthPx = iconSizePx; hotseatCellHeightPx = iconSizePx; // Folder folderCellWidthPx = cellWidthPx + 3 * edgeMarginPx; folderCellHeightPx = cellHeightPx + edgeMarginPx; folderBackgroundOffset = -edgeMarginPx; folderIconSizePx = iconSizePx + 2 * -folderBackgroundOffset; }
/** * @param context The current Android's context. * @param incognito Whether the title are for incognito mode. * @param nullFaviconResourceId A drawable resource id of a default favicon. */ public TitleBitmapFactory(Context context,mFaviconDimension); }
@Override public View onCreateView(ViewGroup parent) { View view = super.onCreateView(parent); if (mNoBottomSpacing) { ApiCompatibilityUtils.setPaddingrelative( view,0); return view; }
/** * 得到字符串信息包括行数,页数等信息 */ public void GetTextIfon() { char ch; int w = 0; int istart = 0; FontMetrics fm = mPaint.getFontMetrics();// 得到系统默认字体属性 mFontHeight = (int) (Math.ceil(fm.descent - fm.top) + 2);// 获得字体高度 mPageLineNum = mTextHeight / mFontHeight;// 获得行数 int count = this.mStrText.length(); for (int i = 0; i < count; i++) { ch = this.mStrText.charat(i); float[] widths = new float[1]; String str = String.valueOf(ch); mPaint.getTextWidths(str,widths); if (ch == '\n') {//原为\n mRealLine++;// 真实的行数加一 mString.addElement(this.mStrText.substring(istart,i)); istart = i + 1; w = 0; } else { w += (int) Math.ceil(widths[0]); if (w > this.mTextWidth) { mRealLine++;// 真实的行数加一 mString.addElement(this.mStrText.substring(istart,i)); istart = i; i--; w = 0; } else { if (i == count - 1) { mRealLine++;// 真实的行数加一 mString.addElement(this.mStrText.substring(istart,count)); } } } } }
private void CalculateText() { paint.setTextSize(mFontSize); //获得字宽 if (mlinewidth == 0) { paint.getTextWidths("正",widths);//获取单个汉字的宽度 paint.getTextWidths(" ",space); // mlinewidth = (int) Math.ceil((widths[0] + space[0]) * 1.1 + 2); mlinewidth = (int) Math.ceil((widths[0] + space[0]) * 1.1 + 2) //获得字体宽度 + displayUtil.dp2px(getContext(),2); //增加间距 } FontMetrics fm = paint.getFontMetrics(); // mFontHeight = (int) (Math.ceil(fm.descent - fm.top) * 0.9);// 获得字体高度 mFontHeight = (int) (Math.ceil(fm.descent - fm.ascent)) // 获得字体高度 + displayUtil.dp2px(getContext(),2); //增加间距 //计算文字行数 mRealLine = 0; for (int i = 0; i < this.TextLength; i++) { ch = this.text.charat(i); if (ch == '\n') { mRealLine++;// 真实的行数加一 h = 0; } else { h += mFontHeight; if (h > this.mTextHeight) { mRealLine++;// 真实的行数加一 i--; h = 0; } else { if (i == this.TextLength - 1) { mRealLine++;// 真实的行数加一 } } } } mRealLine += 1;//额外增加一行 mTextWidth = mlinewidth * mRealLine;//计算文字总宽度 measure(mTextWidth,getHeight());//重新调整大小 layout(getLeft(),getTop(),getLeft() + mTextWidth,getBottom());//重新绘制容器 }
private void initView() { mPaint = new TextPaint(); mPaint.setTextSize(mTextSize); mPaint.setTextAlign(Align.CENTER); mPaint.setAntiAlias(true); // 计算字体高度 FontMetrics fm = mPaint.getFontMetrics(); mTextHeight = (int) (fm.bottom - fm.top); // 滚动动画 mLineAnimator = new ValueAnimator(); mLineAnimator.setIntValues(0,100); mLineAnimator.setDuration(mLineAnimDuration); mLineAnimator.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int value = (Integer) animation.getAnimatedValue(); float percent = 1 - (float) value / 100; mLineOffset = (int) (mAnimOffset * percent); // 更新偏移值,重绘View invalidate(); } }); }
private void drawFirstVisibleColumn(Canvas canvas,float zoom,Paint paint) { FontMetrics fm = paint.getFontMetrics(); float visibleColumnWidth = 0; SheetScroller minRowAndColumninformation = sheetview.getMinRowAndColumninformation(); //draw rest part of first column float columnWidht = (minRowAndColumninformation.getColumnWidth() * zoom); visibleColumnWidth = (float)(minRowAndColumninformation.getVisibleColumnWidth() * zoom); // 绘制header if(HeaderUtil.instance().isActiveColumn(sheetview.getCurrentSheet(),minRowAndColumninformation.getMinColumnIndex())) { paint.setColor(SSConstant.ACTIVE_COLOR); } else { paint.setColor(SSConstant.HEADER_FILL_COLOR); } rect.set((int)x,(int)(x + visibleColumnWidth),columnHeaderHeight); canvas.drawRect(rect,paint); //header line paint.setColor(SSConstant.HEADER_GRIDLINE_COLOR); canvas.drawRect(x,x + 1,columnHeaderHeight,paint); // 绘制文本 canvas.save(); canvas.clipRect(rect); paint.setColor(SSConstant.HEADER_TEXT_COLOR); String rowText = HeaderUtil.instance().getColumnHeaderTextByIndex(minRowAndColumninformation.getMinColumnIndex()); float textWidth = paint.measureText(rowText); float textx = (columnWidht - textWidth) / 2; float textY = (int)(columnHeaderHeight - Math.ceil(fm.descent - fm.ascent)) / 2; canvas.drawText(rowText,x + textx - (columnWidht - visibleColumnWidth),textY - fm.ascent,paint); canvas.restore(); }
/** * single legend width and height * @param renderer * @param titles * @param paint * @param chartWidth * @return */ public Rectangle getSingleAutoLegendSize(DefaultRenderer renderer,String[] titles,int legendWidth) { float width = -1f; float height = -1f; paint.setTextSize(renderer.getLegendTextSize() * renderer.getZoomrate()); int seriesCnt = Math.min(titles.length,renderer.getSeriesRendererCount()); for (int i = 0; i < seriesCnt; i++) { String text = titles[i].replace("\n"," "); //the rest cell FontMetrics fm = paint.getFontMetrics(); // 文本高度 height = Math.max((float)(Math.ceil(fm.descent - fm.ascent)),height); // 文本宽度 width = Math.max((float)(paint.measureText(text)),width); } float maxLegendTextWidth = legendWidth - getLegendShapeWidth(0) * renderer.getZoomrate() * 2; if(width > maxLegendTextWidth) { //the max width legend shape layout more than one lines int lines = (int)Math.ceil(width / maxLegendTextWidth); return new Rectangle(legendWidth,(int)Math.ceil(height) * lines); } else { return new Rectangle((int)Math.ceil(width + getLegendShapeWidth(0) * renderer.getZoomrate() * 2),(int)Math.ceil(height)); } }
@Override public void draw(Canvas canvas) { if (first) { invalidateXY(); if (!mEditText.isFocused() && mEditText.getText().length() == 0) down(); first = false; } FontMetrics fm = mTextPaint.getFontMetrics(); float baseline = 0 - fm.top; float mTxtHeight = fm.bottom - fm.top; float mTxtWidth = mTextPaint.measureText(hintText); mRectF.set(xOffset,yOffset - baseline,xOffset + mTxtWidth,yOffset - baseline + mTxtHeight); canvas.save(); canvas.clipRect(mRectF,Region.Op.DIFFERENCE); mShape.resize(canvas.getWidth(),canvas.getHeight() - mPaddingTop); canvas.translate(0,mPaddingTop); mShape.draw(canvas,mPaint); canvas.restore(); mTextPaint.setTextSize(mEditText.getTextSize()); mTextPaint.setColor(mColor); canvas.drawText(hintText,xOffset,yOffset,mTextPaint); }
/** * Request a different font size. Will make call to parentChanged() to make * sure we resize PTY if needed. */ /* package */ final void setFontSize(float size) { if (size <= 0.0) return; defaultPaint.setTextSize(size); fontSize = size; // read new metrics to get exact pixel dimensions FontMetrics fm = defaultPaint.getFontMetrics(); charTop = (int)Math.ceil(fm.top); float[] widths = new float[1]; defaultPaint.getTextWidths("X",widths); charWidth = (int)Math.ceil(widths[0]); charHeight = (int)Math.ceil(fm.descent - fm.top); // refresh any bitmap with new font size if(parent != null) parentChanged(parent); for (FontSizeChangedListener ofscl : fontSizeChangedListeners) ofscl.onFontSizeChanged(size); host.setFontSize((int) fontSize); manager.hostdb.updateFontSize(host); forcedSize = false; }
/** * 描述:Todo. * * @param canvas the canvas * @see android.view.View#onDraw(android.graphics.Canvas) * @author: amsoft.cn * @date:2013-6-17 上午9:04:49 * @version v1.0 */ @Override protected void onDraw(Canvas canvas) { //if(D)Log4jLog.d(LONG_TAG,"--AbMonitorView onDraw--"); canvas.drawColor(Color.argb(80,0)); if (mStartTime == -1) { mStartTime = SystemClock.elapsedRealtime(); mCounter = 0; } long Now = SystemClock.elapsedRealtime(); long delay = Now - mStartTime; if(delay!=0){ // 计算帧速率 mfps = (int)(mCounter * 1000 / delay); } String text = mfps + " fps"; //获取值的文本的高度 TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setTypeface(Typeface.DEFAULT); mTextPaint.setTextSize(16); FontMetrics fm = mTextPaint.getFontMetrics(); //得到行高 int textHeight = (int)Math.ceil(fm.descent - fm.ascent)+2; int textWidth = (int)AbGraphical.getStringWidth(text,mTextPaint); canvas.drawText(text,(this.getWidth()-textWidth)/2,textHeight,mPaint); if (delay > 1000L) { mStartTime = Now; mfps = mCounter; mCounter = 0; } mCounter++; super.onDraw(canvas); }
关于Java-没有图形的FontMetrics和java没有图标的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于.NoClassDefFoundError: com/yammer/metrics/Metrics、android fontMetrics 使用说明、android.graphics.Paint.FontMetricsInt的实例源码、android.graphics.Paint.FontMetrics的实例源码的相关信息,请在本站寻找。
本文标签: