经常在开发应用程序的时候,软件启动有启用动画界面,一般使用图片来进行界面该版本最新更新等等内容,今天来使用ViewPager来模拟实现这功能,也希望对大家有个小小的参考作用,在以后的项目中能够使用到。
首先准备工作,若干张需要进行显示的图片和底部用到的小圆圈
项目结构图如下:
2:运行效果图
大致效果如上,具体编码实现:
布局文件是android.support.v4.view.ViewPager和底部五个小圆圈main.xml
ViewPageAdapter.java ViewPager页面的适配器
/** * 自定义ViewPager适配器 * @author jiangqingqing * */ public class MyViewPagerAdapter extends PagerAdapter { private ListMainActicvity.javamLists; public MyViewPagerAdapter(List pLists) { this.mLists=pLists; } @Override public int getCount() { // TODO Auto-generated method stub return mLists.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return arg0==arg1; } @Override public void destroyItem(View container, int position, Object object) { ((ViewPager)container).removeView(mLists.get(position)); } @Override public Object instantiateItem(View container, int position) { ((ViewPager)container).addView(mLists.get(position)); return mLists.get(position); }
/** * 使用ViePager简单模拟 应用的启动动画界面 * @author jiangqingqing * */ public class MainActivity extends Activity { private ViewPager mViewPager; private MyViewPagerAdapter mAdapter; private LinearLayout mLinearLayout; private List这个Demo例子比较简单,但是可以实现我们需要的效果。详细代码在下面的链接,有兴趣的可以下载下来跑一下mLists = new ArrayList (); private Integer[] img_ids = new Integer[] { R.drawable.guide_01, R.drawable.guide_02, R.drawable.guide_03, R.drawable.guide_04, R.drawable.guide_05 }; private ImageView[] points = new ImageView[img_ids.length]; private int current=0;//默认在第一页 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mViewPager = (ViewPager) this.findViewById(R.id.myViewpager); mLinearLayout = (LinearLayout) this.findViewById(R.id.linearlayout); //把需要显示的图片加入到ViewPager页面集合中 for (int i = 0; i < img_ids.length; i++) { ImageView iv = new ImageView(this); iv.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); iv.setImageResource(img_ids[i]); mLists.add(iv); } mAdapter=new MyViewPagerAdapter(mLists); mViewPager.setAdapter(mAdapter); //为ViewPager选项卡添加 切换时候的监听 mViewPager.setOnPageChangeListener(new MyOnPagerChangeListener()); //初始化下面的小圆点 initPoints(); } //初始化下面的小圆点 private void initPoints() { for(int i=0;i
http://download.csdn.net/detail/jiangqq781931404/5776739