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, i, 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, 0, 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), x, baseline, mPaint); } // 绘制上方data for (int i = 1; (mCurrentSelected - i) >= 0; i++) { drawOtherText(canvas, i, -1); } // 绘制下方data for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) { drawOtherText(canvas, i, 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, x, baseline, mPaint); } // 绘制上方data for (int i = 1; (mCurrentSelected - i) >= 0; i++) { drawOtherText(canvas, i, -1); } // 绘制下方data for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) { drawOtherText(canvas, i, 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), baseline, 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) (this.mHeight / 2), (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) (this.mHeight / 2), (float) ((textWidth / 2) + stopX), (float) (this.mHeight / 2), 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); } }
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), x, baseline, mPaint); // 绘制上方data for (int i = 1; (mCurrentSelected - i) >= 0; i++) { drawOtherText(canvas, i, -1); } // 绘制下方data for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) { drawOtherText(canvas, i, 1); } }
/** * 代码跟父类代码相似,就是getCachedDrawable()替换成getDrawable(),因为前者里面的图片是WeakReference, * 容易被gc回收,所以这里要避免这个问题 */ @Override public int getSize(Paint paint, CharSequence text, int start, int end, 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 / 3.2f, 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), x, baseline, mPaint); // 绘制上方data for (int i = 1; (mCurrentSelected - i) >= 0; i++) { drawOtherText(canvas, i, -1); } // 绘制下方data for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) { drawOtherText(canvas, i, 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(), x, baseline, mPaint); // 绘制上方data for (int i = 1; (mCurrentSelected - i) >= 0; i++) { drawOtherText(canvas, i, -1); } // 绘制下方data for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++) { drawOtherText(canvas, i, 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), x, baseline, mPaint); //暴力法则,上下都最多绘制10条数据 int count = Math.min(10, mDataList.size()); // 绘制上方data for (int i = 1; i <= count; i++) { drawOtherText(canvas, i, -1); } // 绘制下方data for (int i = 1; i <= count; i++) { drawOtherText(canvas, i, 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)); int valuePosition = mCurrentSelected + type * position; //调整位置偏差在 合法范围 int dataSize = mDataList.size(); valuePosition = (valuePosition + dataSize) % dataSize; //if(valuePosition >= 0 & valuePosition < dataSize) {//以防万一 canvas.drawText(mDataList.get(valuePosition), (float) (mViewWidth / 2.0), baseline, 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, 0, 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 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, 0, line.length())); if (temp > maxContentWidth) { maxContentWidth = temp; } } } return new TextProperty(maxContentWidth, h, 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, CharSequence text, int start, int end, FontMetricsInt fm) { fm = fm == null ? paint.getFontMetricsInt() : fm; int iconSize = fm.bottom - fm.top; mDrawable.setBounds(0, 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 position, 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), (float) (mViewWidth / 2.0), baseline, nPaint); }
@Override public int getSize(Paint paint, CharSequence text, int start, int end, 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, text, start, end, fm); } }
/** * @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)); canvas.drawText(mDataList.get(mCurrentSelected + type * position), (float) (mViewWidth / 2.0), baseline, mPaint); }
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, height, Config.ARGB_8888); Canvas canvas = new Canvas(bitmapPic); canvas.drawBitmap(bitmap0, null, new Rect(0, 0, width, height), null); Paint textPaint = new Paint(); textPaint.setColor(-1); textPaint.setTextSize((float) getTextSize()); textPaint.setFlags(2); canvas.drawText(text, 0, text.length(), 30.0f, 50.0f, textPaint); Rect rect = new Rect(0, height - getTextBgHeight(), width, 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, options); int width = options.outWidth; int height = options.outHeight; Bitmap bitmapPic = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(bitmapPic); canvas.drawBitmap(bitmap0, null, new Rect(0, 0, width, height), null); Rect targetBgRect = new Rect(0, height - getTextBgHeight(), width, 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, CharSequence text, int start, int end, FontMetricsInt fm) { setupFontMetrics(text, start, end, 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, text, start, end, dims.isRtl()); }
private void setupFontMetrics(CharSequence text, int start, int end, FontMetricsInt fm, Paint p) { txtPaint.set(p); final CharacterStyle[] otherSpans = ((Spanned) text).getSpans(start, end, 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); }