這篇文章給大家介紹Android中怎么實(shí)現(xiàn)京東搜索框漸變效果,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

原理:就是自定義scrollview實(shí)現(xiàn)對(duì)滑動(dòng)高度的監(jiān)聽而已,如此實(shí)現(xiàn)對(duì)搜索框的漸變
//自定義ScrollViewpublic class CustomView extends ScrollView { public interface ScrollViewListener { void onScrollChanged(CustomView customView, int x, int y, int oldx, int oldy); } private ScrollViewListener scrollViewListener = null; public CustomView(Context context) { super(context); } public CustomView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public CustomView(Context context, AttributeSet attrs) { super(context, attrs); } public void setScrollViewListener(ScrollViewListener scrollViewListener) { this.scrollViewListener = scrollViewListener; } @Override protected void onScrollChanged(int x, int y, int oldx, int oldy) { super.onScrollChanged(x, y, oldx, oldy); if (scrollViewListener != null) { scrollViewListener.onScrollChanged(this, x, y, oldx, oldy); } }}
好了,接下來就直接在邏輯代碼中調(diào)用就行了!
@Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); //搜索框在布局最上面 line.bringToFront(); mScrollview.setScrollViewListener(new CustomView.ScrollViewListener() { @Override public void onScrollChanged(CustomView customView, int x, int y, int oldx, int oldy) { if (y <= 0) { line.setBackgroundColor(Color.argb((int) 0, 227, 29, 26));//AGB由相關(guān)工具獲得,或者美工提供 } else if (y > 0 && y <= imageHeight) { //獲取ScrollView向下滑動(dòng)圖片消失的比例 float scale = (float) y / imageHeight; //更加這個(gè)比例,讓標(biāo)題顏色由淺入深 float alpha = (255 * scale); // 只是layout背景透明 line.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255)); } } });
關(guān)于Android中怎么實(shí)現(xiàn)京東搜索框漸變效果就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
網(wǎng)頁(yè)標(biāo)題:Android中怎么實(shí)現(xiàn)京東搜索框漸變效果-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://www.chinadenli.net/article0/djopoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、動(dòng)態(tài)網(wǎng)站、電子商務(wù)、做網(wǎng)站、網(wǎng)站改版、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容