Android啟動屏實現左右滑動切換查看功能_Android

來源:互聯網
上載者:User

本文介紹一個app最常見的特性,就是新功能屬性介紹和啟動屏,一般會怎麼實現呢,這不就打算告訴大家了麼。

先說邏輯

  • 先判斷是否第一次啟動app,如果是,則進入功能使用導航(最簡單的做法就是,左右滑動切換查看,滑動到最後一頁點擊按鈕進入首頁)。
  • 如果不是,則顯示啟動屏,2秒之後進入首頁。

邏輯是很簡單,如果有廣告怎麼辦?廣告肯定是從伺服器拿,但會緩衝到本地,沒網的時候可以顯示,可以使用webView來顯示廣告,反正筆者是這樣幹,具體實現先不說。

看看效果

上代碼

SplashActivity.java

package com.devilwwj.featureguide;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import com.devilwwj.featureguide.global.AppConstants;import com.devilwwj.featureguide.utils.SpUtils;public class SplashActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 判斷是否是第一次開啟應用 boolean isFirstOpen = SpUtils.getBoolean(this, AppConstants.FIRST_OPEN); // 如果是第一次啟動,則先進入功能引導頁 if (!isFirstOpen) {  Intent intent = new Intent(this, WelcomeGuideActivity.class);  startActivity(intent);  finish();  return; } // 如果不是第一次啟動app,則正常顯示啟動屏 setContentView(R.layout.activity_splash); new Handler().postDelayed(new Runnable() {  @Override  public void run() {  enterHomeActivity();  } }, 2000); } private void enterHomeActivity() { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); }}

代碼解析:使用SharedPreference來儲存app啟動狀態,如果為true,則進入功能導航,否則延遲2秒之後進入首頁面。

WelcomeGuideActivity.java

package com.devilwwj.featureguide;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;import com.devilwwj.featureguide.global.AppConstants;import com.devilwwj.featureguide.utils.SpUtils;import java.util.ArrayList;import java.util.List;public class WelcomeGuideActivity extends Activity implements OnClickListener { private ViewPager vp; private GuideViewPagerAdapter adapter; private List<View> views; private Button startBtn; // 引導頁圖片資源 private static final int[] pics = { R.layout.guid_view1,  R.layout.guid_view2, R.layout.guid_view3, R.layout.guid_view4 }; // 底部小點圖片 private ImageView[] dots; // 記錄當前選中位置 private int currentIndex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_guide); views = new ArrayList<View>(); // 初始化引導頁視圖列表 for (int i = 0; i < pics.length; i++) {  View view = LayoutInflater.from(this).inflate(pics[i], null);  if (i == pics.length - 1) {  startBtn = (Button) view.findViewById(R.id.btn_login);  startBtn.setTag("enter");  startBtn.setOnClickListener(this);  }  views.add(view); } vp = (ViewPager) findViewById(R.id.vp_guide); // 初始化adapter adapter = new GuideViewPagerAdapter(views); vp.setAdapter(adapter); vp.setOnPageChangeListener(new PageChangeListener()); initDots(); } @Override protected void onResume() { super.onResume(); } @Override protected void onPause() { super.onPause(); // 如果切換到後台,就設定下次不進入功能引導頁 SpUtils.putBoolean(WelcomeGuideActivity.this, AppConstants.FIRST_OPEN, true); finish(); } @Override protected void onStop() { super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); } private void initDots() { LinearLayout ll = (LinearLayout) findViewById(R.id.ll); dots = new ImageView[pics.length]; // 迴圈取得小點圖片 for (int i = 0; i < pics.length; i++) {  // 得到一個LinearLayout下面的每一個子項目  dots[i] = (ImageView) ll.getChildAt(i);  dots[i].setEnabled(false);// 都設為灰色  dots[i].setOnClickListener(this);  dots[i].setTag(i);// 設定位置tag,方便取出與當前位置對應 } currentIndex = 0; dots[currentIndex].setEnabled(true); // 設定為白色,即選中狀態 } /** * 設定當前view *  * @param position */ private void setCurView(int position) { if (position < 0 || position >= pics.length) {  return; } vp.setCurrentItem(position); } /** * 設定當前指示點 *  * @param position */ private void setCurDot(int position) { if (position < 0 || position > pics.length || currentIndex == position) {  return; } dots[position].setEnabled(true); dots[currentIndex].setEnabled(false); currentIndex = position; } @Override public void onClick(View v) { if (v.getTag().equals("enter")) {  enterMainActivity();  return; } int position = (Integer) v.getTag(); setCurView(position); setCurDot(position); } private void enterMainActivity() { Intent intent = new Intent(WelcomeGuideActivity.this,  SplashActivity.class); startActivity(intent); SpUtils.putBoolean(WelcomeGuideActivity.this, AppConstants.FIRST_OPEN, true); finish(); } private class PageChangeListener implements OnPageChangeListener { // 當滑動狀態改變時調用 @Override public void onPageScrollStateChanged(int position) {  // arg0 ==1的時辰默示正在滑動,arg0==2的時辰默示滑動完畢了,arg0==0的時辰默示什麼都沒做。 } // 當前頁面被滑動時調用 @Override public void onPageScrolled(int position, float arg1, int arg2) {  // arg0 :當前頁面,及你點擊滑動的頁面  // arg1:當前頁面位移的百分比  // arg2:當前頁面位移的像素位置 } // 當新的頁面被選中時調用 @Override public void onPageSelected(int position) {  // 設定底部小點選中狀態  setCurDot(position); } }}

代碼解析:左右滑動是使用ViewPager來做的,切換4個不同的View,監聽ViewPager的頁面切換事件來更改底部指示點的切換,滑動到最後一個頁面,設定按鈕的點擊事件,點擊進入首頁。

以上就是本文的全部內容,希望對大家學習Android軟體編程有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.