RyMiniFramework(4)-引导界面

由于引导界面处于欢迎界面和主界面之间, 所以,最后还是把引导界面的实现放在了前面写好的RmfSplash类中。

RMF中引导界面是用gallery来实现,当处于最后一张图片时,向右滑动即可进入主界面。

 

1、cn.rydiy.rmf.common.RmfGuide.java

package cn.rydiy.rmf.common;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

/**
 * Guide Display Class
 * 
 * @author Rydiy
 * @version 1.0.0
 * @Date 2012-07-30
 */
public class RmfGuide {
	private ArrayList<Integer> mImgList; // guide images' id
	private RmfGallery mGallery; // guide gallery
	private Resources mRes; // Resources
	private Activity mAct; // MainActivity
	private int mCurPosition; //gallery current page

	/**
	 * Constructor
	 * 
	 * @param imgs
	 *            : A array of guide-images' id
	 * @param act
	 *            : your object's main activity's instance
	 * @param res
	 *            : A Resources instance for your application's package
	 */
	public RmfGuide(ArrayList<Integer> imgs, Activity act, Resources res) {
		mImgList = imgs;
		mAct = act;
		mRes = res;
		mCurPosition = 0;
	}

	/**
	 * Show guide
	 */
	public void show() {
		//get layout
		LinearLayout layout = (LinearLayout) mAct
				.findViewById(mRes.getIdentifier("rmf_id_splash_ly", "id",
						mAct.getPackageName()));
		mGallery = new RmfGallery(mAct);
		ImageAdapter imgAdapter = new ImageAdapter(mAct);
		mGallery.setAdapter(imgAdapter);
		//add gallery to layout
		layout.removeAllViews();
		layout.addView(mGallery, new LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT));
	}

	/**
	 * Custom gallery
	 * @author Rydiy
	 * 
	 */
	private class RmfGallery extends Gallery {
		public RmfGallery(Context context) {
			super(context);
		}

		public RmfGallery(Context context, AttributeSet attrs) {
			super(context, attrs);
		}

		private boolean isScrollingLeft(MotionEvent e1, MotionEvent e2) {
			return e2.getX() > e1.getX();
		}

		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			int kEvent;
			if (isScrollingLeft(e1, e2)) {
				// Check if scrolling left
				kEvent = KeyEvent.KEYCODE_DPAD_LEFT;
			} else {
				// Otherwise scrolling right
				kEvent = KeyEvent.KEYCODE_DPAD_RIGHT;

				//if it is the last page, load main layout
				if (mCurPosition == getSelectedItemPosition()
						&& mCurPosition == getCount() - 1) {
					mAct.setContentView(mRes.getIdentifier("main", "layout",
							mAct.getPackageName()));
				} else {
					mCurPosition = getSelectedItemPosition();
				}

			}

			onKeyDown(kEvent, null);
			return true;
		}

	}

	/**
	 * ImageAdapter for gallery
	 * @author Rydiy
	 * 
	 */
	private class ImageAdapter extends BaseAdapter {
		private Context mContext = null;

		public ImageAdapter(Context c) {
			this.mContext = c;
		}

		public int getCount() {
			return mImgList.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView mImageView = new ImageView(mContext);
			int id = mImgList.get(position);
			Bitmap bmp = BitmapFactory.decodeResource(mRes, id);
			mImageView.setImageBitmap(bmp);
			mImageView.setScaleType(ImageView.ScaleType.FIT_XY);
			mImageView.setLayoutParams(new android.widget.Gallery.LayoutParams(
					android.widget.Gallery.LayoutParams.FILL_PARENT,
					android.widget.Gallery.LayoutParams.FILL_PARENT));
			return mImageView;
		}
	}
}


 

2、cn.rydiy.rmf.common.RmfSplash.java修改如下:

package cn.rydiy.rmf.common;

import java.util.ArrayList;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

/**
 * Splash Display Class. It will display splash with full screen and then quit
 * full screen.
 * 
 * @author Rydiy
 * @version 1.0.0
 * @Date 2012-07-29
 */
public class RmfSplash {
	private Activity mActivity; // MainActivity
	private int mDelayTime; // delay time
	private int mMainLayoutResID; // /the resource id of the main layout
	private int mSplashLayoutResID; // /the resource id of the splash layout
	private int mSplashImgViewResID; // /the resource id of the splash imageview
	private int mSplashImgResID; // the resource id of the splash image data
	private ArrayList<Integer> mGuideImgs; // imags for guide
	private Handler mHandler; // message handler
	private Resources mRes; // the resources object containing the image data

