Viewflipper for text rotation

Time:2020-10-26

Viewflipper to achieve text rotation (vertical scrolling ads imitating Taobao headlines), for your reference, the specific content is as follows

Ad entries can be written as layout files separately and then added to the general layout in the layout file or code

From the source code, we can see that viewflipper indirectly inherits FrameLayout. In other words, viewflipper is actually a FrameLayout, which just encapsulates a loop between animation implementation and handler implementation.

Layout file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/activity_main"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <!-- android:autoStart : set auto load next view -- >
 <!-- android:flipInterval : set the time interval for switching between views -- >
 <!-- android:inAnimation : set the entry animation of switching View -- >
 <!-- android:outAnimation : set the exit animation of switching View -- >
 <ViewFlipper

  android:id="@+id/view_flipper"
  android:layout_width="match_parent"
  android:layout_height="100dp"
  android:layout_centerInParent="true"
  android:autoStart="true"
  android:background="#808080"
  android:flipInterval="2000"
  android:inAnimation="@anim/slide_in_down"
  android:outAnimation="@anim/slide_out_up">

  <TextView
   android:id="@+id/first"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text= "In the daytime, the mountain is at its end"
   android:textColor="#FF00FF"
   android:textSize="50sp" />

  <TextView
   android:id="@+id/second"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text= "Yellow River into the sea"
   android:textColor="#FF00FF"
   android:textSize="50sp" />

  <TextView
   android:id="@+id/third"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text= "Want to be poor for thousands of miles"
   android:textColor="#FF00FF"
   android:textSize="50sp" />

  <TextView
   android:id="@+id/forth"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text= "To a higher level"
   android:textColor="#FF00FF"
   android:textSize="50sp" />
 </ViewFlipper>
</RelativeLayout>

This article introduces the properties used by viewflipper. These properties can be implemented by code, but they are placed in the layout for the sake of beautiful code

  • android:autoStart : set to automatically load the next view
  • android:flipInterval : set the time interval for switching between views
  • android:inAnimation : set the entry animation of switching view
  • android:outAnimation : set the exit animation of switching view

The following is an introduction to the commonly used methods of viewflipper. In addition to setting the above properties, other methods are also provided

  • Isflipping: determine whether the view switch is in progress
  • Setflip interval: set the time interval for switching between views
  • Start flipping: starts the view switching, and it will loop by default
  • Stopflipping: stop the view switching
  • Setoutanimation: set the exit animation of switching view
  • Setinanimation: set the entry animation of switching view
  • Shownext: displays the next view in viewflipper
  • Showprevious: displays the previous view in the viewfliper

There are also two animations, which are actually a translation animation, which are saved in the anim folder

slide_ In_ down.xml Enter animation


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
 <translate
  android:duration="@android:integer/config_mediumAnimTime"
  android:fromYDelta="100%"
  android:toYDelta="0"/>
</set>

slide_ out_ up.xml Exit animation


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
 android:duration="@android:integer/config_mediumAnimTime"
 android:fromYDelta="0"
 android:toYDelta="-100%"/>
</set>

MainActivity


package com.nrf.mydemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

 }

}

After running, the rendering

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.