android.text.StaticLayout#getLineTop ( )源码实例Demo

下面列出了android.text.StaticLayout#getLineTop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Telegram-FOSS   文件: TextSelectionHelper.java
@Override
protected int getLineHeight() {
    if (selectedView != null && selectedView.getMessageObject() != null) {
        MessageObject object = selectedView.getMessageObject();
        StaticLayout layout = null;
        if (isDescription) {
            layout = selectedView.getDescriptionlayout();
        } else if (selectedView.hasCaptionLayout()) {
            layout = selectedView.getCaptionLayout();
        } else if (object.textLayoutBlocks != null) {
            layout = object.textLayoutBlocks.get(0).textLayout;
        }
        if (layout == null) {
            return 0;
        }
        int lineHeight = layout.getLineBottom(0) - layout.getLineTop(0);
        return lineHeight;
    }
    return 0;
}
 
源代码2 项目: Telegram-FOSS   文件: TextSelectionHelper.java
@Override
protected int getLineHeight() {
    if (selectedView == null) {
        return 0;
    } else {
        arrayList.clear();
        selectedView.fillTextLayoutBlocks(arrayList);
        int index = startPeek ? startViewChildPosition : endViewChildPosition;
        if (index < 0 || index >= arrayList.size()) {
            return 0;
        }
        StaticLayout layout = arrayList.get(index).getLayout();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < layout.getLineCount(); i++) {
            int h = layout.getLineBottom(i) - layout.getLineTop(i);
            if (h < min) min = h;
        }
        return min;
    }
}
 
源代码3 项目: Telegram   文件: TextSelectionHelper.java
@Override
protected int getLineHeight() {
    if (selectedView != null && selectedView.getMessageObject() != null) {
        MessageObject object = selectedView.getMessageObject();
        StaticLayout layout = null;
        if (isDescription) {
            layout = selectedView.getDescriptionlayout();
        } else if (selectedView.hasCaptionLayout()) {
            layout = selectedView.getCaptionLayout();
        } else if (object.textLayoutBlocks != null) {
            layout = object.textLayoutBlocks.get(0).textLayout;
        }
        if (layout == null) {
            return 0;
        }
        int lineHeight = layout.getLineBottom(0) - layout.getLineTop(0);
        return lineHeight;
    }
    return 0;
}
 
源代码4 项目: Telegram   文件: TextSelectionHelper.java
@Override
protected int getLineHeight() {
    if (selectedView == null) {
        return 0;
    } else {
        arrayList.clear();
        selectedView.fillTextLayoutBlocks(arrayList);
        int index = startPeek ? startViewChildPosition : endViewChildPosition;
        if (index < 0 || index >= arrayList.size()) {
            return 0;
        }
        StaticLayout layout = arrayList.get(index).getLayout();
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < layout.getLineCount(); i++) {
            int h = layout.getLineBottom(i) - layout.getLineTop(i);
            if (h < min) min = h;
        }
        return min;
    }
}
 
