gpt4 book ai didi

Android - ImageSpan - 如何在文本末尾居中对齐图像

转载 作者:行者123 更新时间:2023-11-29 14:30:06 29 4
gpt4 key购买 nike

我在 xml 布局中有以下内容:

enter image description here

请注意六边形 #4 没有与文本居中对齐。我该怎么做:这是我到目前为止尝试过的:

为了实际获得带有 # 的 View ,我膨胀了一个看起来像这样的 View :

//my_hexagon_button.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="0dp"
tools:ignore="MissingPrefix">


<Button
android:id="@+id/tv_icon"
fontPath="proxima_nova_semi_bold.otf"
android:layout_width="16dp"
android:layout_height="17.5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:alpha=".25"
android:background="@drawable/hexagon"
android:clickable="true"
android:contentDescription="@string/content_description"
android:focusable="false"
android:padding="0dp"
android:text="4"
android:textColor="@color/white"
android:textSize="8dp"
/>

</LinearLayout>

膨胀 View 后,我复制了它的绘图缓存,并在 ImageSpan 中使用它。这是我获取绘图缓存副本的方法:

public Bitmap getIconBitmap() {
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(getActivity());

// inflate and measure the button then grab its image from the view cache
ViewGroup parent = (ViewGroup) inflater.inflate(R.layout.my_hexagon_button, myRoot);
TextView tv = (TextView) parent.findViewById(R.id.tv_icon);

parent.setDrawingCacheEnabled(true);
parent.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
parent.layout(0, 0, parent.getMeasuredWidth(), parent.getMeasuredHeight());

parent.buildDrawingCache(true);
// if you need bounds on the view, swap bitmap for a drawable and call setbounds, im not using bounds
Bitmap b = Bitmap.createBitmap(parent.getDrawingCache());
parent.setDrawingCacheEnabled(false); // clear drawing cache

return b;
}

所以现在我有一个位图,看起来像我附加的绘图中的六边形#4 图像。现在让我们在 ImageSpan 中使用它:

public Spannable createImageSpan(TextView tv,Bitmap bitmapIcon) {

Spannable span = new SpannableString(tv.getText());
int start = span.length() - 1;
int end = span.length();

ImageSpan image = new ImageSpan(new BitmapDrawable(getResources(), bitmapIcon),ImageSpan.ALIGN_BASELINE);
span.setSpan(image, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

return span;

}

然后我只是在我的 TextView 上设置了那个跨度。也不要忘记在可绘制对象上设置边界,否则它不会显示,它可以工作,但图像未在文本中居中对齐。注意它是如何下降到底部的。我怎样才能彻底解决这个问题?

最佳答案

您可以使用此类将 ImageSpan 与文本对齐

public class VerticalImageSpan extends ImageSpan {

public VerticalImageSpan(Drawable drawable) {
super(drawable);
}

/**
* update the text line height
*/
@Override
public int getSize(Paint paint, CharSequence text, int start, int end,
Paint.FontMetricsInt fontMetricsInt) {
Drawable drawable = getDrawable();
Rect rect = drawable.getBounds();
if (fontMetricsInt != null) {
Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
int fontHeight = fmPaint.descent - fmPaint.ascent;
int drHeight = rect.bottom - rect.top;
int centerY = fmPaint.ascent + fontHeight / 2;

fontMetricsInt.ascent = centerY - drHeight / 2;
fontMetricsInt.top = fontMetricsInt.ascent;
fontMetricsInt.bottom = centerY + drHeight / 2;
fontMetricsInt.descent = fontMetricsInt.bottom;
}
return rect.right;
}

/**
* see detail message in android.text.TextLine
*
* @param canvas the canvas, can be null if not rendering
* @param text the text to be draw
* @param start the text start position
* @param end the text end position
* @param x the edge of the replacement closest to the leading margin
* @param top the top of the line
* @param y the baseline
* @param bottom the bottom of the line
* @param paint the work paint
*/
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, Paint paint) {

Drawable drawable = getDrawable();
canvas.save();
Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
int fontHeight = fmPaint.descent - fmPaint.ascent;
int centerY = y + fmPaint.descent - fontHeight / 2;
int transY = centerY - (drawable.getBounds().bottom - drawable.getBounds().top) / 2;
canvas.translate(x, transY);
drawable.draw(canvas);
canvas.restore();
}
}

多亏了这个answer

关于Android - ImageSpan - 如何在文本末尾居中对齐图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43404526/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com