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

原理:就是自定義scrollview實現對滑動高度的監聽而已,如此實現對搜索框的漸變
//自定義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); } }}
好了,接下來就直接在邏輯代碼中調用就行了!
@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由相關工具獲得,或者美工提供 } else if (y > 0 && y <= imageHeight) { //獲取ScrollView向下滑動圖片消失的比例 float scale = (float) y / imageHeight; //更加這個比例,讓標題顏色由淺入深 float alpha = (255 * scale); // 只是layout背景透明 line.setBackgroundColor(Color.argb((int) alpha, 255, 255, 255)); } } });
關于Android中怎么實現京東搜索框漸變效果就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網頁標題:Android中怎么實現京東搜索框漸變效果-創新互聯
轉載源于:http://www.js-pz168.com/article0/djopoo.html
成都網站建設公司_創新互聯,為您提供移動網站建設、動態網站、電子商務、做網站、網站改版、微信小程序
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