我如何使用HTextView缩放textview效果(com.hanks.htextview.HTextView)作为多行效果?

时间:2018-02-12 18:56:09

标签: android android-layout android-studio android-animation textview

目前它使用单行,当我输入包含多行的文本时,应用程序崩溃。

MainActivity:

    HTextView tvQuestion_hanks = (HTextView) findViewById(R.id.tvQs_hanks);
    tvQuestion_hanks.setAnimateType(HTextViewType.SCALE);
    tvQuestion_hanks.animateText("Hello world");

布局:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:htext="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">


         <com.hanks.htextview.HTextView

                android:id="@+id/tvQs_hanks"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginTop="16dp"
                android:textColor="#000000"
                android:textSize="17dp"
                android:text="Question"
                htext:animateType="scale"
                android:gravity="start">

         </com.hanks.htextview.HTextView>

     </RelativeLayout>

我尝试过不同的布局代码,例如;

     android:lines="5"
     android:maxLines="5"
     android:nestedScrollingEnabled="true"

但它没有用。这是我的logcat刚刚按下按钮后,多行文本被发送到HTextView(正如我所说的应用程序崩溃):

            02-13 20:01:18.074 4456-4456/com.example.moham.myapplication D/AndroidRuntime: 
            Shutting down VM
            02-13 20:01:18.074 4456-4456/com.example.moham.myapplication W/dalvikvm: 
            threadid=1: thread exiting with uncaught exception (group=0x94caeb20)
            02-13 20:01:18.074 4456-4456/com.example.moham.myapplication E/AndroidRuntime: 
            FATAL EXCEPTION: main
                                                                           Process: com.example.moham.myapplication, PID: 
           4456
                                                                           java.lang.ArrayIndexOutOfBoundsException: 
           length=100; index=100
                                                                               at 
           com.hanks.htextview.animatetext.HText.prepareAnimate(HText.java:88)
                                                                               at 
           com.hanks.htextview.animatetext.HText.animateText(HText.java:74)
                                                                               at 
           com.hanks.htextview.HTextView.animateText(HTextView.java:107)
                                                                               at 
           com.example.moham.myapplication.MainActivity$1.onClick(MainActivity.java:34)
                                                                               at 
           android.view.View.performClick(View.java:4438)
                                                                               at 
           android.view.View$PerformClick.run(View.java:18422)
                                                                               at 
           android.os.Handler.handleCallback(Handler.java:733)
                                                                               at 
           android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:136)
                                                                               at 
           android.app.ActivityThread.main(ActivityThread.java:5019)
                                                                               at java.lang.reflect.Method.invokeNative(Native 
           Method)
                                                                               at 
           java.lang.reflect.Method.invoke(Method.java:515)
                                                                               at 
           com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                               at 
           com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                               at dalvik.system.NativeStart.main(Native 
           Method)

HTextView GitHub页面:https://github.com/hanks-zyh/HTextView

HTextView GitHub第2页:https://github.com/yikwing/HTextView

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,不只是通过它的正确方式,但它终于奏效了。作为&#34; Re&#39;&#34;在评论中注意到,这是来自HTextView主库的一个错误,所以我让它的开发人员解决它,但我很匆忙,所以我这样解决了:

由于HTextView只能处理单行文本条目,我编写了一个函数,以便通过以下方式测量屏幕宽度上可能出现的字符大小:

首先查找文本宽度(以像素为单位)。

第二步)将其划分为文本长度。

    PixelPerChar = TXT_width / TXT.length();

然后,为了沿屏幕宽度近似可能的字符数量,我将屏幕宽度划分为PixelPerChar,因此结果是在屏幕上正确匹配的字符的近似数量并且它不会被赢得超过一行文字。

    PossibleChars = lcdWidth() / PixelPerChar;

下一步是将HtextViews一个接一个地添加到LinearLayout中,同时将文本子串化为单行serries,并将其发送到每个创建的HTextView,直到文本变得比屏幕宽度短。 / p>

查看完整的功能代码:(Qs = Question = text :)

