android仿知乎標(biāo)題欄隨ScrollView滾動(dòng)變色
本文實(shí)例為大家分享了android標(biāo)題欄隨ScrollView滾動(dòng)變色的具體代碼,供大家參考,具體內(nèi)容如下
參考:Android之scrollview滑動(dòng)使標(biāo)題欄漸變背景色的實(shí)例代碼
效果圖:
核心類:ObservableScrollView
package com.jukopro.titlebarcolor; import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; /** * 帶滾動(dòng)監(jiān)聽的scrollview */ 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) { super(context, attrs); } public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } 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); } } }
MyListview
解決ScrollView嵌套Listview的滑動(dòng)沖突:
public class MyListview extends ListView { public MyListview(Context context) { super(context); } public MyListview(Context context, AttributeSet attrs) { super(context, attrs); } public MyListview(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
MainActivity
package com.jukopro.titlebarcolor; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.ViewTreeObserver; import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.jukopro.titlebarcolor.ObservableScrollView.ScrollViewListener; public class MainActivity extends Activity implements ScrollViewListener { private ObservableScrollView scrollView; private ListView listView; private ImageView imageView; private TextView textView; private int imageHeight; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scrollView = (ObservableScrollView) findViewById(R.id.scrollview); listView = (ListView) findViewById(R.id.listview); imageView = (ImageView) findViewById(R.id.imageview); textView = (TextView) findViewById(R.id.textview); initListeners(); initData(); } private void initListeners() { // 獲取頂部圖片高度后,設(shè)置滾動(dòng)監(jiān)聽 ViewTreeObserver vto = imageView.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this); imageHeight = imageView.getHeight(); scrollView.setScrollViewListener(MainActivity.this); } }); } private void initData() { ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.data)); listView.setAdapter(adapter); } /** * ScrollView滾動(dòng)監(jiān)聽 * * @param scrollView:滾動(dòng)控件 * @param x:x軸坐標(biāo) * @param y:y軸坐標(biāo) * @param oldx:上一個(gè)x軸坐標(biāo) * @param oldy:上一個(gè)y軸坐標(biāo) */ @Override public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) { if (y <= 0) { textView.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背景透明(仿知乎滑動(dòng)效果) textView.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26)); } else { textView.setBackgroundColor(Color.argb((int) 255, 227, 29, 26)); } } }
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <com.jukopro.titlebarcolor.ObservableScrollView android:id="@+id/scrollview" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="200dp" android:background="@drawable/ic_launcher" /> <com.jukopro.titlebarcolor.MyListview android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </com.jukopro.titlebarcolor.ObservableScrollView> <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="48dp" android:background="#00000000" android:gravity="center" android:text="我是標(biāo)題" android:textColor="@android:color/white" android:textSize="18sp" /> </RelativeLayout>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)支付寶螞蟻森林水滴浮動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)支付寶螞蟻森林水滴浮動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android編程之SurfaceView實(shí)例詳解
這篇文章主要介紹了Android編程之SurfaceView用法,簡要分析了View和SurfaceView的區(qū)別,并結(jié)合實(shí)例形式分析了SurfaceView的具體使用步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02Android 8.1 Launcher3實(shí)現(xiàn)動(dòng)態(tài)指針時(shí)鐘功能
這篇文章主要介紹了Android 8.1 Launcher3實(shí)現(xiàn)動(dòng)態(tài)指針時(shí)鐘功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07Android編程實(shí)現(xiàn)隱藏狀態(tài)欄及測試Activity是否活動(dòng)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)隱藏狀態(tài)欄及測試Activity是否活動(dòng)的方法,涉及Android界面布局設(shè)置及Activity狀態(tài)操作的相關(guān)技巧,需要的朋友可以參考下2016-10-10Android筆記之:CM9源碼下載與編譯的應(yīng)用
本篇文章介紹了,在Android中CM9源碼下載與編譯的應(yīng)用,需要的朋友參考下2013-04-04Android測試中Appium的一些錯(cuò)誤解決技巧
今天小編就為大家分享一篇關(guān)于Android測試中Appium的一些錯(cuò)誤解決技巧的文章,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10Android的Launcher啟動(dòng)器中添加快捷方式及小部件實(shí)例
這篇文章主要介紹了在Android的Launcher啟動(dòng)器中添加快捷方式及窗口小部件的方法,包括在自己的應(yīng)用程序中添加窗口小部件AppWidget的例子,需要的朋友可以參考下2016-02-02