TextView加载大文本发生卡顿

编程入门 行业动态 更新时间:2024-10-11 13:23:13

TextView<a href=https://www.elefans.com/category/jswz/34/1771433.html style=加载大文本发生卡顿"/>

TextView加载大文本发生卡顿

大概100多kb的文本内容的时候,会发生这个问题,采用的就是原生的TextView,原因的话网上讲得也比较详细,有的甚至连加载流程也都讲出来了,总的来说就是描画文本的作业是在UI线程里进行的

解决方案的话,网上提了大概四个吧。

方法一,把TextView的宽度设定为一个数字或者match_parent。NG,至少对我来说不起任何作用

方法二,使用androidx.appcompat.widget.AppCompatTextView。同上

方法三,在方法二的基础上,采用下述方法,加载速度肉眼可见的有提升,关键是改动量比较小

import androidx.appcompat.widget.AppCompatTextView;	
import androidx.core.text.PrecomputedTextCompat;	
import java.util.concurrent.Future;
。。。。
//关键代码
Future<PrecomputedTextCompat> future = PrecomputedTextCompat.getTextFuture(contentDelayed, txtViewContent.getTextMetricsParamsCompat(), null);
txtViewContent.setTextFuture(future);

方法四,相对方法三来说,改动量有点大:使用StaticLayout,具体原因以及理由网上有,可以自行百度。其实这个方法相当于是一个自定义view了。

参考:

import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.View;public class StaticLayoutView extends View {private Layout mLayout = null;private int mWidth;private int mHeight;public StaticLayoutView(Context context) {super(context);}public StaticLayoutView(Context context, AttributeSet attrs) {super(context, attrs);}public StaticLayoutView(Context context, AttributeSet attrs,int defStyleAttr) {super(context, attrs, defStyleAttr);}public void setLayout(Layout layout) {if (layout == null) {return;}mLayout = layout;if (mLayout.getWidth() != mWidth || mLayout.getHeight() != mHeight) {mWidth = mLayout.getWidth();mHeight = mLayout.getHeight();requestLayout();}}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);canvas.save();if (mLayout != null) {mLayout.draw(canvas, null, null, 0);}canvas.restore();}@Overrideprotected void onLayout(boolean changed, int left, int top, int right, int bottom) {super.onLayout(changed, left, top, right, bottom);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {if (mLayout != null) {setMeasuredDimension(mLayout.getWidth(), mLayout.getHeight());} else {super.onMeasure(widthMeasureSpec, heightMeasureSpec);}}}
public class StaticLayoutManager {public StaticLayout longStringLayout;private TextPaint textPaint;private Layout.Alignment alignment;private int hardCodeWidth;@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)public void initLayout(Context context, CharSequence longString) {textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);textPaint.density = context.getResources().getDisplayMetrics().density;textPaint.setTextSize(DisplayUtils.fromDPtoPix(context, DisplayUtils.TEXT_SIZE_DP));alignment = Layout.Alignment.ALIGN_NORMAL;hardCodeWidth = DisplayUtils.getScreenWidth(context);longStringLayout = new StaticLayout(longString, textPaint, hardCodeWidth, alignment, 1.0f, 0f, true);}public StaticLayout getLongStringLayout() {return longStringLayout;}private static StaticLayoutManager INSTANCE = null;public static StaticLayoutManager getInstance() {if (INSTANCE == null) {INSTANCE = new StaticLayoutManager();}return INSTANCE;}
}

使用方法

public class StaticLayoutAty extends AppCompatActivity {private String readFile() {try {InputStream is = getAssets().open("0.txt");int size = is.available();byte[] buffer = new byte[size];is.read(buffer);is.close();return new String(buffer);} catch (IOException e) {return "读取错误...";}}StaticLayoutView staticLayoutView;ProgressDialog pd;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_static_layout_aty);staticLayoutView = findViewById(R.id.static_content);pd = new ProgressDialog(this);pd.show();new Thread(new Runnable() {@Overridepublic void run() {StaticLayoutManager.getInstance().initLayout(StaticLayoutAty.this,  readFile());runOnUiThread(new Runnable() {@Overridepublic void run() {staticLayoutView.setLayout(StaticLayoutManager.getInstance().getLongStringLayout());Toast.makeText(StaticLayoutAty.this, "init span finish", Toast.LENGTH_LONG).show();pd.dismiss();}});}}).start();}
}
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android=""xmlns:tools=""android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/colorAccent"tools:context=".activity.StaticLayoutAty"><com.xx.StaticLayoutViewandroid:id="@+id/static_content"android:layout_width="fill_parent"android:layout_height="match_parent"/></ScrollView>

PS:方法四在试验的时候还是有问题,progressDialog消失了10多秒之后,文本才加载出来,而且还会ANR,可能是用的不对?

更多推荐

TextView加载大文本发生卡顿

本文发布于:2024-03-10 03:23:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1726886.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:加载   文本   发生   TextView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!