gpt4 book ai didi

android - 如何在 Android TextView 中准确地阅读更多内容?

转载 作者:行者123 更新时间:2023-12-04 09:58:37 24 4
gpt4 key购买 nike

问题:如何淡出最后一行的末尾并在 TextView 中附加 Read More 文本?

见:

enter image description here

就像在这篇文章中一样,我想在描述即将结束时附加阅读更多文本,并且您会注意到有些文本是模糊的,不可见的。我想要所有这些。

我尝试了一些答案,但那是不同的。

我要这样和最后我想附加“阅读更多” :
enter image description here

我知道这个问题有答案:
How to fade out the end of the last line in a TextView?

但那个答案对我不起作用。

我的目的是在单击“阅读更多”时不展开 texview,而是打开新 Activity 。

最佳答案

这个answer有一些问题.我把它改成这样:

public class FadingTextView extends android.support.v7.widget.AppCompatTextView {

private static final double FADE_LENGTH_FACTOR = 1.5;

private final Shader shader;
private final Matrix matrix;
private final Paint paint;
private final Rect bounds;

public FadingTextView(Context context) {
this(context, null);
}

public FadingTextView(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.textViewStyle);
}

public FadingTextView(Context context, AttributeSet attrs, int defStyleAttribute) {
super(context, attrs, defStyleAttribute);

matrix = new Matrix();
paint = new Paint();
bounds = new Rect();
shader = new LinearGradient(0f, 0f, 1f, 0f, 0, 0xFF000000, Shader.TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
}

@Override
protected void onDraw(Canvas canvas) {
// Locals
final Matrix m = matrix;
final Rect b = bounds;
final Layout l = getLayout();

// Last line index
final int line = getLineCount() - 1;

// Determine text direction
final boolean isRtl = l.getParagraphDirection(line) == Layout.DIR_RIGHT_TO_LEFT;

// Last line bounds
getLineBounds(line, b);

// Adjust end bound to text length
final int lineStart = l.getLineStart(line);
final int lineEnd = l.getLineEnd(line);
final CharSequence text = getText().subSequence(lineStart, lineEnd);
final int measure = (int) (getPaint().measureText(text, 0, text.length()) + .5f);

final int fadeLength = (int) (b.width() / FADE_LENGTH_FACTOR);


// Save the layer
final int saveCount = canvas.saveLayer(b.left, 0, b.right,
b.bottom, null,
Canvas.ALL_SAVE_FLAG);

// Let TextView draw itself
super.onDraw(canvas);

// Adjust and set the Shader Matrix
m.reset();
m.setScale(fadeLength, 1f);
if (isRtl) {
m.postRotate(180f, fadeLength / 2f, 0f);
}
m.postTranslate(b.left, 0f);
shader.setLocalMatrix(matrix);

// Finally draw the fade
canvas.drawRect(b, paint);

canvas.restoreToCount(saveCount);
}
}

TextView
 <com.example.FadingTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e2f3eb"
android:ellipsize="end"
android:maxLines="3"
android:text="Lorem ipsum dolor....."
android:textColor="#0b8043" />

阅读更多:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/post_desc"
android:layout_alignBottom="@+id/post_desc"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
android:background="@android:color/transparent"
android:text="Read More"/>

FADE_LENGTH_FACTOR = 1

enter image description here

FADE_LENGTH_FACTOR = 1.5

enter image description here

关于android - 如何在 Android TextView 中准确地阅读更多内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61872357/

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