private void QsPrint(String Qs){

    LinearLayout LinearLay1 = (LinearLayout) findViewById(R.id.LinearLay1);
    LinearLay1.removeAllViews();


    int Qswidth = QsWidth(tvQuestion_hanks , Qs);

    DisplayMetrics metrics = MainMenu.this.getResources().getDisplayMetrics();
    int lcdwidth = metrics.widthPixels;
    int lcdheight = metrics.heightPixels;



    int PIXperCHAR = Qswidth / Qs.length();
    SubStart = 0;
    SubEnd = lcdwidth / PIXperCHAR;
    sub1 = "";


    while (true)
    {


        HTextView tvQs_Hanks_multiline = new HTextView(MainMenu.this);

        RelativeLayout.LayoutParams params1 = new RelativeLayout
                .LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);


        tvQs_Hanks_multiline.setLayoutParams(params1);
        tvQs_Hanks_multiline.setTextSize(TypedValue.COMPLEX_UNIT_DIP, PixelToDp((int)(tvQuestion_hanks.getTextSize())));
        tvQs_Hanks_multiline.setAnimateType(HTextViewType.SCALE);
        LinearLay1.addView(tvQs_Hanks_multiline);

        sub1 = "";
        if(Qswidth >= lcdwidth) {
                try {
                    sub1 = Qs.substring(SubStart, SubEnd);
                } catch (Exception e) {
                    QsPrintSubString(Qs);
                }


            while (QsWidth(tvQs_Hanks_multiline , sub1) >= lcdwidth){
                SubEnd --;
                sub1 = Qs.substring(SubStart, SubEnd);
            }

            SubEnd -=4; // For spacing before/after the TXT
            sub1 = Qs.substring(SubStart, SubEnd);
            tvQs_Hanks_multiline.animateText("   "+sub1+"");
        }
        else if(Qswidth < lcdwidth){
            sub1 = Qs;
            tvQs_Hanks_multiline.animateText("   " + sub1 + "");
            break;
        }




        if(Qswidth >= lcdwidth) {

            Qs = Qs.substring(SubEnd, Qs.length());
            Qswidth = QsWidth(tvQs_Hanks_multiline , Qs);
            PIXperCHAR = Qswidth / Qs.length();
            SubStart = 0;
            if(Qswidth >= lcdwidth) {
                SubEnd = lcdwidth / PIXperCHAR;
            }
        }



    }

}

我使用此函数计算文本宽度(以像素为单位):

private int QsWidth(TextView tv , String Question){

    String Qs = Question;
    Rect bound = new Rect();
    Paint textPaint = tv.getPaint();
    textPaint.getTextBounds(Qs, 0, Qs.length(), bound);
    int QsWidth = bound.width();

    return QsWidth;
}

像素到dp转换器功能:

public static int PixelToDp(int px)
{
    return (int) (px / Resources.getSystem().getDisplayMetrics().density);
}

最后,我使用了这个功能,因为有时候文本长度是特殊数量的字符,而且#34; QsPrint&#34;函数无法正确子串并崩溃:(,所以我使用了这个函数。

它非常简单,它不断减少SubEnd并再次测试Substring。

该功能将继续运行,直到它不再崩溃为止:)

private void QsPrintSubString(String Qs){

    SubEnd--;
    try {
        sub1 = Qs.substring(SubStart, SubEnd);
    }catch (Exception e){
        QsPrintSubString(Qs);
    }
}

XML布局:

(你在下面看到的tvQs_hanks几乎没用,只是需要&#34; QsWidth()&#34;函数才能在我们的LinearLayout中创建任何HTextView之前计算文本宽度

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:htext="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">




            <LinearLayout
                android:id="@+id/LinearLay1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical">


            </LinearLayout>

            <com.hanks.htextview.HTextView
                android:id="@+id/tvQs_hanks"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Question"
                android:textColor="#000000"
                android:textSize="17dp"
                android:visibility="gone"
                htext:animateType="scale">

            </com.hanks.htextview.HTextView>

 </RelativeLayout>