Android is based on Imageswitcher to implement picture switching function _android

Source: Internet
Author: User

Switch the picture control left and right, we all use Viewpager, Viewfipper more, I also used viewpager to achieve, using the viewpager to achieve around the circular sliding picture, interested can go to see, Introduction today is based on imageswitcher implementation of the left and right to switch the picture, first screenshot bar

OK, next look at the code, the first picture is a GridView, click Item to jump to the second interface, the first interface can be ignored, mainly to explain the imageswitcher of the picture, first look at the layout file

 <?xml version= "1.0" encoding= "UTF-8"?> <framelayout "xmlns:android=" 
  Schemas.android.com/apk/res/android "android:layout_width=" fill_parent "android:layout_height=" Fill_parent "> <imageswitcher android:id= "@+id/imageswitcher1" android:layout_width= "Fill_parent" android:layout_height = "Fill_parent" > </ImageSwitcher> <relativelayout android:layout_width= "Fill_parent" an droid:layout_height= "wrap_content" android:orientation= "vertical" > <linearlayout android:id= "@+id/viewgroup" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" Androi    
      D:layout_alignparentbottom= "true" android:layout_marginbottom= "30DP" android:gravity= "Center_horizontal" android:orientation= "Horizontal" > </LinearLayout> </RelativeLayout> </framelayou T> 

And then the activity code, which is generally simpler, and the code I added a comment

Package com.example.photoalbum; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import android.view.MotionEvent; 
Import Android.view.View; 
Import Android.view.View.OnTouchListener; 
Import Android.view.ViewGroup; 
Import Android.view.animation.AnimationUtils; 
Import Android.widget.ImageSwitcher; 
Import Android.widget.ImageView; 
Import Android.widget.LinearLayout; 
Import Android.widget.RelativeLayout.LayoutParams; 
Import Android.widget.Toast; 
 
Import Android.widget.ViewSwitcher.ViewFactory; 
   public class Showphotoactivity extends activity implements Viewfactory, ontouchlistener{/** * Imagaswitcher Reference 
  * * Private Imageswitcher Mimageswitcher; 
  /** * Picture ID array/private int[] imgids; 
  /** * Currently selected picture ID number */private int currentposition; 
  /** * Press the point of the x coordinate * * Private float DOWNX; 
  /** * Loading point of the container * * Private linearlayout linearlayout; 
  
  /** * Point Points Group * * Private imageview[] tips; @Override ProtectEd void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
     
    Setcontentview (R.layout.show_photo); Imgids = new int[]{r.drawable.item01,r.drawable.item02,r.drawable.item03,r.drawable.item04, R.drawable.item05, R. Drawable.item06, R.drawable.item07, r.drawable.item08,r.drawable.item09, R.DRAWABLE.ITEM10, R.drawable.item11, R. 
    DRAWABLE.ITEM12}; 
    Instantiate imageswitcher Mimageswitcher = (imageswitcher) Findviewbyid (r.id.imageswitcher1); 
    Set Factory Mimageswitcher.setfactory (this); 
     
    Set Ontouchlistener, we use touch events to toggle the picture Mimageswitcher.setontouchlistener (this); 
     
    LinearLayout = (linearlayout) Findviewbyid (R.id.viewgroup); 
    Tips = new Imageview[imgids.length]; 
      for (int i=0; i Downx) {if (currentposition > 0) {//Set animation, where the animation is relatively simple, not 
          Clear to the Internet to look at the relevant content mimageswitcher.setinanimation (Animationutils.loadanimation (Getapplication (), r.anim.left_in)); 
          Mimageswitcher.setoutanimation (Animationutils.loadanimation (Getapplication (), r.anim.right_out)); 
          CurrentPosition--; 
          Mimageswitcher.setimageresource (imgids[currentposition% imgids.length]); Setimagebackground (currentposition); 
        }else{Toast.maketext (Getapplication (), "Already the first", Toast.length_short). Show (); } if (Lastx < Downx) {if (CurrentPosition < imgids.length-1) {MIMAGESWI 
          Tcher.setinanimation (Animationutils.loadanimation (Getapplication (), r.anim.right_in)); 
          Mimageswitcher.setoutanimation (Animationutils.loadanimation (Getapplication (), r.anim.lift_out)); 
          CurrentPosition + +; 
          Mimageswitcher.setimageresource (Imgids[currentposition]); 
        Setimagebackground (currentposition); 
        }else{Toast.maketext (Getapplication (), "to the last piece", Toast.length_short). Show (); 
    }} break; 
  return true; 
 } 
 
}

The above switch picture mainly uses is the animation, uses is the translate movement animation, here I did not introduce, next I bar animation code to post, in Res creates a Anim directory, the following figure

Left into the animation, Left_in.xml

<?xml version= "1.0" encoding= "UTF-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
  <translate  
    android:fromxdelta=" -100%p " 
    android:toxdelta=" 0 " 
    android:duration=" "/> 
</set> 

Left out of the animation, Left_out.xml

<?xml version= "1.0" encoding= "UTF-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
  <translate  
    android:fromxdelta=" 0 " 
    android:toxdelta=" -100%p " 
    android: Duration= "/>" 
</set> 

Right into the animation, Right_in.xml

<?xml version= "1.0" encoding= "UTF-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
  <translate  
    android:fromxdelta=" 100%p " 
    android:toxdelta=" 0 " 
    android:duration=" "/> 
</set> 

Right out of the animation, Right_out.xml

<?xml version= "1.0" encoding= "UTF-8"?> <set xmlns:android= 
"http://schemas.android.com/apk/res/" Android "> 
  <translate  
    android:fromxdelta=" 0 " 
    android:toxdelta=" 100%p " 
    android: Duration= "/>" 
</set> 

Well, the introduction is over, the code is not very good, write the bad place hope everyone understanding, small series must work harder.

Related Article

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.