	/**
	 * Constructor
	 * 
	 * @param mainActivity
	 *            :Application's main acitivity
	 */
	public RmfSplash(Activity mainActivity) {
		this.mActivity = mainActivity;
	}

	/**
	 * Set splash settings
	 * @param res
	 *            :The resources object containing the image data
	 * @param splashImgResID
	 *            :The resource id of the image data
	 */
	public void setSplash(Resources res, int splashImgResID) {
		this.mRes = res;
		String packageName = mActivity.getPackageName();
		// get R.layout.main
		mMainLayoutResID = mRes.getIdentifier("main", "layout", packageName);
		setSplash(res, splashImgResID, mMainLayoutResID);
	}

	/**
	 * Set splash settings
	 * @param res
	 *            :The resources object containing the image data
	 * @param splashImgResID
	 *            :The resource id of the image data
	 * @param mainLayoutResID
	 *            :The resource id of the application's main layout
	 */
	public void setSplash(Resources res, int splashImgResID, int mainLayoutResID) {
		this.mRes = res;
		this.mSplashImgResID = splashImgResID;
		this.mMainLayoutResID = mainLayoutResID;

		String packageName = mActivity.getPackageName();
		// get R.layout.rmf_ly_splash
		mSplashLayoutResID = mRes.getIdentifier("rmf_ly_splash", "layout",
				packageName);
		// get R.id.rmf_id_splash_img
		mSplashImgViewResID = mRes.getIdentifier("rmf_id_splash_img", "id",
				packageName);
		// create Handler
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				if (1 == msg.arg1) {
					Log.d("RMF", "RmfSplash recive handler");
					// quit full screen
					quiteFullScreen();
					// show guideView
					if (null == mGuideImgs) {
						mActivity.setContentView(mMainLayoutResID);
					} else {
						showGuide();
					}
				}
			}
		};
	}

	/**
	 * Show splash and guide
	 * @param delayTime
	 *            : Time for splash display
	 */
	public void show(int delayTime) {
		Log.d("RMF", "RmfSplash show()");
		this.mDelayTime = delayTime;
		// no title
		mActivity.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// full screen
		mActivity.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// set splash layout
		mActivity.setContentView(mSplashLayoutResID);

		// load bitmap
		Bitmap bmp = BitmapFactory.decodeResource(mActivity.getResources(),
				mSplashImgResID);
		ImageView img = (ImageView) mActivity.findViewById(mSplashImgViewResID);
		img.setImageBitmap(bmp);

		// delay time
		new Thread() {
			public void run() {
				Message msg = Message.obtain(mHandler);
				msg.arg1 = 1;
				mHandler.sendMessageDelayed(msg, mDelayTime);
			};
		}.start();
	}

	/**
	 * Set images for guide
	 * @param guideImgs
	 *            :Images for guide
	 */
	public void setGuide(ArrayList<Integer> guideImgs) {
		mGuideImgs = guideImgs;
	}

	/**
	 * Quite full screen
	 */
	private void quiteFullScreen() {
		final WindowManager.LayoutParams attrs = mActivity.getWindow()
				.getAttributes();
		attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mActivity.getWindow().setAttributes(attrs);
		mActivity.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
	}

	/**
	 * Show guide view
	 */
	private void showGuide() {
		SharedPreferences appInfo = mActivity.getPreferences(0);
		Boolean first = appInfo.getBoolean("first", true);
		if (first) {
			// first time, show guide
			appInfo.edit().putBoolean("first", false).commit();
			RmfGuide rmfGuide = new RmfGuide(mGuideImgs, mActivity, mRes);
			rmfGuide.show();
		} else {
			// Not the first time, set main layout
			mActivity.setContentView(mMainLayoutResID);
		}
	}
}


3、调用

 

       Log.d("RMF", "OnCreate");

        //new RmfSplash
        RmfSplash rSplash = new RmfSplash(this);
        
        //imgs for guide
        ArrayList<Integer> imgs = new ArrayList<Integer>();
        imgs.add(R.drawable.feature_guide_0);
        imgs.add(R.drawable.feature_guide_1);
        imgs.add(R.drawable.feature_guide_2);
        imgs.add(R.drawable.feature_guide_3);
        //set imgs to RmfSplash
        rSplash.setGuide(imgs);

        //set splash settings
        //rSplash.setSplash(getResources(), R.drawable.rmf_img_splash);      
        rSplash.setSplash(getResources(), R.drawable.rmf_img_splash, R.layout.rmf_ly_tabmain);
        
        //show splash and guide
        rSplash.show(3000);

 

现在的版本可以指定加载的主layout,  默认为main.layout

你可能感兴趣的:(RyMiniFramework(4)-引导界面)