這篇文章給大家介紹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中怎么實現京東搜索框漸變效果就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。