TextView+View渐变,viewpager滑动的时候

思路:就是一个位置设置一样的两个控件,这两个控件设置自己想要的两种不同得颜色,在实现OnPageChangeListener监听的时候,在onPageScrolled方法里,设置对应控件的透明度。


xml1(要设置两种颜色的字体,用个RelativeLayout包起来):

</pre><pre name="code" class="html"><RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true" >

                <<span style="color:#33ff33;">TextView</span>
                    android:id="@+id/posting_home_txt_select"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/home_text"
                    android:textColor="@color/menu_back"
                    android:textSize="16sp" />

                <<span style="color:#33ff33;">TextView</span>
                    android:id="@+id/posting_home_txt_normal"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/home_text"
                    android:textColor="@color/back_color2"
                    android:textSize="16sp" />
            </RelativeLayout>


xml2:

<RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="3dp"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true" >

                <<span style="color:#ff6666;">View</span>
                    android:id="@+id/posting_home_view_select"
                    android:layout_width="fill_parent"
                    android:layout_height="3dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:background="@color/menu_back" />

                <<span style="color:#ff6666;">View</span>
                    android:id="@+id/posting_home_view_normal"
                    android:layout_width="fill_parent"
                    android:layout_height="3dp"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:background="@color/white" />
            </RelativeLayout>

最开始控件定义,然后赋透明度(0~1之间):

<span style="white-space:pre">		</span>posting_home_txt_normal.setAlpha(<span style="color:#33ccff;">0f</span>);
		posting_home_txt_select.setAlpha(<span style="color:#cc0000;">1f</span>);
		posting_community_text_normal.setAlpha(1f);
		posting_community_text_select.setAlpha(0f);
		posting_search_text_normal.setAlpha(1f);
		posting_search_text_select.setAlpha(0f);

页面滑动的时候,监听:

/**
	 * 页卡切换监听
	 */
	public class MyOnPageChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {
		}

		@Override
		public void onPageScrolled(int index, float ratio, int offset) {
			<span style="color:#3333ff;">if (ratio > 0) {
				colorChange(index, index + 1, ratio);
			}</span>
		}

		@Override
		public void onPageSelected(int index) {
				
		}
	}

控制控件颜色变化透明度的方法:

/**
	 *@param srcIndex 失去焦点的索引
	 * @param destIndex 选中的索引
	 * @param ratio 透明的比例
	 */
	private void colorChange(int srcIndex, int destIndex, float ratio) {
		<span style="color:#000099;">textList.get(srcIndex * 2).setAlpha(1-ratio);
		textList.get(srcIndex * 2 + 1).setAlpha(ratio);</span>
		
		textList.get(destIndex * 2).setAlpha(ratio);
		textList.get(destIndex * 2 + 1).setAlpha(1-ratio);
		
		viewList.get(srcIndex * 2).setAlpha(1-ratio);
		viewList.get(srcIndex * 2 + 1).setAlpha(ratio);
		
		viewList.get(destIndex * 2).setAlpha(ratio);
		viewList.get(destIndex * 2 + 1).setAlpha(1-ratio);
	}

点击时间,在xml部分设置,代码部分自定义变量:

android:onClick="<span style="color:#ff0000;">posting_home_clic</span>k" 

private final int <span style="color:#666600;">POSTING_HOME </span>= 0; 

public void posting_home_click(View view) {
		if (curIndex != POSTING_HOME ) {
			colorChange(POSTING_HOME , curIndex, 0);
			
			posting_post_viewpage.setCurrentItem(POSTING_HOME , <span style="color:#6633ff;">false</span>);
		}
	}

这个博主写的很详细,也解释的很清晰:http://blog.youkuaiyun.com/gesanri/article/details/49382463



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值