Android 滑動(dòng)Scrollview標(biāo)題欄漸變效果(仿京東toolbar)
Scrollview標(biāo)題欄滑動(dòng)漸變
仿京東樣式(上滑顯示下滑漸變消失)
/** * @ClassName MyScrollView * @Author Rex * @Date 2021/1/27 17:38 */ public class MyScrollView extends ScrollView { private TranslucentListener mTranslucentListener; public void setTranslucentListener(TranslucentListener translucentListener) { this.mTranslucentListener = translucentListener; } public MyScrollView(Context context) { this(context, null); } public MyScrollView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (mTranslucentListener != null) { //ScrollView滑出高度 int scrollY = getScrollY(); //屏幕高度 int screenHeight = getContext().getResources().getDisplayMetrics().heightPixels; //有效滑動(dòng)距離為屏幕2分之一 // alpha = 滑動(dòng)高度/(screenHeight/3f) if (scrollY <= screenHeight / 2f) { Log.d(">>>>>>>>>", "ScrollView劃出高度:" + scrollY); Log.d(">>>>>>>>>", "屏幕高度:" + screenHeight); Log.d(">>>>>>>>>", "漸變值:" + (0 + scrollY / (screenHeight / 4f))); // 漸變的過(guò)程 1~0 mTranslucentListener.onTranslucent(0 + scrollY / (screenHeight /4f)); } } } }
Activity 設(shè)置
public class ToolbarActivity extends AppCompatActivity implements TranslucentListener { private Toolbar mToolBar; private MyScrollView mScrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_toobar); mToolBar = findViewById(R.id.id_toolbar); mScrollView = findViewById(R.id.id_scrollView); //初始化漸變?yōu)? mToolBar.setAlpha(0); //設(shè)置漸變回調(diào) mScrollView.setTranslucentListener(this); } @Override public void onTranslucent(float alpha) { mToolBar.setAlpha(alpha); } }
漸變回調(diào)接口
/** * @ClassName TranslucentListener * @Author rex * @Date 2021/1/27 17:38 */ public interface TranslucentListener { /** * 透明度的回調(diào)監(jiān)聽(tīng) * * @param alpha 0~1 透明度 */ public void onTranslucent(float alpha); }
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <com.rex.rxhttpdemo.MyScrollView android:id="@+id/id_scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false" android:clipToPadding="false" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button0" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button1" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button2" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button3" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button4" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button5" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button5" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button5" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button5" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button5" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button5" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button5" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button5" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="Button" /> </LinearLayout> </com.rex.rxhttpdemo.MyScrollView> <androidx.appcompat.widget.Toolbar android:id="@+id/id_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" app:title="title" /> </RelativeLayout>
下滑顯示上滑漸變消失
/** * @ClassName MyScrollView * @Author Rex * @Date 2021/1/27 17:38 */ public class MyScrollView extends ScrollView { private TranslucentListener mTranslucentListener; public void setTranslucentListener(TranslucentListener translucentListener) { this.mTranslucentListener = translucentListener; } public MyScrollView(Context context) { this(context, null); } public MyScrollView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (mTranslucentListener != null) { //ScrollView滑出高度 int scrollY = getScrollY(); //屏幕高度 int screenHeight = getContext().getResources().getDisplayMetrics().heightPixels; //有效滑動(dòng)距離為屏幕2分之一 // alpha = 滑動(dòng)高度/(screenHeight/3f) if (scrollY <= screenHeight / 2f) { Log.d(">>>>>>>>>", "ScrollView劃出高度:" + scrollY); Log.d(">>>>>>>>>", "屏幕高度:" + screenHeight); Log.d(">>>>>>>>>", "漸變值:" + (1 - scrollY / (screenHeight / 4f))); // 漸變的過(guò)程 1~0 mTranslucentListener.onTranslucent(1 - scrollY / (screenHeight /4f)); } } } }
注意: 這里只是更改了 mTranslucentListener.onTranslucent 里的 漸變值
Activty 里 把初始化 mToolBar.setAlpha(0); 去掉
XML
<com.rex.rxhttpdemo.MyScrollView android:id="@+id/id_scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false" android:clipToPadding="false" android:paddingTop="?attr/actionBarSize" > </com.rex.rxhttpdemo.MyScrollView>
xml 加入 paddingtop .
注意:
android:clipChildren=“false”
android:clipToPadding="false"
這倆個(gè)屬性 如果不加會(huì)有留白
到此這篇關(guān)于Android 滑動(dòng)Scrollview標(biāo)題欄漸變效果(仿京東toolbar)的文章就介紹到這了,更多相關(guān)Android 滑動(dòng)Scrollview標(biāo)題欄漸變內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android 使用XStream解析xml的實(shí)例
下面小編就為大家分享一篇android 使用XStream解析xml的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01利用DrawerLayout和觸摸事件分發(fā)實(shí)現(xiàn)抽屜側(cè)滑效果
這篇文章主要為大家詳細(xì)介紹了利用DrawerLayout和觸摸事件分發(fā)實(shí)現(xiàn)抽屜側(cè)滑效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android BroadcastReceiver常見(jiàn)監(jiān)聽(tīng)整理
這篇文章主要介紹了Android BroadcastReceiver常見(jiàn)監(jiān)聽(tīng)整理的相關(guān)資料,需要的朋友可以參考下2016-10-10Android使用surfaceView自定義抽獎(jiǎng)大轉(zhuǎn)盤(pán)
這篇文章主要為大家詳細(xì)介紹了Android使用surfaceView自定義抽獎(jiǎng)大轉(zhuǎn)盤(pán),熟練掌握SurfaceVie實(shí)現(xiàn)抽獎(jiǎng)大轉(zhuǎn)盤(pán),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12Android實(shí)戰(zhàn)教程第七篇之如何在內(nèi)存中存儲(chǔ)用戶名和密碼
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)在內(nèi)存中存儲(chǔ)用戶名和密碼的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Android自定義View實(shí)現(xiàn)BMI指數(shù)條
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)BMI指數(shù)條,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06