源代码5 项目: zone-sdk   文件: TextViewLinkActivity.java
private void roateSpan() {
        TextView tv_rotate_img_span = (TextView) findViewById(R.id.tv_rotate_img_span);
        ImageView img_rotate = (ImageView) findViewById(R.id.img_rotate);
        Drawable drawable = getResources().getDrawable(R.drawable.icon);
//        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.setBounds(0, 0, 200, 200);
        img_rotate.setImageDrawable(drawable);

        ViewGroup.LayoutParams lp = img_rotate.getLayoutParams();
        lp.width = 200;
        lp.height = 200;
        img_rotate.setLayoutParams(lp);

        //这个可以在oncreate的时候测量出来
        StaticLayout sl = new StaticLayout("aa", tv_rotate_img_span.getPaint(),
                100, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
        int lineHeight = sl.getLineBottom(0) - sl.getLineTop(0);// 比 tv_rotate_img_span.getLineHeight() 这个好 因为每行高刚度可能不一致

        int affectLine = 0;
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            if (i * lineHeight > 200 + 20) {
                affectLine = i - 1;
                break;
            }
        }

        SpannableString msp = new SpannableString("可是现在我已不是在天上翱翔的小鸟,在地上奔跑的动物了,因为我长大了,已经五年级了,大家都在努力地学习,谁都不想输给别人,每个人都在奋发图强,“学习如逆水行舟,不进则退”。这句话在我耳边回荡,难道我们就不应该有一个孩子应该有的童真吗?难道我已经失去了吗?难道现在我们所有的孩子都应该抓住学习这根攀山绳吗?为什么我们不?");
        tv_rotate_img_span.setText(msp);
        tv_rotate_img_span.setGravity(Gravity.LEFT);

        msp.setSpan(new ImageRotate(affectLine), 0, msp.length() - 1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tv_rotate_img_span.setText(msp);
    }
 
源代码6 项目: Telegram-FOSS   文件: TextSelectionHelper.java
private void drawLine(Canvas canvas, StaticLayout layout, int line, int start, int end) {
    layout.getSelectionPath(start, end, path);
    if (path.lastBottom < layout.getLineBottom(line)) {
        int lineBottom = layout.getLineBottom(line);
        int lineTop = layout.getLineTop(line);
        float lineH = lineBottom - lineTop;
        float lineHWithoutSpaсing = path.lastBottom - lineTop;
        canvas.save();
        canvas.scale(1f, lineH / lineHWithoutSpaсing, 0, lineTop);
        canvas.drawPath(path, selectionPaint);
        canvas.restore();
    } else {
        canvas.drawPath(path, selectionPaint);
    }
}
 
源代码7 项目: Telegram-FOSS   文件: LinkPath.java
public void setCurrentLayout(StaticLayout layout, int start, float yOffset) {
    currentLayout = layout;
    currentLine = layout.getLineForOffset(start);
    lastTop = -1;
    heightOffset = yOffset;
    if (Build.VERSION.SDK_INT >= 28) {
        int lineCount = layout.getLineCount();
        if (lineCount > 0) {
            lineHeight = layout.getLineBottom(lineCount - 1) - layout.getLineTop(lineCount - 1);
        }
    }
}
 
源代码8 项目: Telegram   文件: TextSelectionHelper.java
private void drawLine(Canvas canvas, StaticLayout layout, int line, int start, int end) {
    layout.getSelectionPath(start, end, path);
    if (path.lastBottom < layout.getLineBottom(line)) {
        int lineBottom = layout.getLineBottom(line);
        int lineTop = layout.getLineTop(line);
        float lineH = lineBottom - lineTop;
        float lineHWithoutSpaсing = path.lastBottom - lineTop;
        canvas.save();
        canvas.scale(1f, lineH / lineHWithoutSpaсing, 0, lineTop);
        canvas.drawPath(path, selectionPaint);
        canvas.restore();
    } else {
        canvas.drawPath(path, selectionPaint);
    }
}
 
源代码9 项目: Telegram   文件: LinkPath.java
public void setCurrentLayout(StaticLayout layout, int start, float yOffset) {
    currentLayout = layout;
    currentLine = layout.getLineForOffset(start);
    lastTop = -1;
    heightOffset = yOffset;
    if (Build.VERSION.SDK_INT >= 28) {
        int lineCount = layout.getLineCount();
        if (lineCount > 0) {
            lineHeight = layout.getLineBottom(lineCount - 1) - layout.getLineTop(lineCount - 1);
        }
    }
}
 
源代码10 项目: no-player   文件: SubtitlePainter.java
@SuppressWarnings("PMD.NPathComplexity")  // TODO break this method up
private void drawTextLayout(Canvas canvas) {
    StaticLayout layout = textLayout;
    if (layout == null) {
        // Nothing to draw.
        return;
    }

    int saveCount = canvas.save();
    canvas.translate(textLeft, textTop);

    if (Color.alpha(windowColor) > 0) {
        paint.setColor(windowColor);
        canvas.drawRect(-textPaddingX, 0, layout.getWidth() + textPaddingX, layout.getHeight(),
                paint);
    }

    if (Color.alpha(backgroundColor) > 0) {
        paint.setColor(backgroundColor);
        float previousBottom = layout.getLineTop(0);
        int lineCount = layout.getLineCount();
        for (int i = 0; i < lineCount; i++) {
            lineBounds.left = layout.getLineLeft(i) - textPaddingX;
            lineBounds.right = layout.getLineRight(i) + textPaddingX;
            lineBounds.top = previousBottom;
            lineBounds.bottom = layout.getLineBottom(i);
            previousBottom = lineBounds.bottom;
            canvas.drawRoundRect(lineBounds, cornerRadius, cornerRadius, paint);
        }
    }

    if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
        textPaint.setStrokeJoin(Join.ROUND);
        textPaint.setStrokeWidth(outlineWidth);
        textPaint.setColor(edgeColor);
        textPaint.setStyle(Style.FILL_AND_STROKE);
        layout.draw(canvas);
    } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
        textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
    } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
            || edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
        boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
        int colorUp = raised ? Color.WHITE : edgeColor;
        int colorDown = raised ? edgeColor : Color.WHITE;
        float offset = shadowRadius / 2;
        textPaint.setColor(foregroundColor);
        textPaint.setStyle(Style.FILL);
        textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
        layout.draw(canvas);
        textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
    }

    textPaint.setColor(foregroundColor);
    textPaint.setStyle(Style.FILL);
    layout.draw(canvas);
    textPaint.setShadowLayer(0, 0, 0, 0);

    canvas.restoreToCount(saveCount);
}
 
