An example of automatic text switcher developed by Android

Time:2021-6-7

In this paper, an example of Android development to achieve automatic text switch function. The details are as follows:

Introduction:

1. Textswitch is a subclass of viewswitch and inherits all the methods of viewswitch

2. Similar to another subclass of viewswitcher, textswitcher also has

3. The difference of imageswitcher is that the viewfactory method of textswitchermakeVieW()A textview component must be put back

Specific effect:

Radial thinking:

If you combine it with the carousel map( https://www.jb51.net/article/158149.htm )Combined with text effect can be achieved with the carousel map.

First, the layout file is given

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center_horizontal">
    <!-- Define a viewswitcher and make the animation effect when switching text -- >
    <TextSwitcher
      android:id="@+id/textSwitcher"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:inAnimation="@android:anim/slide_in_left"
      android:outAnimation="@android:anim/slide_out_right"
      android:onClick="next">
    </TextSwitcher>
</RelativeLayout>

On the realization of text timing switch:

1. first, write a next method and call the parent class in this song method.setText()Method to set the text

2. Create a special thread in the main thread to switch pictures. Note: you can’t change the view directly in the thread, so you need to send a minor repair to change the layout content (text) in the handler object

The implementation is as follows:

public class MainActivity extends Activity {
  String[] string = new String[]{
      "I love high numbers.",
      I love probability,
      "I love computer networks.",
      "I love the operating system"
  };
  TextSwitcher textSwitcher;
  int curStr ;
  Handler handler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
      next(null);
    }
  };
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);
    textSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
      @Override
      public View makeView() {
        TextView textView = new TextView(MainActivity.this);
        textView.setTextSize(40);
        textView.setTextColor(Color.RED);
        return textView;
      }
    });
    new Thread(){
      @Override
      public void run() {
        while (true){
          Message message = handler.obtainMessage();
          message.obj = 0;
          handler.sendMessage(message);
          try {
            sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }.start();
  }
  private void next(View scource){
    textSwitcher.setText(string[curStr = ( curStr++ % string.length )]);
  }
}

For more information about Android, readers interested in this website can see the following topics: summary of Android graphics and image processing skills, introduction and advanced course of Android development, summary of Android debugging skills and solutions to common problems, summary of usage of Android basic components, summary of Android view skills, and “Android layout skills summary” and “Android control usage summary”

I hope this article is helpful for Android programming.