仿微信內容滑動底部Tab背景顏色漸變
二:新建一個Android專案
1. 將下載下來的JazzyViewPager-master開原始碼中的src目錄下的程式碼全部複製到自己專案的src目錄下。
2. 將JazzyViewPager-master中res目錄拷貝到專案的目錄當中,將之前的res目錄覆蓋,然後我們只需要在這個res基礎上去新增我們還需要的佈局。
3. 可能從GitHub中下載的NineOldAndroids-library不是不能作為專案庫檔案匯入,這時我們要將master-JazzyViewPager資料夾中lib\libs中的nineoldandroids-2.4.0.jar複製到自己專案中來。
4. 前期需要的程式碼準備完後,我們還需要修改一下JazzViewPager.java的程式碼。
a.在JazzViewPager類中定義callback介面,為了在onOptionsItemSelected方法中回撥該介面,顯示背景顏色的百分比。
public static interface SlideCallback {
void callBack(int position, float positionOffset);
}
private SlideCallback slideCallback = null;
public void setSlideCallBack(SlideCallback slideCallBack) {
this .slideCallback = slideCallBack;
}
b.修改animateFade方法定義程式碼如下:
protected void animateFade(View left, View right, float positionOffset, int position) {
if (left != null) {
ViewHelper.setAlpha(left, 1-positionOffset);
if (slideCallback != null) {
slideCallback.callBack(position, 1 -positionOffset);
}
}
if (right != null) {
ViewHelper.setAlpha(right, positionOffset);
if (slideCallback != null) {
slideCallback.callBack(position+1, positionOffset);
}
}
}
該方法在onPageScrolled方法中呼叫,又在callBack方法中去對兩個佈局設定背景顏色百分比。
5.程式碼修改完之後,下一步就是設計佈局檔案。
activity_main.xml程式碼
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:visibility="gone" >
</FrameLayout>
<com.jfeinstein.jazzyviewpager.JazzyViewPager
xmlns:app="http://schemas.android.com/apk/res/com.example.bottommenuslidegradientswipe"
android:id="@+id/jazzyPager"
app:style="standard"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="60dp"
android:visibility="visible" />
</LinearLayout>
</TabHost>
main_tabwidget_layout.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:padding="5dp" >
<LinearLayout
android:id="@+id/normalLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/normalImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/normalTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信" />
</LinearLayout>
<LinearLayout
android:id="@+id/selectedLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/selectedImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/selectedTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="微信"
android:textColor="#CA464C"/>
</LinearLayout>
</RelativeLayout>
1.TabWidget就是微信底部整塊控制元件,TabHost指的是ViewPager和 TabWidget外面的一層佈局。
2.在TabHost中要有一個FrameLayout佈局,如果在main佈局中沒有的話彙報一個TabHost must have a FrameLayout whose id attribute is ‘android.R.id.tabcontent’異常。
3.TabHost、TabWidget 使用的Id是android系統分配的Id。
4.TabWidget中子Tab項就是main_tabwidget_layout.xml所展示的佈局。
5.在TabHost中的那個幀佈局一開始時隱藏的,但是在呼叫了setOnTabChangedListener方法之後變成可見的了,這個時候我們要在tabHost.getTabContentView().setVisibility(View.GONE);隱藏掉。
6.jazzyPager.setPageMargin(30); 設定頁之間的間距。
7.jazzyPager.setFadeEnabled(true); 設為true之後,滑動的時候即將消失的一頁會變暗,而且才可以有顏色漸變的效果。
8.兩個佈局normal和selected顯示。
三、MainActivity.java程式碼
package com.example.weixincopy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import com.example.bottommenuslidegradientswipe.R;
import com.jfeinstein.jazzyviewpager.JazzyViewPager;
import com.jfeinstein.jazzyviewpager.JazzyViewPager.SlideCallback;
import com.jfeinstein.jazzyviewpager.JazzyViewPager.TransitionEffect;
import com.jfeinstein.jazzyviewpager.OutlineContainer;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nineoldandroids.view.ViewHelper;
public class MainActivity extends Activity {
@ViewInject(R.id.jazzyPager)
private JazzyViewPager jazzyPager;
List<Map<String, View>> tabViews = new ArrayList<Map<String, View>>();
Context context;
public TabHost tabHost;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewUtils.inject(this);
context = this;
ActionBar actionBar = getActionBar();
actionBar.setTitle("仿微信滑動底部背景顏色漸變");
// --------------------
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("0").setIndicator(createTab("微信", 0)).setContent(android.R.id.tabcontent));
tabHost.addTab(tabHost.newTabSpec("1").setIndicator(createTab("通訊錄", 1)).setContent(android.R.id.tabcontent));
tabHost.addTab(tabHost.newTabSpec("2").setIndicator(createTab("發現", 2)).setContent(android.R.id.tabcontent));
tabHost.addTab(tabHost.newTabSpec("3").setIndicator(createTab("我", 3)).setContent(android.R.id.tabcontent));
// 點選tabHost 來切換不同的訊息
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
int index = Integer.parseInt(tabId);
setTabSelectedState(index, 4); // 引數2:頁數4
tabHost.getTabContentView().setVisibility(View.GONE);// 隱藏content,就是那個幀佈局
switch (index) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
}
});
tabHost.setCurrentTab(0);
initJazzyPager(TransitionEffect.Standard);
}
/**
* 建立 TabWidget的Tab項,設定normalLayout的alpha為1,selectedLayout的alpha為0
* @param tabLabelText
* @param tabIndex
* @return
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
private View createTab(String tabLabelText, int tabIndex) {
View tabIndicator = LayoutInflater.from(this).inflate(R.layout.main_tabwidget_layout, null);
ImageView normalImg = (ImageView) tabIndicator.findViewById(R.id.normalImg);
ImageView selectedImg = (ImageView) tabIndicator.findViewById(R.id.selectedImage);
TextView normalTV = (TextView) tabIndicator.findViewById(R.id.normalTV);
TextView selectedTV = (TextView) tabIndicator.findViewById(R.id.selectedTV);
normalTV.setText(tabLabelText);
selectedTV.setText(tabLabelText);
switch (tabIndex) {
case 0:
normalImg.setImageResource(R.drawable.home2);
selectedImg.setImageResource(R.drawable.home_press2);
break;
case 1:
normalImg.setImageResource(R.drawable.govaffairs2);
selectedImg.setImageResource(R.drawable.govaffairs_press2);
break;
case 2:
normalImg.setImageResource(R.drawable.newscenter2);
selectedImg.setImageResource(R.drawable.newscenter_press2);
break;
case 3:
normalImg.setImageResource(R.drawable.setting2);
selectedImg.setImageResource(R.drawable.setting_press2);
break;
}
View normalLayout = tabIndicator.findViewById(R.id.normalLayout);
normalLayout.setAlpha(1f);// 透明度顯示
View selectedLayout = tabIndicator.findViewById(R.id.selectedLayout);
selectedLayout.setAlpha(0f);// 透明的隱藏
Map<String, View> map = new HashMap<String, View>();
map.put("normal", normalLayout);
map.put("selected", selectedLayout);
tabViews.add(map);
return tabIndicator;
}
/**
* 設定tab狀態選中
* @param index
*/
@SuppressLint("NewApi")
private void setTabSelectedState(int index, int tabCount) {
for (int i = 0; i < tabCount; i++) {
if (i == index) {
tabViews.get(i).get("normal").setAlpha(0f);
tabViews.get(i).get("selected").setAlpha(1f);
} else {
tabViews.get(i).get("normal").setAlpha(1f);
tabViews.get(i).get("selected").setAlpha(0f);
}
}
jazzyPager.setCurrentItem(index, false);// false表示 程式碼切換 pager
// 的時候不帶scroll動畫
}
@Override
protected void onResume() {
super.onResume();
setTabSelectedState(tabHost.getCurrentTab(), 4);
}
private void initJazzyPager(TransitionEffect effect) {
jazzyPager.setTransitionEffect(effect);
jazzyPager.setAdapter(new MainAdapter());
jazzyPager.setPageMargin(30);
jazzyPager.setFadeEnabled(true);
jazzyPager.setSlideCallBack(new SlideCallback() {
@Override
public void callBack(int position, float positionOffset) {
Map<String, View> map = tabViews.get(position);
// System.out.println("position="+position+",positionOffset="+positionOffset);
ViewHelper.setAlpha(map.get("selected"), positionOffset);
ViewHelper.setAlpha(map.get("normal"), 1 - positionOffset);
}
});
jazzyPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
tabHost.setCurrentTab(position);
}
@Override
public void onPageScrolled(int paramInt1, float paramFloat, int paramInt2) {
}
@Override
public void onPageScrollStateChanged(int paramInt) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Toggle Fade");
String[] effects = this.getResources().getStringArray(R.array.jazzy_effects);
for (String effect : effects)
menu.add(effect);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle().toString().equals("Toggle Fade")) {
jazzyPager.setFadeEnabled(!jazzyPager.getFadeEnabled());
} else {
TransitionEffect effect = TransitionEffect.valueOf(item.getTitle().toString());
initJazzyPager(effect);
}
return true;
}
private class MainAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, final int position) {
TextView text = new TextView(MainActivity.this);
text.setGravity(Gravity.CENTER);
text.setTextSize(30);
text.setTextColor(Color.WHITE);
text.setText("Page " + position);
text.setPadding(30, 30, 30, 30);
int bg = Color.rgb((int) Math.floor(Math.random() * 128) + 64, (int) Math.floor(Math.random() * 128) + 64, (int) Math.floor(Math.random() * 128) + 64);
text.setBackgroundColor(bg);
container.addView(text, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
jazzyPager.setObjectForPosition(text, position);
return text;
}
@Override
public void destroyItem(ViewGroup container, int position, Object obj) {
container.removeView(jazzyPager.findViewFromObject(position));
}
@Override
public int getCount() {
return 4;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
if (view instanceof OutlineContainer) {
return ((OutlineContainer) view).getChildAt(0) == obj;
} else {
return view == obj;
}
}
}
}
另外在程式碼裡面有用到XUtils庫檔案的程式碼:ViewUtils.inject(this);
將XUtils庫檔案的專案匯入WorkSpace,並且和自己的專案關聯就可以使用了。