一款通用的欢迎页,可以有多张图片

自定义欢迎页张数,带应到点。多的不说了,直接上代码。


效果大家可以自己试一下,


import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

/**
 * @项目名称:	Make_wish
 * @类名称: 	Welcome_Activity.java
 * @创建人:	Mr.ladeng ([email protected])
 * @创建时间: 2015-5-11下午8:09:42
 * @修改备注:    
 * @version 1.0   
 * @类描述:引导欢迎页面,首次安装会进入这个界面
 */
public class Welcome_Activity extends BaseActivity {
	
	private TextView wel_jump;
	
	private ImageView tv;
	private ViewPager vp;// 页卡内容
	private List<ImageView> datas; // Tab页面列表

	private LinearLayout ll_points;
	
	//ArrayList<ImagePage>  imageP ;

	private static final String SHAREDPREFERENCES_NAME = "first_pref";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		PushAgent.getInstance(this).onAppStart();
		
		// 设置全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE); // 无title
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); // 全屏

		setContentView(R.layout.welcome_activity);
		
		(new GetWelcomeImg()).execute();
		//Bundle bundle=getIntent().getExtras();
		//imageP =bundle.getParcelableArrayList("img");
		// 初始化
		initView();

	}

	private void initView() {
		
		wel_jump = (TextView) findViewById(R.id.wel_jump);
		
		tv = (ImageView) findViewById(R.id.wel_tv);
		vp = (ViewPager) findViewById(R.id.vp);
		ll_points = (LinearLayout) findViewById(R.id.ll_points);

		initDatas(imageP);
		vp.setOffscreenPageLimit(3);
		vp.setAdapter(new MyPagerAdapter());
		setListener();
		
	}

	/**	未选中	*/
	private int prePosition = 0;
	@SuppressWarnings("deprecation")
	private void setListener() {
		/** 设置小圆点**/
		vp.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				
				ll_points.getChildAt(position).setBackgroundResource(R.drawable.wel_dot_enable);//选中背景色
				ll_points.getChildAt(prePosition).setBackgroundResource(R.drawable.wel_normal);//未选中背景色
				
				/**	选中	*/
				prePosition = position;
				/**
				 * 设置tv可见 当position为第4个图片的时候激活下面的方法 其他时候都是不可见的
				 * 下面一个动画 ,在最后一个页面的时候可以通过动画跳转到主界面
				 */
				if (position == datas.size() - 1) {
					
					wel_jump.setVisibility(View.VISIBLE);//跳过
					
					
				}else {
					wel_jump.setVisibility(View.GONE);//跳过
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		
		/**
		 * 设置点击跳转
		 */
		wel_jump.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setGuided();
				openActivity(LoginActivity.class);
				finish();
			}
		});
			
		tv.setOnClickListener(new OnClickListener() {
			@Override
			// 点击方法
			public void onClick(View v) {
				setGuided();
				openActivity(LoginActivity.class);
				finish();
			}
		});
		
	}
	
	
	
	/**
	 * 
	 * method desc:设置已经引导过了,下次启动不用再次引导
	 */
	private void setGuided(){
		SharedPreferences preferences = getSharedPreferences( SHAREDPREFERENCES_NAME, MODE_PRIVATE);
		Editor editor = preferences.edit();
		// 存入数据
		editor.putBoolean("isFirstIn", false);
		// 提交修改
		editor.commit();
	}


	// 将图片添加到界面上
	private void initDatas(ArrayList<ImagePage> imageP) {
		datas = new ArrayList<ImageView>();
			for (int i = 0; i < imageP.size() ; i++) {
			
			SimpleDraweeView iv = new SimpleDraweeView(Welcome_Activity.this);
			
			FrescoUtils.loadImageStart( iv , imageP.get(i).imgs);
			
			datas.add(iv);

			int px2dp = ScreenUtils.px2dp(getApplicationContext(), 30);
			int screenWidth = ScreenUtils.getScreenWidth(getApplicationContext());
			if (screenWidth>=1080) {
				px2dp = 30 ;
			}
			
			View view = new View(this);
			// 为这个view设置背景 未选中状态背景色
			view.setBackgroundResource(R.drawable.wel_normal); 
			// 定义布局参数,并给view设置参数(宽,高)
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(px2dp , px2dp);
			params.leftMargin = px2dp ;
			view.setLayoutParams(params);
			// 将定义好的view加入到线性布局中
			ll_points.addView(view);
		}
		ll_points.getChildAt(0).setBackgroundResource(R.drawable.wel_dot_enable);//选中状态背景色

	}

	private class MyPagerAdapter extends PagerAdapter {
		// viewPager的页数
		@Override
		public int getCount() {
			return datas.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object obj) {
			return view == obj;
		}

		// 初始化条目
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// 必须要加入到ViewGroup中
			container.addView(datas.get(position));
			return datas.get(position);
		}

		// 从ViewPager中移除
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(datas.get(position));
		}
	}
	
	/********* 	欢迎页  start******** */
	class GetWelcomeImg extends AsyncTask<Void, Integer, Integer> {

		private String flag;
		ArrayList<ImagePage>  imageP ;

		GetWelcomeImg() {
			imageP = new ArrayList<ImagePage> ();
		}

		@Override
		protected void onPreExecute() {
		}

		@Override
		protected Integer doInBackground(Void... params) {
			JSONObject jsonObject = JsonUtils.ReadHttpGet(Constant.WelcomeImg);
			Integer result = 0;
			
			if (null != jsonObject) {
				try {
					flag = jsonObject.getString("flag").toString().trim();
					if (flag.equals("1")) {
						JSONArray jsonArray = new JSONArray(jsonObject.getString("data"));
						ImagePage imagePage ;
						for (int i = 0; i < jsonArray.length(); i++) {
							imagePage = new ImagePage();
							JSONObject temp = (JSONObject) jsonArray.get(i);
							imagePage.imgs = temp.getString("pic");
							imageP.add(imagePage);
						}
						result = 1;
					}else {
						result = 0;
					}
				} catch (JSONException e) {
					e.printStackTrace();
					result = 0;
				}
			}
			return result;
		}

		@Override
		protected void onPostExecute(Integer result) {
			// over do somethhing		
			if (1 == result) {
				initDatas(imageP);
				vp.setOffscreenPageLimit(3);
				vp.setAdapter(new MyPagerAdapter());
				setListener();
			}else {
				openActivity(LoginActivity.class);
				finish();
			}
			super.onPostExecute(result);
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
		}
	}
	
	class ImagePage {
		public String imgs ;
	}
 
	/** 异步任务 end ******** */

	
	/****************** 欢迎页##### *********************/

	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}


布局代码


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Welcome_Activity" >

    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- alpha  是透明度(范围0-1),,visibility设置是否可见(gone为不可见,visible可见) -->

    <LinearLayout
        android:id="@+id/ll_points"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/vp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:gravity="center"
        android:orientation="horizontal" />
    
    <ImageView
        android:id="@+id/wel_tv"
        android:layout_width="100dp"
        android:layout_height="20dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="15dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/jumpto"
        android:gravity="center"
        android:visibility="visible" />

    <TextView
        android:id="@+id/wel_jump"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/ll_points"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="21dp"
        android:background="@drawable/jump_tv"
        android:gravity="center"
        android:padding="10dp"
        android:textColor="@color/white"
        android:text="立即体验"
        android:visibility="gone" />

</RelativeLayout>


需要图片资源



你可能感兴趣的:(欢迎页)