仿京東滑動(dòng)頁面搜索框背景漸變實(shí)現(xiàn)方法

這里主要的兩個(gè)控件就是 Linelayout(包裹的是搜索框部分) Scrollview(包裹的滑動(dòng)整體頁面)

提醒(一定要用RelativeLayout布局)

設(shè)置控件在布局最上邊line.bringToFront();//相當(dāng)于改變Z軸

好了直接上代碼

第一步:定義控件

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);
        }
    }
}

定義控件添加監(jiān)聽方法

第二步:布局

<linearlayout android:id="@+id/line" android:layout_height="100dp" android:layout_width="match_parent" android:orientation="horizontal"> </linearlayout> <com.example.dell.myapplication.observablescrollview android:id="@+id/scrollView" android:layout_height="495dp" android:layout_width="368dp" tools:layout_editor_absolutex="8dp" tools:layout_editor_absolutey="8dp"> <linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical"> </linearlayout> </com.example.dell.myapplication.observablescrollview>

scrollview默認(rèn)只有一個(gè)子空間,所以要添加一個(gè)布局進(jìn)行包裹,內(nèi)容自己加
第三步:Activity.class

public class MainActivity extends AppCompatActivity {
 
    private LinearLayout line;
    private ObservableScrollView scrollView;
    private int imageHeight=300; //設(shè)置漸變高度,一般為導(dǎo)航圖片高度,自己控制
    @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();
        //滑動(dòng)監(jiān)聽
        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由相關(guān)工具獲得,或者美工提供
                } 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));
                }
            }
        });
 
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,021評(píng)論 25 709
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,328評(píng)論 0 17
  • 早上在家,我催著大子穿鞋出門,抓緊去幼兒園。 大子邊穿邊對(duì)小子嘀咕:弟弟,今天你夠幸福的了! 我問為何?答:他今天...
    雙子老爸閱讀 614評(píng)論 0 50
  • 美國著名心理學(xué)家威廉詹姆斯說:“播下一個(gè)行動(dòng),收獲一種習(xí)慣;播下一種習(xí)慣,收獲一種性格;播下一種性格,收獲一種命運(yùn)...
    你來不來我都在你在不在我都來閱讀 964評(píng)論 0 0

友情鏈接更多精彩內(nèi)容