Android_textswitcher and imageswitcher

Source: Internet
Author: User

First, let's look at the inheritance relationship. The Inheritance relationships between imageswitcher and textswitcher are the same. Two important parent classes: viewswitcher and viewanimator

Inherited from viewswitcher, it indicates that the switch function is available.

Inherited from viewanimator, it indicates that the animation function is available.

Imageswitcher Principle

The imageswitcher content has been explained in Gallery.

A rough understanding of imageswitcher is the imageview selector.

Imageswitcher principle: imageswitcher has two sub-views: imageview. When sliding left and right, the two imageviews are switched back and forth to display images.

Next, let's take a look at the built-in Android source to better understand this principle:

Since there are two sub-imageviews, we need to create two imageviews for imageswitcher. Create imageswitcher through the factory. See the following code.

imageSwicher.setFactory(this);

Set viewfactory for imageswitcher

@Overridepublic View makeView() {ImageView imageView = new ImageView(this);imageView.setImageResource(arrayPictures[pictureIndex]);return imageView;}

Implements the makeview () method of viewfactory. The makeview () method is used to create two imageviews for imageswitcher.

Next let's take a look at the specific code of the setfactory () method.

public void setFactory(ViewFactory factory) {    mFactory = factory;    obtainView();    obtainView();}

We can see that at the same time of setfactory, The obtainview () method is called twice. The obtainview () method is to add a sub-imageview to imageswitcher, and two sub-imageviews are added after two calls.

Let's take a look at the specific code of the obtainview () method.

private View obtainView() {    View child = mFactory.makeView();    LayoutParams lp = (LayoutParams) child.getLayoutParams();    if (lp == null) {        lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);    }    addView(child, lp);    return child;}

You can see that the duty of the obtainview () method is to create a view using the makeview () method, and then add the created view to imageswitcher.

Let's take a look at the following method.

public void setImageResource(int resid){    ImageView image = (ImageView)this.getNextView();    image.setImageResource(resid);    showNext();}

This method is used to display the next image. We can see that the getnextview () and shownext () methods are called in this method. Let's take a look at the specific code of the two methods.

public View getNextView() {    int which = mWhichChild == 0 ? 1 : 0;    return getChildAt(which);}
public void showNext() {    setDisplayedChild(mWhichChild + 1);}

The getnextview () method switches between two sub-imageviews. The shownext () method is used to display which of the two sub-Views

That is to say, the getnextview () method is used to get the next view, then the imageresource of the view is reset, And the next view is displayed through the shownext () method.

Now, the principles of imageswitcher are completely explained. A demo is attached below.

Imageswitcher instance

Main. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ImageSwitcher        android:id="@+id/imageSwicher"        android:layout_width="fill_parent"        android:layout_height="0dip"        android:layout_weight="1"        android:background="@android:color/white"        android:gravity="center" >    </ImageSwitcher></LinearLayout>

Imageswicherdemoactivity. Java

