实现这个功能主要涉及的知识点有
public abstract void onPageScrollStateChanged (int state) public abstract void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) public abstract void onPageSelected (int position)
public static final int SCROLL_STATE_DRAGGING Constant Value: 1 (0x00000001) public static final int SCROLL_STATE_IDLE Constant Value: 0 (0x00000000) public static final int SCROLL_STATE_SETTLING Constant Value: 2 (0x00000002)
/** * @author Quinn * @date 2015-2-28 */ public class MainPagerChangeListener implements OnPageChangeListener{ public interface PagerCallback{ public void changePageColor(int index, int offset); } private PagerCallback callback; public MainPagerChangeListener(MainActivity mainActivity){ this.callback = (PagerCallback)mainActivity; } //顺序为1-2-0 @Override public void onPageScrollStateChanged(int state) { } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if(positionOffset != 0.0f){ callback.changePageColor(position+1, (int)(positionOffset * 255)); callback.changePageColor(position, (int)((1-positionOffset) * 255)); } } @Override public void onPageSelected(int position) { } }
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="FooterTextIcon"> <attr name="iconSrc" format="reference" /> <attr name="color" format="color"/> <attr name="text" format="string"/> <attr name="textSize" format="dimension"/> </declare-styleable> </resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:FooterIcon="http://schemas.android.com/apk/res/com.quinn.xmpp" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.quinn.xmpp.ui.main.MainActivity" > //省略,,,, //,,, //,,, <com.quinn.xmpp.ui.widget.FooterTextIcon android:id="@+id/chattingIcon" android:layout_width="0dip" android:layout_height="match_parent" android:layout_weight="1" FooterIcon:iconSrc="@drawable/ic_action_chat" FooterIcon:text="聊天" FooterIcon:textSize="12sp" android:padding="5dip" FooterIcon:color="#ff181818" /> //省略,,,, //,,, //,,, </LinearLayout>
xmlns:自己起一个名字="http://schemas.android.com/apk/res/应用的包名"注意,上面代码的最后一部分是应用的包名,而不是某个Activity的包名或者自定义组件对应Java文件的包名
FooterIcon:iconSrc="@drawable/ic_action_chat"而string,color,dimension的值可以直接写某个值,也可以是引用于资源文件string.xml,color.xml,dimens.xml
FooterIcon:text="聊天" FooterIcon:textSize="12sp" android:padding="5dip" FooterIcon:color="#ff181818"</span>也可以写成
FooterIcon:text="@string/xxxxx" FooterIcon:textSize="@dimen/xxxx" android:padding="@dimen/xxxx" FooterIcon:color="@color/xxxx" />
public class FooterTextIcon extends View { //四个属性 private String text; private int themeColor; private int textSize; private int iconRid; //、、、 /** * @param context */ public FooterTextIcon(Context context) { this(context, null); } public FooterTextIcon(Context context, AttributeSet attrs) { this(context, attrs, 0); } /** * @param context * @param attrs * @param defStyleAttr */ public FooterTextIcon(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FooterTextIcon); iconRid = a.getResourceId(R.styleable.FooterTextIcon_iconSrc, -1); text = a.getString(R.styleable.FooterTextIcon_text); textSize = (int) a.getDimension(R.styleable.FooterTextIcon_textSize, TypedValue .applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics())); themeColor = a.getColor(R.styleable.FooterTextIcon_color, Color.parseColor(DEFAULT_CHOSEN_COLOR)); a.recycle(); //,,, } //,,省略,, }如上,我们在构造函数中,通过一个TypeArray对象获取各种类型的属性,并且最后调用TypeArray的recycle函数回收内存。一般我们将这些代码放在有三个参数的构造方法中执行,而又一个参数的构造方法直接用this指针调用有两个参数的构造方法,同理,两个的调用三个的。
微信底部导航渐变效果-----viewpager&PorterDuffXfermode
原文地址:http://blog.csdn.net/chziroy/article/details/44087995