源代码11 项目: K-Sonic   文件: SubtitlePainter.java
private void drawTextLayout(Canvas canvas) {
  StaticLayout layout = textLayout;
  if (layout == null) {
    // Nothing to draw.
    return;
  }

  int saveCount = canvas.save();
  canvas.translate(textLeft, textTop);

  if (Color.alpha(windowColor) > 0) {
    paint.setColor(windowColor);
    canvas.drawRect(-textPaddingX, 0, layout.getWidth() + textPaddingX, layout.getHeight(),
        paint);
  }

  if (Color.alpha(backgroundColor) > 0) {
    paint.setColor(backgroundColor);
    float previousBottom = layout.getLineTop(0);
    int lineCount = layout.getLineCount();
    for (int i = 0; i < lineCount; i++) {
      lineBounds.left = layout.getLineLeft(i) - textPaddingX;
      lineBounds.right = layout.getLineRight(i) + textPaddingX;
      lineBounds.top = previousBottom;
      lineBounds.bottom = layout.getLineBottom(i);
      previousBottom = lineBounds.bottom;
      canvas.drawRoundRect(lineBounds, cornerRadius, cornerRadius, paint);
    }
  }

  if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
    textPaint.setStrokeJoin(Join.ROUND);
    textPaint.setStrokeWidth(outlineWidth);
    textPaint.setColor(edgeColor);
    textPaint.setStyle(Style.FILL_AND_STROKE);
    layout.draw(canvas);
  } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
    textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
  } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
      || edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
    boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
    int colorUp = raised ? Color.WHITE : edgeColor;
    int colorDown = raised ? edgeColor : Color.WHITE;
    float offset = shadowRadius / 2f;
    textPaint.setColor(foregroundColor);
    textPaint.setStyle(Style.FILL);
    textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
    layout.draw(canvas);
    textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
  }

  textPaint.setColor(foregroundColor);
  textPaint.setStyle(Style.FILL);
  layout.draw(canvas);
  textPaint.setShadowLayer(0, 0, 0, 0);

  canvas.restoreToCount(saveCount);
}
 
源代码12 项目: MixtureTextView   文件: MixtureTextView.java
private void cacuLineHeight()
{
    layout = new StaticLayout("爱我中华", mTextPaint, 0, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0f, false);
    mLineHeight = layout.getLineBottom(0) - layout.getLineTop(0);
}
 
