Android viewpager image flip example,

Source: Internet
Author: User
Tags image flip android sdk manager

Android viewpager image flip example,

You can use the ViewPager class to easily slide multiple pages.

Viewpager is not found on the tool interface by default, the need to add a android-support-v4.jar package;

If not found, open the Android SDK Manager to download it in Extras. Remember to introduce the project and build path.

(However, If I COPY the Code directly to. XML, it will be automatically added)

 

Example Description: item01.. item08 is an image to flip pages. page_indicator_focused, page_indicator_unfocused is a dot picture displayed at the bottom. You must add it to res \ drawable-hdpi;

. XML

Code download: http://pan.baidu.com/s/1mgA7MBY

 1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     android:layout_width="fill_parent" 3     android:layout_height="fill_parent" 4     android:orientation="vertical" > 5  6     <android.support.v4.view.ViewPager 7         android:id="@+id/viewPager" 8         android:layout_width="fill_parent" 9         android:layout_height="wrap_content" />10 11     <RelativeLayout12         android:layout_width="fill_parent"13         android:layout_height="wrap_content"14         android:orientation="vertical" >15 16         <LinearLayout17             android:id="@+id/viewGroup"18             android:layout_width="fill_parent"19             android:layout_height="wrap_content"20             android:layout_alignParentBottom="true"21             android:layout_marginBottom="30dp"22             android:gravity="center_horizontal"23             android:orientation="horizontal" >24         </LinearLayout>25     </RelativeLayout>26 27 </FrameLayout>

. JAVA

1 package com. example. testa; 2 3 import android. support. v4.view. pagerAdapter; 4 import android. support. v4.view. viewPager; 5 import android. support. v4.view. viewPager. onPageChangeListener; 6 import android. app. activity; 7 import android. OS. bundle; 8 import android. view. view; 9 import android. view. viewGroup; 10 import android. view. viewGroup. layoutParams; 11 import android. widget. imageView; 12 import android. widget. linearLayout; 13 14 public class MainActivity extends Activity implements OnPageChangeListener {15 16/** 17 * ViewPager 18 */19 private ViewPager viewPager; 20 21/** 22 * Installation Point ImageView array 23 */24 private ImageView [] tips; 25 26/** 27 * install ImageView array 28 */29 private ImageView [] mImageViews; 30 31/** 32 * image resource id 33 */34 private int [] imgIdArray; 35 36 @ Override 37 protected void onCreate (Bundle savedInstanceState) {38 super. onCreate (savedInstanceState); 39 setContentView (R. layout. activity_main); 40 ViewGroup group = (ViewGroup) findViewById (R. id. viewGroup); 41 viewPager = (ViewPager) findViewById (R. id. viewPager); 42 43 // load image resource ID 44 imgIdArray = new int [] {R. drawable. item01, R. drawable. item02, 45 R. drawable. item03, R. drawable. item04, R. drawable. item05, 46 R. drawable. item06, R. drawable. item07, R. drawable. item08}; 47 48 // Add the dot to the ViewGroup 49 tips = new ImageView [imgIdArray. length]; 50 for (int I = 0; I <tips. length; I ++) {51 ImageView imageView = new ImageView (this); 52 imageView. setLayoutParams (new LayoutParams (10, 10); 53 tips [I] = imageView; 54 if (I = 0) {55 tips [I]. setBackgroundResource (R. drawable. page_indicator_focused); 56} else {57 tips [I]. setBackgroundResource (R. drawable. page_indicator_unfocused); 58} 59 60 LinearLayout. layoutParams layoutParams = new LinearLayout. layoutParams (61 new ViewGroup. layoutParams (LayoutParams. WRAP_CONTENT, 62 LayoutParams. WRAP_CONTENT); 63 layoutParams. leftMargin = 5; 64 layoutParams. rightMargin = 5; 65 group. addView (imageView, layoutParams); 66} 67 68 // load the image to the array 69 mImageViews = new ImageView [imgIdArray. length]; 70 for (int I = 0; I <mImageViews. length; I ++) {71 ImageView imageView = new ImageView (this); 72 mImageViews [I] = imageView; 73 imageView. setBackgroundResource (imgIdArray [I]); 74} 75 76 // set Adapter 77 viewPager. setAdapter (new MyAdapter (); 78 // set the listener, mainly set the background of the dot. 79 viewPager. setOnPageChangeListener (this); 80 // you can set the default value of ViewPager to 100 times the length, so that you can move 81 viewPager to the left. setCurrentItem (mImageViews. length) * 100 ); 82 83} 84 85/** 86*87 * @ author xiaanming 88*89 */90 public class MyAdapter extends PagerAdapter {91 92 @ Override 93 public int getCount () {94 return Integer. MAX_VALUE; 95} 96 97 @ Override 98 public boolean isViewFromObject (View arg0, Object arg1) {99 return arg0 = arg1; 100} 101 102 @ Override103 public void destroyItem (View container, int position, Object object) {104} 105 106/** 107 * load the image, dividing the current position by the length of the Image array to obtain the remainder is critical: 108 */109 @ Override110 public Object instantiateItem (View container, int position) {111 try {112 (ViewPager) container ). addView (mImageViews [position % mImageViews. length], 0); 113} catch (Exception e) {114 // handler something 115} 116 return mImageViews [position % mImageViews. length]; 117 118} 119 120} 121 122 @ Override123 public void onPageScrollStateChanged (int arg0) {124 125} 126 127 @ Override128 public void onPageScrolled (int arg0, float arg1, int arg2) {129 130} 131 132 @ overridemo-public void onPageSelected (int arg0) {134 setImageBackground (arg0% mImageViews. length); 135} 136 137/** 138 * set the background of the selected tip 139*140 * @ param selectItems141 */142 private void setImageBackground (int selectItems) {143 for (int I = 0; I <tips. length; I ++) {144 if (I = selectItems) {145 tips [I]. setBackgroundResource (R. drawable. page_indicator_focused); 146} else {147 tips [I]. setBackgroundResource (R. drawable. page_indicator_unfocused); 148} 149} 150 151}

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.