常常會有一些APP開發(fā)需要用到啟動畫面或者引導頁面刁俭,啟動頁面正常都是一些簡單的Logo或者圖片之類,主要是能實現(xiàn)一些后臺數(shù)據(jù)的加載。而引導頁面可以通過左右滑動方式傳達信息(APP介紹宛渐,新功能,插入廣告等)給用戶。
簡單實現(xiàn)啟動畫面
兩種方法的實現(xiàn):
一 建立一個activity窥翩,展示啟動畫面业岁,然后再跳轉到主Activity。
public class WelcomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設置無標題
requestWindowFeature(Window.FEATURE_NO_TITLE);
//設置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome);
new Handler().postDelayed(new Runnable() {//一秒后跳轉到主頁面
public void run() {
// 跳轉至 MainActivity
Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(intent);
WelcomeActivity.this.finish();//如果沒有finish掉寇蚊,按回退鍵就會回到歡迎界面不合理笔时。
}
}, 1000);
}
}
布局layout.activity_welcome.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:orientation="vertical"
android:background="@mipmap/timg">
</LinearLayout>
二 在同一個activity展示啟動畫面,后進行隱藏仗岸。
public class Welcome2Activity extends Activity implements Animation.AnimationListener {
LinearLayout welcomelinearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome2);
AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);//2秒漸變
animation.setDuration(2000);
welcomelinearLayout = (LinearLayout) findViewById(R.id.welcome);
welcomelinearLayout.setAnimation(animation);
animation.setAnimationListener(this);
}
@Override
public void onAnimationStart(Animation animation) {
welcomelinearLayout.setVisibility(View.VISIBLE);//開始時進行顯示
}
@Override
public void onAnimationEnd(Animation animation) {
welcomelinearLayout.setVisibility(View.GONE); //結束時進行隱藏起來
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
布局layout.activity_welcome2.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:orientation="vertical">
<LinearLayout
android:id="@+id/welcome"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/timg"
android:orientation="vertical"></LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Welcome To MainActivity!"
android:textSize="24sp" />
</LinearLayout>
簡單實現(xiàn)引導頁
這里是用ViewPager實現(xiàn)引導頁糊闽。
介紹:引導頁有四個可以左右滑動的頁面,最后一個頁面有一個按鈕點擊可進入主頁面爹梁。
分別創(chuàng)建4個子布局welcome_item1.xml右犹,welcome_item2.xml,welcome_item3.xml都是一樣布局background設置不一樣圖片而已
<?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:background="@drawable/timg"
android:orientation="vertical">
</LinearLayout>
welcome_item4.xml也就多一個按鈕
<?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/timg4"
android:orientation="vertical">
<Button
android:id="@+id/Bt_start"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:text="進入主頁面"
android:textSize="20sp" />
</RelativeLayout>
接下來我們來看看主布局activity_welcome4.xml姚垃,主頁面分兩個模塊一個是ViewPager還一個是要實現(xiàn)底部的小圓點功能:
<?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:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/welcome_vp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<LinearLayout
android:id="@+id/spot_linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>
接下來編寫小圓點的selector念链,分為選中和沒選中兩種狀態(tài)。
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="@drawable/dark_dot" />
<item android:state_enabled="false" android:drawable="@drawable/white_dot" />
</selector>
dark_dot.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#33ccff"/>
<size android:width="20dp"
android:height="20dp"/>
</shape>
white_dot
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#ccffbb"/>
<size android:width="20dp"
android:height="20dp"/>
</shape>
接下來就是activity部分积糯,先上全碼
public class Welcome4Activity extends Activity implements ViewPager.OnPageChangeListener {
private ViewPager welcome_vp;
private ArrayList<View> ViewList;
private Button Bt_start;
private View view1, view2, view3, view4;
//底部小點圖片
private ImageView[] dots;
//引導頁個數(shù)
private int Number=4;
//記錄當前選中位置
private int currentIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//設置無標題
requestWindowFeature(Window.FEATURE_NO_TITLE);
//設置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_welcome4);
initData();
//初始化底部小點
initDots();
welcome_vp = (ViewPager) findViewById(R.id.welcome_vp);
WelcomeVPAdapter adapter = new WelcomeVPAdapter();
welcome_vp.setAdapter(adapter);
welcome_vp.addOnPageChangeListener(this);
Bt_start = (Button) view4.findViewById(R.id.Bt_start);
Bt_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goToActivity(MainActivity.class.getName());
finish();
}
});
}
//初始化底部小點
private void initDots() {
LinearLayout spot_linearLayout = (LinearLayout) findViewById(R.id.spot_linearLayout);
//根據(jù)個數(shù)添加對應小點的ImageView
for (int i = 0; i < Number; i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.dot);
iv.setPadding(30, 30, 30, 30);
spot_linearLayout.addView(iv);
}
dots = new ImageView[Number];
//循環(huán)取得小點圖片
for (int i = 0; i < Number; i++) {
dots[i] = (ImageView) spot_linearLayout.getChildAt(i);
dots[i].setEnabled(true);//全部設置顏色為不選中狀態(tài)
}
currentIndex = 0;
dots[currentIndex].setEnabled(false);//默認第一個選中狀態(tài)
}
private void setCurDot(int positon) {
if (positon < 0 || positon > Number - 1 || currentIndex == positon) {
return;
}
dots[positon].setEnabled(false);
dots[currentIndex].setEnabled(true);
currentIndex = positon;
}
//初始化數(shù)據(jù)
private void initData() {
ViewList = new ArrayList<View>();
view1 = LayoutInflater.from(this).inflate(R.layout.welcome_item1, null);
view2 = LayoutInflater.from(this).inflate(R.layout.welcome_item2, null);
view3 = LayoutInflater.from(this).inflate(R.layout.welcome_item3, null);
view4 = LayoutInflater.from(this).inflate(R.layout.welcome_item4, null);
ViewList.add(view1);
ViewList.add(view2);
ViewList.add(view3);
ViewList.add(view4);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
setCurDot(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
class WelcomeVPAdapter extends PagerAdapter {
//返回可以滑動的VIew的個數(shù)
@Override
public int getCount() {
return ViewList.size();
}
//滑動切換的時候銷毀當前的組件
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(ViewList.get(position));
}
//將當前視圖添加到container中并返回當前View視圖
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(ViewList.get(position));
return ViewList.get(position);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
protected void goToActivity(String className) {
Intent intent = new Intent();
intent.setClassName(getBaseContext(), className);
startActivity(intent);
}
}
首先肯定是初始化數(shù)據(jù)咯
這里有三個個模塊的初始化(點擊按鈕的就不說了)
- view的初始化
//初始化數(shù)據(jù)
private void initData() {
ViewList = new ArrayList<View>();
view1 = LayoutInflater.from(this).inflate(R.layout.welcome_item1, null);
view2 = LayoutInflater.from(this).inflate(R.layout.welcome_item2, null);
view3 = LayoutInflater.from(this).inflate(R.layout.welcome_item3, null);
view4 = LayoutInflater.from(this).inflate(R.layout.welcome_item4, null);
ViewList.add(view1);
ViewList.add(view2);
ViewList.add(view3);
ViewList.add(view4);
}
- 小圓點的初始化
//初始化底部小點
private void initDots() {
LinearLayout spot_linearLayout = (LinearLayout) findViewById(R.id.spot_linearLayout);
//動態(tài)添加小圓點的ImageView
for (int i = 0; i < Number; i++) {
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.dot);
iv.setPadding(30, 30, 30, 30);
spot_linearLayout.addView(iv);
}
dots = new ImageView[Number];
//循環(huán)取得小點圖片
for (int i = 0; i < Number; i++) {
dots[i] = (ImageView) spot_linearLayout.getChildAt(i);
dots[i].setEnabled(true);//全部設置顏色為不選中狀態(tài)
}
currentIndex = 0;
dots[currentIndex].setEnabled(false);//默認第一個選中狀態(tài)
}
我們知道ViewPager類需要一個PagerAdapter適配器類給它提供數(shù)據(jù)
所以
class WelcomeVPAdapter extends PagerAdapter {
//返回可以滑動的VIew的個數(shù)
@Override
public int getCount() {
return ViewList.size();
}
//滑動切換的時候銷毀當前的組件
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(ViewList.get(position));
}
//將當前視圖添加到container中并返回當前View視圖
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(ViewList.get(position));
return ViewList.get(position);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
然后我們還需要實現(xiàn)了OnPageChangeListener接口掂墓,對ViewPager滑動事件作出相應的反應。
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
setCurDot(position);//在這里我們進行小點狀態(tài)的修改
}
@Override
public void onPageScrollStateChanged(int state) {
}
ok一個簡單的引導頁實現(xiàn)了看成!
1.png