"Android Development Tips" magical textviewswitcher and Imageviewswitcher

Source: Internet
Author: User

This article is intended to talk about the use of Textviewswitcher and Imageviewswitcher.

Simple TextView and ImageView when changing content, the switch between the new content and the old content looks very stiff.

The corresponding extension classes, Textviewswitcher, and Imageviewswitcher are available in the Android SDK to animate the switching content.


<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Fill_ Parent "    android:layout_height=" fill_parent ">    <textswitcher        android:id=" @+id/your_textview "        android:layout_width= "fill_parent"        android:layout_height= "wrap_content"        android:layout_ Centerinparent= "true"/>    <button        android:layout_width= "fill_parent"        android:layout_height= " Wrap_content "        android:layout_alignparentbottom=" true "        android:onclick=" Onswitchtext "        android: text= "Click to replace Text"/></relativelayout>

public class Mainactivity extends Activity {private static final string[] texts = {"hello! "How is U?" "," Fine,and u?    " };    private int mtextsposition = 0;    Private Textswitcher Mtextswitcher;        @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.main);        Gets an instance of textswitcher Mtextswitcher = (textswitcher) Findviewbyid (R.id.your_textview); Mtextswitcher.setfactory (New Viewfactory () {@Override public View Makeview () {TEXTV                Iew t = new TextView (mainactivity.this);                T.setgravity (Gravity.center);            return t;                }        }); Set new content into the animated mtextswitcher.setinanimation (this, Android.        R.ANIM.FADE_IN); Sets the move-out animation for old content mtextswitcher.setoutanimation (this, Android.        R.anim.fade_out); Buton Click event ONSWITCHText (NULL);        } public void Onswitchtext (View v) {mtextswitcher.settext (texts[mtextsposition]);    Setnextposition ();    } private void Setnextposition () {mtextsposition = (mtextsposition + 1)% Texts.length; }}

The imageviewswitcher is used in a very similar way to Textviewswitcher.

"Android Development Tips" magical textviewswitcher and Imageviewswitcher

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.