Android應用第一次開啟,引導介面的實現,android介面
轉載請註明出處,謝謝http://blog.csdn.net/harryweasley/article/details/42079167
先說下思路:1.利用Preference儲存資料,來記錄是否是第一次開啟軟體
2.利用ViewPager實現幾個圖片之間的切換,在每個圖片下方用代碼畫圓圈,圓圈會跟著圖片的改變而改變。
3.在最後一張圖片,添加button點擊事件,進入正式介面。
程式雖然很簡單,但是很實用。
看下:
我們會看到圓圈的點會根據圖片改變而改變。
下面開始講解:
首先是activity_main.xml檔案
<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=".MainActivity" > <android.support.v4.view.ViewPager android:id="@+id/guide_viewPager" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/transparent" /> <LinearLayout android:id="@+id/linearlayout" android:layout_width="match_parent" android:layout_height="30dp" android:layout_alignParentBottom="true" android:gravity="center_horizontal" android:orientation="horizontal" > </LinearLayout> </RelativeLayout>
裡面有一個viewPager控制項和LinearLayout控制項,其中LinearLayout是裝小圓圈的
接下來是三個pager_layout1.xml,pager_layout2.xml,pager_layout3.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="@drawable/guide1" android:orientation="vertical" > </LinearLayout>
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide2" android:orientation="vertical" > </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/guide3" android:gravity="center" android:orientation="vertical" > <Button android:id="@+id/guide_enter" android:layout_width="match_parent" android:layout_height="100dp" android:layout_gravity="center_horizontal|bottom" android:layout_marginBottom="0dp" android:background="@android:color/transparent" /></FrameLayout>
我們可以看到前面兩個layout就直接是個圖片做背景,第三個我只是加了一個透明的按鈕,模仿下面的“立即進入”按鈕。(在這裡,如果你有更好的方法,可以分享一下哈)
接下來是PreferenceUtil類,儲存isShow的值
package com.example.guiddemo;import android.content.Context;import android.content.SharedPreferences;public class PreferenceUtil {/** * 是否顯示歡迎介面,true表示顯示,false表示不顯示 */ public static final String SHOW_GUIDE = "showguide";/** * 儲存到Preference */public static void setBoolean(Context context, String key, boolean value) {// 得到SharedPreferencesSharedPreferences preferences = context.getSharedPreferences("preference", Context.MODE_PRIVATE);SharedPreferences.Editor editor = preferences.edit();editor.putBoolean(key, value);editor.commit();}/** * 從Preference取出資料 */public static boolean getBoolean(Context context, String key) {SharedPreferences preferences = context.getSharedPreferences("preference", Context.MODE_PRIVATE);// 返回key值,key值預設值是falsereturn preferences.getBoolean(key, false);}}
關於Preference儲存和取出資料這些操作,我想我不用多說了吧。
主要功能都是在MainActivity裡,My Code裡寫了詳細的注釋的
package com.example.guiddemo;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Intent;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.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup.LayoutParams;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends Activity {/** * 是否顯示引導介面 */boolean isShow = false;/** * ViewPager對象 */private ViewPager mViewPager;/** * 裝載小圓圈的LinearLayout */private LinearLayout indicatorLayout;/** * ViewPager的每個頁面集合 */private List<View> views;/** * ViewPager下面的小圓圈 */private ImageView[] mImageViews;/** * PagerAdapter對象 */private MyPagerAdapter myPagerAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 得到Preference儲存的isShow資料isShow = PreferenceUtil.getBoolean(this, PreferenceUtil.SHOW_GUIDE);//isShow=false;調試的時候用的if (isShow) {initLog();} else {initView();}}/** * 進入登入介面 */private void initLog() {startActivity(new Intent(this, LogActivity.class));finish();}/** * 進入引導介面 */private void initView() {mViewPager = (ViewPager) findViewById(R.id.guide_viewPager);indicatorLayout = (LinearLayout) findViewById(R.id.linearlayout);LayoutInflater inflater = LayoutInflater.from(this);views = new ArrayList<View>();views.add(inflater.inflate(R.layout.pager_layout1, null));views.add(inflater.inflate(R.layout.pager_layout2, null));views.add(inflater.inflate(R.layout.pager_layout3, null));myPagerAdapter = new MyPagerAdapter(this, views);mImageViews = new ImageView[views.size()];drawCircl();mViewPager.setAdapter(myPagerAdapter);mViewPager.setOnPageChangeListener(new GuidePageChangeListener());}/** * 畫圓圈 */private void drawCircl() {int num = views.size();for (int i = 0; i < num; i++) {//執行個體化每一個mImageViews[i]mImageViews[i] = new ImageView(this);if (i == 0) {// 預設選中第一張照片,所以將第一個小圓圈變為icon_carousel_02mImageViews[i].setImageResource(R.drawable.icon_carousel_02);} else {mImageViews[i].setImageResource(R.drawable.icon_carousel_01);}// 給每個小圓圈都設定間隔mImageViews[i].setPadding(7, 7, 7, 7);LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);params.gravity = Gravity.CENTER_VERTICAL;// 讓每一個小圓圈都在LinearLayout的CENTER_VERTICAL(中間垂直)indicatorLayout.addView(mImageViews[i], params);}}/** * * @author Harry 頁面改變監聽事件 */private class GuidePageChangeListener implements OnPageChangeListener {public void onPageScrollStateChanged(int arg0) {}public void onPageScrolled(int arg0, float arg1, int arg2) {}/** * 頁面有所改變,如果是當前頁面,將小圓圈改為icon_carousel_02,其他頁面則改為icon_carousel_01 */public void onPageSelected(int arg0) {for (int i = 0; i < mImageViews.length; i++) {if (arg0 != i) {mImageViews[i].setImageResource(R.drawable.icon_carousel_01);} else {mImageViews[arg0].setImageResource(R.drawable.icon_carousel_02);}}}}class MyPagerAdapter extends PagerAdapter {private List<View> mViews;private Activity mContext;public MyPagerAdapter(Activity context, List<View> views) {this.mViews = views;this.mContext = context;}@Overridepublic int getCount() {return mViews.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}@Overridepublic int getItemPosition(Object object) {return super.getItemPosition(object);}@Overridepublic void destroyItem(View arg0, int arg1, Object arg2) {((ViewPager) arg0).removeView(mViews.get(arg1));}/** * 執行個體化頁卡,如果變為最後一頁,則擷取它的button並且添加點擊事件 */@Overridepublic Object instantiateItem(View arg0, int arg1) {((ViewPager) arg0).addView(mViews.get(arg1), 0);if (arg1 == mViews.size() - 1) {TextView enterBtn = (TextView) arg0.findViewById(R.id.guide_enter);enterBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 將isShow儲存為true,並進入登入介面PreferenceUtil.setBoolean(mContext,PreferenceUtil.SHOW_GUIDE, true);initLog();}});}return mViews.get(arg1);}}}
主要代碼我就寫完了,雖然比較簡單,但是卻很實用,希望協助到大家。
關於ViewPager相關知識,大家可以看這個部落格,該博主講的很詳細
http://blog.csdn.net/wangjinyu501/article/details/8169924
我的demo:
http://download.csdn.net/detail/harryweasley/8278633