源代码13 项目: Exoplayer_VLC   文件: SubtitleView.java
@Override
protected void onDraw(Canvas c) {
  final StaticLayout layout = this.layout;
  if (layout == null) {
    return;
  }

  final int saveCount = c.save();
  final int innerPaddingX = this.innerPaddingX;
  c.translate(getPaddingLeft() + innerPaddingX, getPaddingTop());

  final int lineCount = layout.getLineCount();
  final Paint textPaint = this.textPaint;
  final Paint paint = this.paint;
  final RectF bounds = lineBounds;

  if (Color.alpha(backgroundColor) > 0) {
    final float cornerRadius = this.cornerRadius;
    float previousBottom = layout.getLineTop(0);

    paint.setColor(backgroundColor);
    paint.setStyle(Style.FILL);

    for (int i = 0; i < lineCount; i++) {
      bounds.left = layout.getLineLeft(i) - innerPaddingX;
      bounds.right = layout.getLineRight(i) + innerPaddingX;
      bounds.top = previousBottom;
      bounds.bottom = layout.getLineBottom(i);
      previousBottom = bounds.bottom;

      c.drawRoundRect(bounds, cornerRadius, cornerRadius, paint);
    }
  }

  if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
    textPaint.setStrokeJoin(Join.ROUND);
    textPaint.setStrokeWidth(outlineWidth);
    textPaint.setColor(edgeColor);
    textPaint.setStyle(Style.FILL_AND_STROKE);
    layout.draw(c);
  } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
    textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
  } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
      || edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
    boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
    int colorUp = raised ? Color.WHITE : edgeColor;
    int colorDown = raised ? edgeColor : Color.WHITE;
    float offset = shadowRadius / 2f;
    textPaint.setColor(foregroundColor);
    textPaint.setStyle(Style.FILL);
    textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
    layout.draw(c);
    textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
  }

  textPaint.setColor(foregroundColor);
  textPaint.setStyle(Style.FILL);
  layout.draw(c);
  textPaint.setShadowLayer(0, 0, 0, 0);
  c.restoreToCount(saveCount);
}
 
源代码14 项目: Telegram-FOSS   文件: TextSelectionHelper.java
@Override
protected int getCharOffsetFromCord(int x, int y, int offsetX, int offsetY, ArticleSelectableView view, boolean maybe) {
    if (view == null) {
        return -1;
    }

    int line = -1;
    x -= offsetX;
    y -= offsetY;

    arrayList.clear();
    view.fillTextLayoutBlocks(arrayList);

    int childIndex;
    if (maybe) {
        childIndex = maybeTextIndex;
    } else {
        childIndex = startPeek ? startViewChildPosition : endViewChildPosition;
    }
    StaticLayout layout = arrayList.get(childIndex).getLayout();
    if (x < 0) {
        x = 1;
    }
    if (y < 0) {
        y = 1;
    }
    if (x > layout.getWidth()) {
        x = layout.getWidth();
    }
    if (y > layout.getLineBottom(layout.getLineCount() - 1)) {
        y = (int) (layout.getLineBottom(layout.getLineCount() - 1) - 1);
    }

    for (int i = 0; i < layout.getLineCount(); i++) {
        if (y > layout.getLineTop(i) && y < layout.getLineBottom(i)) {
            line = i;
            break;
        }
    }
    if (line >= 0) {
        return layout.getOffsetForHorizontal(line, x);
    }

    return -1;
}
 
源代码15 项目: Telegram   文件: TextSelectionHelper.java
@Override
protected int getCharOffsetFromCord(int x, int y, int offsetX, int offsetY, ArticleSelectableView view, boolean maybe) {
    if (view == null) {
        return -1;
    }

    int line = -1;
    x -= offsetX;
    y -= offsetY;

    arrayList.clear();
    view.fillTextLayoutBlocks(arrayList);

    int childIndex;
    if (maybe) {
        childIndex = maybeTextIndex;
    } else {
        childIndex = startPeek ? startViewChildPosition : endViewChildPosition;
    }
    StaticLayout layout = arrayList.get(childIndex).getLayout();
    if (x < 0) {
        x = 1;
    }
    if (y < 0) {
        y = 1;
    }
    if (x > layout.getWidth()) {
        x = layout.getWidth();
    }
    if (y > layout.getLineBottom(layout.getLineCount() - 1)) {
        y = (int) (layout.getLineBottom(layout.getLineCount() - 1) - 1);
    }

    for (int i = 0; i < layout.getLineCount(); i++) {
        if (y > layout.getLineTop(i) && y < layout.getLineBottom(i)) {
            line = i;
            break;
        }
    }
    if (line >= 0) {
        return layout.getOffsetForHorizontal(line, x);
    }

    return -1;
}