首页  »   Android

Android中viewPager兑现欢迎引导

网友分享于:2013-08-11  浏览:196次
Android中viewPager实现欢迎引导

public class MainActivity extends Activity implements OnClickListener, OnPageChangeListener{
	 private ViewPager vp;
	    private ViewPagerAdapter vpAdapter;
	    private List<View> views;
	    private int lastValue = -1;
	    private Bitmap bitmap;
	    
	    //引导图片资源
	    private static final int[] pics = { R.drawable.guide1,
	            R.drawable.guide2, R.drawable.guide3,
	            R.drawable.guide4,R.drawable.guide5 };
	    
	    //底部小店图片
	    private ImageView[] dots ;
	    
	    //记录当前选中位置
	    private int currentIndex;
	    
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.main);
	        
	        views = new ArrayList<View>();
	       
	        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
	                LinearLayout.LayoutParams.WRAP_CONTENT);
	        
	        //初始化引导图片列表
	        for(int i=0; i<pics.length; i++) {
	            ImageView iv = new ImageView(this);
	            iv.setLayoutParams(mParams);
	            iv.setImageBitmap(getBitmap(pics[i]));
	            views.add(iv);
	        }
	        vp = (ViewPager) findViewById(R.id.viewpager);
	        //初始化Adapter
	        vpAdapter = new ViewPagerAdapter(views);
	        vp.setAdapter(vpAdapter);
	        //绑定回调
	        vp.setOnPageChangeListener(this);
	        
	        //初始化底部小点
	        initDots();
	        
	    }
	    /**
	     * 以最小内存获取资源图片
	     * @param resId
	     * @return
	     */
	    private Bitmap getBitmap(int resId){
	    	BitmapFactory.Options opt = new BitmapFactory.Options();
	    	opt.inPreferredConfig = Bitmap.Config.RGB_565;
	    	opt.inPurgeable = true;
	    	opt.inInputShareable = true;
	    	InputStream is = this.getResources().openRawResource(resId);
	    	bitmap = BitmapFactory.decodeStream(is, null, opt);
	    	return bitmap;
	    }
	    private void initDots() {
	        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);

	        dots = new ImageView[pics.length];

	        //循环取得小点图片
	        for (int i = 0; i < pics.length; i++) {
	            dots[i] = (ImageView) ll.getChildAt(i);
	            dots[i].setEnabled(true);//都设为灰色
	            dots[i].setOnClickListener(this);
	            dots[i].setTag(i);//设置位置tag,方便取出与当前位置对应
	        }

	        currentIndex = 0;
	        dots[currentIndex].setEnabled(false);//设置为白色,即选中状态
	    }
	    
	    /**
	     *设置当前的引导页 
	     */
	    private void setCurView(int position)
	    {
	        if (position < 0 || position >= pics.length) {
	            return;
	        }

	        vp.setCurrentItem(position);
	    }

	    /**
	     *这只当前引导小点的选中 
	     */
	    private void setCurDot(int positon)
	    {
	        if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
	            return;
	        }

	        dots[positon].setEnabled(false);
	        dots[currentIndex].setEnabled(true);

	        currentIndex = positon;
	    }

	    //当滑动状态改变时调用
	    @Override
	    public void onPageScrollStateChanged(int arg0) {
          if(arg0 == 0){
        	   if(lastValue == pics.length-1){
        		   Toast.makeText(this, "已经是最后一张了", Toast.LENGTH_SHORT).show();
        	   }
           }	        
	    }

	    //当当前页面被滑动时调用
	    @Override
	    public void onPageScrolled(int arg0, float arg1, int arg2) {
	    	lastValue = arg0;
	    }

	    //当新的页面被选中时调用
	    @Override
	    public void onPageSelected(int arg0) {
	        //设置底部小点选中状态
	        setCurDot(arg0);
	    }

	    @Override
	    public void onClick(View v) {
	        int position = (Integer)v.getTag();
	        setCurView(position);
	        setCurDot(position);
	    }
}





public class ViewPagerAdapter extends PagerAdapter {

	 //界面列表
    private List<View> views;
    
    public ViewPagerAdapter (List<View> views){
        this.views = views;
    }

    //销毁arg1位置的界面
    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView(views.get(arg1));        
    }

    @Override
    public void finishUpdate(View arg0) {
        // TODO Auto-generated method stub
        
    }

    //获得当前界面数
    @Override
    public int getCount() {
        if (views != null)
        {
            return views.size();
        }
        
        return 0;
    }
    

    //初始化arg1位置的界面
    @Override
    public Object instantiateItem(View arg0, int arg1) {
        
        ((ViewPager) arg0).addView(views.get(arg1), 0);
        
        return views.get(arg1);
    }

    //判断是否由对象生成界面
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return (arg0 == arg1);
    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
        // TODO Auto-generated method stub
        
    }

}
 

相关解决方案

最新解决方案