📜  Android中的TextWriter示例

📅  最后修改于: 2021-05-13 16:23:13             🧑  作者: Mango

TextWriter用于为文本设置动画。当用户打开应用程序(例如,启动画面)时,可以使用TextWriter。也可以使用“启动画面”代替TextWriter,但是TextWriter是一个动画库,众所周知,动画有助于吸引用户的注意力,因此最好学习它。可以根据诸如textColor,textSize,letterSpacing等要求自定义TextWriter。
文字作家

方法

  • 步骤1:在根build.gradle文件中添加支持库(不在模块build.gradle文件中)。这个库jitpack是一个新颖的软件包存储库。它是为JVM设计的,因此github和bigbucket中存在的任何库都可以直接在应用程序中使用。
    allprojects {           
     repositories {           
            maven { url 'https://jitpack.io' }           
         }          
    }           
    
  • 步骤2:build.gradle文件中添加支持库,并在“依赖项”部分中添加依赖项。
    implementation 'com.github.sarnavakonar:TextWriter:v1.0'          
    
  • 步骤3:activity_main.xml文件中添加以下代码。在此文件中,将TextWriter添加到布局中。
    activity_main.xml
    
    
      
         
      
    


    MainActivity.java
    package org.geeksforgeeks.textWriter          
      
    import android.graphics.Color;
    import android.os.Bundle;
    import android.widget.Toast;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import com.sarnava.textwriter.TextWriter;
      
    public class Activity extends AppCompatActivity {
        TextWriter textWriter;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            textWriter = findViewById(R.id.textWriter);   
            textWriter
                    // sets the width of the view
                    .setWidth(8)
                    // More the value of delay,
                    // more time it will take to finish
                    // duration is in milliseconds
                    .setDelay(30)
                    // set color of text
                    .setColor(Color.GREEN)
                    // sets the configuration/shape of the drawing
                    // based on Configuration selected
                    .setConfig(TextWriter.Configuration.INTERMEDIATE)
                    // set size of text
                    .setSizeFactor(30f)
                    // set letter spacing of text
                    .setLetterSpacing(15f)
                    .setText("ALGORITHM GFG")
                    // when writing is finished this 
                    // function will get invoked automaically.
                    .setListener(new TextWriter.Listener() {
                        @Override
                        public void WritingFinished() {
                            Toast.makeText(Activity.this,
                                    "Learn Algorithm!",
                                    Toast.LENGTH_SHORT).show();
                        }
                    })
                    .startAnimation();
        }
    }


  • 步骤4:MainActivity中添加以下代码。 Java文件。在此文件中,添加TextWriter的重要标签,并向其中添加setListner() ,当TextWriter编写整个文本时,该setListner()将自动调用。

    主要活动。Java

    package org.geeksforgeeks.textWriter          
      
    import android.graphics.Color;
    import android.os.Bundle;
    import android.widget.Toast;
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import com.sarnava.textwriter.TextWriter;
      
    public class Activity extends AppCompatActivity {
        TextWriter textWriter;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
      
            textWriter = findViewById(R.id.textWriter);   
            textWriter
                    // sets the width of the view
                    .setWidth(8)
                    // More the value of delay,
                    // more time it will take to finish
                    // duration is in milliseconds
                    .setDelay(30)
                    // set color of text
                    .setColor(Color.GREEN)
                    // sets the configuration/shape of the drawing
                    // based on Configuration selected
                    .setConfig(TextWriter.Configuration.INTERMEDIATE)
                    // set size of text
                    .setSizeFactor(30f)
                    // set letter spacing of text
                    .setLetterSpacing(15f)
                    .setText("ALGORITHM GFG")
                    // when writing is finished this 
                    // function will get invoked automaically.
                    .setListener(new TextWriter.Listener() {
                        @Override
                        public void WritingFinished() {
                            Toast.makeText(Activity.this,
                                    "Learn Algorithm!",
                                    Toast.LENGTH_SHORT).show();
                        }
                    })
                    .startAnimation();
        }
    }
    

      输出:在模拟器上运行