仿京东滑动页面搜索框背景渐变
来源:互联网 发布:锁 java 编辑:程序博客网 时间:2024/05/21 10:02
原文地址:http://blog.csdn.net/qq_39734239/article/details/78504691
最近在做电商项目,看京东的首页搜索框渐变效果十分的不错就找了一些大神的博客
应用到自己的项目就是这个样子
这里主要的两个控件就是 Linelayout(包裹的是搜索框部分) Scrollview(包裹的滑动整体页面)
提醒(一定要用RelativeLayout布局)
设置控件在布局最上边line.bringToFront();
好了直接上代码
第一步:定义控件
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); } }}
定义控件添加监听方法第二步:布局
<LinearLayout android:layout_width="match_parent" android:id="@+id/line" android:orientation="horizontal" android:layout_height="100dp"></LinearLayout><com.example.dell.myapplication.ObservableScrollView android:layout_width="368dp" android:layout_height="495dp" android:id="@+id/scrollView" tools:layout_editor_absoluteY="8dp" tools:layout_editor_absoluteX="8dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout></com.example.dell.myapplication.ObservableScrollView>
scrollview默认只有一个子空间,所以要添加一个布局进行包裹,内容自己加
第三步:Activity.class
import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.LinearLayout;public class MainActivity extends AppCompatActivity { private LinearLayout line; private ObservableScrollView scrollView; private int imageHeight=300; //设置渐变高度,一般为导航图片高度,自己控制 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //查找控件 line= (LinearLayout) findViewById(R.id.line); scrollView= (ObservableScrollView) findViewById(R.id.scrollView); //搜索框在布局最上面 line.bringToFront(); //滑动监听 scrollView.setScrollViewListener(new ObservableScrollView.ScrollViewListener() { @Override public void onScrollChanged(ObservableScrollView scrollView, 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) { float scale = (float) y / imageHeight; float alpha = (255 * scale); // 只是layout背景透明 line.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26)); } else { line.setBackgroundColor(Color.argb((int) 255, 227, 29, 26)); } } }); }}
以上就可以实现滑动的导航栏颜色的渐变了。默认添加span标签什么鬼,麻烦自己去一下吧!
谢谢观看!!!
阅读全文
4 0
- 仿京东滑动页面搜索框背景渐变
- android页面滑动时,顶部title背景渐变的实现
- 一个简单的仿京东搜索框背景渐变
- HTML页面背景渐变
- 网页页面背景渐变色
- 仿京东搜索栏随滑动切换图标及透明度渐变
- js实现:仿京东搜索栏随滑动透明度渐变
- iOS绘制渐变背景滑动条
- 框模型、背景、渐变
- viewpager滑动背景透明效果渐变(引导界面滑动)
- android 如何构建背景渐变的页面
- 仿微信内容滑动底部Tab背景颜色渐变
- ViewPage滑动背景渐变效果的Splash主页面
- (浅谈)监听ListView滑动实现标题背景Alpha渐变
- Android 监听ScrollView滑动 实现布局背景、文本颜色渐变
- ListView滑动使得标题栏背景渐变(透明-不透明)
- 监听ListView滑动实现标题背景Alpha渐变
- 滑动ScrollView,改变搜索栏背景,透明
- 关于JavaBean/EJB/POJO/Entity的概念
- 自行车比赛
- java数据结构
- devpress 的gridview 控件的行拖拽
- 母牛的故事
- 仿京东滑动页面搜索框背景渐变
- 怎么理解 IaaS、SaaS 和 PaaS 的区别?
- java的内存结构
- 干货|敏捷实践重构
- 程序员来做设计,世界会怎样?
- nginx无法启动
- Java Map 按Key排序和按Value排序
- 测鬼记(中)之奋斗——第一个项目
- Nginx的命令行控制