如何使用 HTextView 比例文本视图效果 (com.hanks.htextview.HTextView) 作为多行效果?

How can i use HTextView scale textview effect (com.hanks.htextview.HTextView) as multiline effect?

目前它只能使用单行,当我输入多行文本时,应用程序会崩溃。

主要活动:

    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.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

我已经解决了这个问题,不仅方法正确,而且终于奏效了。正如 "Re'em" 在评论中注意到的那样,这是 HTextView 主库中的一个错误,所以我要求它的开发人员解决它,但我很着急,所以我这样解决了:

由于 HTextView 只能处理 one-line 文本条目,我编写了一个函数来测量 screen-width 中大约可能的字符数量:

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

第二)将其分成文本长度。

    PixelPerChar = TXT_width / TXT.length();

然后,为了估计沿 screen-width 可能出现的字符数量,我将 screen-width 分成 PixelPerChar ,因此结果是沿屏幕正确显示的大约字符数,并且它不会超过 one-line 文本。

    PossibleChars = lcdWidth() / PixelPerChar;

下一步是将HtextView一个接一个添加到一个LinearLayout中,同时将文本串成one-line系列,发送给每一个创建的HTextView,直到文本变得比screen-width.

看看完整的功能代码:(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);
}

最后,我使用了这个函数,因为有时文本长度是一个特殊数量的字符,"QsPrint" 函数无法正确地对其进行子字符串化并崩溃:(,所以我使用了这个函数。

它工作起来很简单,它不断减少 SubEnd 并再次测试 Substring。

该函数将保持 运行 直到它不再崩溃:)

private void QsPrintSubString(String Qs){

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

XML布局:

(tvQs_hanks 你在下面看到的几乎没用,只是在 "QsWidth()" 函数中需要它,以便在我们的 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>

试试这个方法...它肯定适合你,因为它对我来说非常适合多行文本。 .

稍后谢谢我:)

动画是为我们的应用程序注入活力的好方法。动画视图很容易,但是如何动画文本。对于动画文本(不是 TextView),我们将使用 GitHub.

上可用的免费库 HTextView

对于这个演示应用程序,我们有六个 HTextView,我们在所有 HTextView 中为不同的文本设置动画。为了使用 HTextView,我们将添加 HTextView 依赖项 in-app Gradle 文件。

build.gradle(模块:App)

def htextview_version = "0.1.2"
compile "com.hanks:htextview-base:$htextview_version"        // base library
compile "com.hanks:htextview-fall:$htextview_version"        // optional
compile "com.hanks:htextview-fade:$htextview_version"        // optional
compile "com.hanks:htextview-line:$htextview_version"        // optional
compile "com.hanks:htextview-rainbow:$htextview_version"     // optional
compile "com.hanks:htextview-typer:$htextview_version"       // optional
compile "com.hanks:htextview-scale:$htextview_version"       // optional
compile "com.hanks:htextview-evaporate:$htextview_version"   // optional

同步,你的项目。

以下是 activity_main.xml 文件的代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.hanks.htextview.fall.FallTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text=""
        android:id="@+id/textView"
        android:textSize="20sp"
        android:gravity="center"
         />


    <com.hanks.htextview.line.LineTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text=""
        android:id="@+id/textViewLine"
        app:animationDuration="1000"
        android:textSize="20sp"
        app:lineColor="#1367bc"
        app:lineWidth="4dp"
        android:gravity="center"
        />

    <com.hanks.htextview.fade.FadeTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text=""
        android:id="@+id/textViewFade"
        app:animationDuration="1000"
        android:textSize="20sp"
        android:gravity="center"
        />


    <com.hanks.htextview.typer.TyperTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text=""
        android:id="@+id/textViewTyper"
        app:charIncrease="3"
        app:typerSpeed="80"
        android:textSize="20sp"
        android:gravity="center"
        />


    <com.hanks.htextview.rainbow.RainbowTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text=""
        android:id="@+id/textViewRainBow"
        android:textSize="20sp"
        app:colorSpace="150dp"
        app:colorSpeed="4dp"
        android:gravity="center"
        />


    <com.hanks.htextview.scale.ScaleTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text=""
        android:id="@+id/textViewScale"
        android:textSize="20sp"
        android:gravity="center"
        />

</LinearLayout>

在我们的MainActivity.java文件中,我们首先初始化所有的HTextView。我们有一个用于不同消息的字符串数组。我们正在使用字符串数组随时间更改所有 HTextView 文本。以下是 MainActivity.java 文件的代码...

package com.vlemonn.java.blog.htextview;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.hanks.htextview.base.HTextView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    
    private HTextView textView,textViewScale,textViewRainBow,textViewTyper,textViewFade,textViewLine;
    int delay = 2000; //milliseconds
    Handler handler;
    ArrayList<String> arrMessages = new ArrayList<>();
    int position=0;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /* Some Sample Messages for Animation */
        arrMessages.add("Hello, vLemonn!");
        arrMessages.add("Hello, World!");
        arrMessages.add("Free Android Tutorials");
        arrMessages.add("By Mayank Sanghvi");
        arrMessages.add("@vLemonn");
        arrMessages.add("Subscribe, Share, Like");
        arrMessages.add("Support Us");
        
        /* Initialize HTextView */
        textView = findViewById(R.id.textView);
        textViewScale= findViewById(R.id.textViewScale);
        textViewRainBow= findViewById(R.id.textViewRainBow);
        textViewTyper= findViewById(R.id.textViewTyper);
        textViewFade= findViewById(R.id.textViewFade);
        textViewLine= findViewById(R.id.textViewLine);

        /* First Message */
        textView.animateText(arrMessages.get(position));
        textViewScale.animateText(arrMessages.get(position));
        textViewRainBow.animateText(arrMessages.get(position));
        textViewTyper.animateText(arrMessages.get(position));
        textViewFade.animateText(arrMessages.get(position));
        textViewLine.animateText(arrMessages.get(position));
        position++;
        
        /* Change Messages every 2 Seconds */
        handler = new Handler();
        handler.postDelayed(new Runnable(){
            public void run(){

                handler.postDelayed(this, delay);
                if(position>=arrMessages.size())
                    position=0;
                textView.animateText(arrMessages.get(position));
                textViewScale.animateText(arrMessages.get(position));
                textViewRainBow.animateText(arrMessages.get(position));
                textViewTyper.animateText(arrMessages.get(position));
                textViewFade.animateText(arrMessages.get(position));
                textViewLine.animateText(arrMessages.get(position));
                position++;
            }
        }, delay);

    }
}

最终演示(输出): Final Demo (Output)

如果觉得这个回答有用请采纳,让大家用起来更方便...