Package COM. tianjf; import android. app. activity; import android. OS. bundle; import android. view. motionevent; import android. view. view; import android. view. view. ontouchlistener; import android. view. animation. animationutils; import android. widget. imageswitcher; import android. widget. imageview; import android. widget. viewswitcher. viewfactory;/*** a demo of swiping the picture left and right ** @ author tianjf **/public class imageswicherd Emoactivity extends activity implements viewfactory, ontouchlistener {private imageswitcher imageswicher; // Image array private int [] arraypictures = {R. drawable. bg001, R. drawable. bg002, R. drawable. bg003, R. drawable. bg004}; // indexprivate int pictureindex of the image to be displayed in the Image array; // The X coordinate private float touchdownx pressed by the finger when sliding left and right; // The X coordinate private float touchupx released by the finger when sliding left and right; @ overridepublic void oncreate (bundle savedinstancestate) {s Uper. oncreate (savedinstancestate); setcontentview (R. layout. main); imageswicher = (imageswitcher) findviewbyid (R. id. imageswicher); // set factory for imageswicher to create imageviewimageswicher for imageswicher. setfactory (this); // sets the imageswitcher slide event imageswicher. setontouchlistener (this) ;}@ overridepublic view makeview () {imageview = new imageview (this); imageview. setimageresource (arraypictures [pictureindex ]); Return imageview;} @ overridepublic Boolean ontouch (view V, motionevent event) {If (event. getaction () = motionevent. action_down) {// obtain the X coordinate touchdownx = event pressed by the finger when sliding left or right. getx (); Return true;} else if (event. getaction () = motionevent. action_up) {// obtain the X coordinate touchupx = event that is released by the finger when sliding left or right. getx (); // from left to right, check the previous if (touchupx-touchdownx> 100) {// get the indexpictureindex = pictureindex = 0? Arraypictures. length-1: pictureindex-1; // sets the animation imageswicher for image switching. setinanimation (animationutils. loadanimation (this, android. r. anim. slide_in_left); imageswicher. setoutanimation (animationutils. loadanimation (this, android. r. anim. slide_out_right); // you can specify the image imageswicher. setimageresource (arraypictures [pictureindex]); // from right to left, check the next one} else if (touchdownx-touchupx> 100) {// obtain the indexpictureindex = Pictureindex = arraypictures. Length-1? 0: pictureindex + 1; // set the animation for image switching // because slide_out_left and slide_in_right are not provided by Android, slide_out_left and slide_out_right are used to write slide_out_left and animation (animationutils. loadanimation (this, R. anim. slide_out_left); imageswicher. setoutanimation (animationutils. loadanimation (this, R. anim. slide_in_right); // you can specify the image imageswicher. setimageresource (arraypictures [pictureindex]);} return true;} return false ;}}

Because Android does not provide slide_out_left and slide_in_right, slide_out_left and slide_in_right are compiled in the same way as slide_in_left and slide_in_right. The Code is as follows:

Slide_in_right.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate android:fromXDelta="50%p" android:toXDelta="0" android:duration="300"/>    <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /></set>

Slide_out_left.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate android:fromXDelta="0" android:toXDelta="-50%p" android:duration="300"/>    <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /></set>

Now, the imageswitcher explanation ends. The following is a demo of textswitcher similar to imageswitcher.

Textswitcher works in the same way as imageswitcher, but only imageview and textview. So I will not talk about it here. directly add the code

Main. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextSwitcher        android:id="@+id/textSwicher"        android:layout_width="fill_parent"        android:layout_height="0dip"        android:layout_weight="1"        android:background="@android:color/white"        android:gravity="center" >    </TextSwitcher></LinearLayout>

Textswitcherdemoactivity. Java

Package COM. tianjf; import android. app. activity; import android. OS. bundle; import android. view. gravity; import android. view. motionevent; import android. view. view; import android. view. view. ontouchlistener; import android. view. viewgroup. layoutparams; import android. view. animation. animationutils; import android. widget. textswitcher; import android. widget. textview; import android. widget. viewswitcher. viewfactory ;/* ** A demo ** @ author tianjf **/public class textswitcherdemoactivity extends activity implements viewfactory, ontouchlistener {private textswitcher textswicher; // Image array private string [] arraytexts = {"text 01", "text 02", "text 03", "text 04 "}; // indexprivate int textindex of the image to be displayed in the Image array; // The X coordinate private float touchdownx pressed by the finger when sliding left and right; // The X coordinate private float touchupx released by the finger when sliding left and right; @ overridepublic void oncreate (Bund Le savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); textswicher = (textswitcher) findviewbyid (R. id. textswicher); // set the factory for textswitcher to create textviewtextswicher for textswitcher. setfactory (this); // set textswitcher to slide the event textswicher left and right. setontouchlistener (this) ;}@ overridepublic view makeview () {textview = new textview (this); textview. settextsize (100); textview. set Layoutparams (New textswitcher. layoutparams (layoutparams. fill_parent, layoutparams. fill_parent); textview. setgravity (gravity. center); textview. settext (arraytexts [textindex]); Return textview ;}@ overridepublic Boolean ontouch (view V, motionevent event) {If (event. getaction () = motionevent. action_down) {// obtain the X coordinate touchdownx = event pressed by the finger when sliding left or right. getx (); Return true;} else if (event. getaction () = motionevent. Action_up) {// obtain the X coordinate touchupx = event that is released by the finger when sliding left or right. getx (); // read the previous text from left to right IF (touchupx-touchdownx> 100) {// retrieve the indextextindex = textindex = 0? Arraytexts. length-1: textindex-1; // sets the textswicher animation for text switching. setinanimation (animationutils. loadanimation (this, android. r. anim. slide_in_left); textswicher. setoutanimation (animationutils. loadanimation (this, android. r. anim. slide_out_right); // sets textswicher. settext (arraytexts [textindex]); // from right to left, check the next one} else if (touchdownx-touchupx> 100) {// get the indextextindex = textindex = arraytexts of the text to be viewed. le Ngth-1? 0: textindex + 1; // sets the animation for text switching. // because Android does not provide slide_out_left and slide_in_right, slide_out_left and animation (animationutils. loadanimation (this, R. anim. slide_out_left); textswicher. setoutanimation (animationutils. loadanimation (this, R. anim. slide_in_right); // sets textswicher. settext (arraytexts [textindex]);} return true;} return false ;}}

Slide_in_right.xml and slide_out_left.xml can be referenced in the demo of imageswitcher. They will not be repeated here.

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.