标签:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="wrap_content" 4 android:layout_height="wrap_content" 5 tools:context=".MainActivity" > 6 7 <android.support.v4.view.ViewPager 8 android:id="@+id/id_viewPager" 9 android:layout_width="match_parent" 10 android:layout_height="match_parent" > 11 </android.support.v4.view.ViewPager> 12 13 </RelativeLayout>
后台代码:
1 package org.dream.android_001; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.app.Activity; 7 import android.os.Bundle; 8 import android.support.v4.view.PagerAdapter; 9 import android.support.v4.view.ViewPager; 10 import android.view.View; 11 import android.view.ViewGroup; 12 import android.widget.ImageView; 13 import android.widget.ImageView.ScaleType; 14 15 public class MainActivity extends Activity { 16 private ViewPager viewPager; 17 18 /** 19 * 本地图片资源id 20 * Assets与res的区别 21 * 1、assets文件夹下文件不会被映射到R.java文件中,res文件夹下的文件会 22 * 2、assets可以有子目录,res不可以 23 */ 24 private int[] mImgIds = new int[] { R.drawable.guide_image1, 25 R.drawable.guide_image2, R.drawable.guide_image3 }; 26 27 /** 28 * 用于存储ImageView列表,并用于销毁等一系列操作 29 */ 30 private List<ImageView> mImgs = new ArrayList<ImageView>(); 31 32 @Override 33 protected void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 setContentView(R.layout.activity_main); 36 viewPager = (ViewPager) findViewById(R.id.id_viewPager); 37 viewPager.setAdapter(new PagerAdapter() { 38 39 // 实例化Item 40 @Override 41 public Object instantiateItem(ViewGroup container, int position) { 42 ImageView imageView = new ImageView(MainActivity.this); 43 imageView.setImageResource(mImgIds[position]); 44 imageView.setScaleType(ScaleType.CENTER_CROP); 45 container.addView(imageView); 46 mImgs.add(imageView); 47 return imageView; 48 }; 49 50 @Override 51 public void destroyItem(ViewGroup container, int position, 52 Object object) { 53 container.removeView(mImgs.get(position)); 54 }; 55 56 @Override 57 public boolean isViewFromObject(View view, Object obj) { 58 return view == obj; 59 } 60 61 @Override 62 public int getCount() { 63 return mImgIds.length; 64 } 65 }); 66 } 67 }
可用于welcome引导页面
标签:
原文地址:http://www.cnblogs.com/fb-boy/p/4301838.html