一个简单的仿京东搜索框背景渐变
2016-12-02 17:39
337 查看
1、首先自定义一个scollview
2、在Activity里实现接口并监听滑动距离设置透明度
xml代码就不在这里提供了 下面附上demo
如果你觉得这篇文章对你有用,那么赞一个或者留个言吧~
源码点击下载
public class ObservableScrollView extends ScrollView { public interface ScrollViewListener { void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); } private ScrollViewListener scrollViewListener = null; public ObservableScrollView(Context context) { super(context); } public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ObservableScrollView(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); } }
2、在Activity里实现接口并监听滑动距离设置透明度
public class MainActivity extends Activity implements ObservableScrollView.ScrollViewListener{ LinearLayout linear; ObservableScrollView scroll; GradientDrawable grad;//这里我们使用一个shape指向的类可以实现shape的各种效果 @Override protected void onCreate(Bundle savedInstanceState) { this.requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linear=(LinearLayout) findViewById(R.id.m_home_search); scroll=(ObservableScrollView) findViewById(R.id.m_home_scroll); scroll.setScrollViewListener(this); grad=new GradientDrawable(); float scale2 = (float) 150 / 350; float alpha2 = (255 * scale2); grad.setColor(Color.argb((int) alpha2, 204, 204, 204)); //初始化透明度为完全透明 grad.setCornerRadius(45); linear.setBackground(grad); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) { if(y<=0){ grad.setColor(Color.argb((int)0, 240, 240, 240)); //Y轴0点的时候完全透明 linear.setBackground(grad); }else if(y > 100 && y <= 350){ //我们假设轮播图的高度为350 float scale = (float) y / 350; float alpha = (255 * scale); grad.setColor(Color.argb((int) alpha, 240, 240, 240)); linear.setBackground(grad); } } }
xml代码就不在这里提供了 下面附上demo
如果你觉得这篇文章对你有用,那么赞一个或者留个言吧~
源码点击下载
相关文章推荐
- fib系统分析(linux网络协议栈笔记)
- jsp刷新技术总结
- while循环的用法
- Xperia Android 调教指南
- 【leetcode 141】 Linked List Cycle
- hduoj 1421搬寝室【动态规划】
- iphone4s 降级
- android QQ好友分享
- 自动安装squid+iptables上网代理及上网行为管理脚本(一)。
- PHP面向对象中的析构方法
- java 重载 多个方法的调用顺序
- 编译unity-mono( 写给超级初学者的朋友)
- iOS 音乐后台播放
- C#对 CSV文件读写
- HDU1016 Prime Ring Problem(素数环,深搜DFS)
- 日期字符串校验的问题
- Multi class ovr or ovo
- sax,dom,jdom的优缺点比较
- def 定义函数的用法
- 函数式编程概念:柯里化(currying)