Android control Imageswitcher to achieve left and right picture switching function _android

Source: Internet
Author: User

The Imageswitcher class is a subclass of the Viewswitcher class, and its effect is to perform imageview transitions and animate. The following two steps are required to use this class:

1 provides a viewfactory for the Imageswitcher class, the view component that Viewfactory generates must be ImageView.

2 need to switch, only need to use the Imageswitcher setimagedrawable (), Setimageresource (), Setimageurl () method can be switched.

Activity_main.xml:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http:// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:o" rientation= "Vertical" tools:context= ". Mainactivity "> <imageswitcher android:id=" @+id/imageswitcher "android:layout_width=" 200DP "android:l ayout_height= "200DP" android:layout_gravity= "Center_horizontal"/> <relativelayout android:layout_width= " Match_parent "android:layout_height=" match_parent "android:orientation=" Horizontal "> <Button and Roid:id= "@+id/back" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:lay Out_alignparentleft= "true" android:text= "Back"/> <button android:id= "@+id/forward" android:l Ayout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentright= "true" a Ndroid:text= "FOrward "/> </RelativeLayout> </LinearLayout>

 

Main_activity.java:

Package com.example.android_imageswitcher1;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.ImageSwitcher;
Import Android.widget.ImageView;

Import Android.widget.ViewSwitcher.ViewFactory;  public class Mainactivity extends activity implements Viewfactory, Onclicklistener {imageswitcher Mimageswitcher =
  Null
  Button btn1, btn2;
  int index = 0;

  int[] Resid = new INT[9];
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.activity_main);
    Mimageswitcher = (imageswitcher) This.findviewbyid (R.id.imageswitcher);
    BTN1 = (Button) This.findviewbyid (r.id.back);
    BTN2 = (Button) This.findviewbyid (R.id.forward);
    Btn1.setonclicklistener (this);
    Btn2.setonclicklistener (this);
    Mimageswitcher.setfactory (this); Mimageswitcher.setinanimation (this, Android.) R. Anim.slide_in_left); Mimageswitcher.setoutanimation (this, Android.)
    R.anim.slide_out_right);
    Initresources ();
    if (Resid.length > 0) {mimageswitcher.setimageresource (resid[0]);
    } public void Initresources () {resid[0] = R.drawable.adobe;
    RESID[1] = r.drawable.android;
    RESID[2] = r.drawable.circle;
    RESID[3] = R.drawable.digg;
    RESID[4] = R.drawable.flower;
    RESID[5] = R.drawable.gmail;
    RESID[6] = R.drawable.imdb;
    RESID[7] = R.drawable.photo;
  RESID[8] = R.drawable.point;
  @Override public View Makeview () {return new ImageView (mainactivity.this);
    @Override public void OnClick (view view) {int action = View.getid ();
      Switch (action) {case r.id.back:index--;
      if (Index < 0) {index = resid.length-1;
      } mimageswitcher.setimageresource (Resid[index]);
    Break
      Case r.id.forward:index++;
if (Index > resid.length-1) {index = 0;      } mimageswitcher.setimageresource (Resid[index]);
    Break
    Default:break;

 }

  }

}

The effect of the implementation is as follows:

This is the entire content of this article, I hope to learn more about Android software programming help.

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.