免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

在Android開發中利用Toolbar實現隨著ScrollView改變透明度

本篇文章為大家展示了在Android開發中利用Toolbar實現隨著ScrollView改變透明度,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

創新互聯技術團隊10年來致力于為客戶提供網站設計制作、成都網站設計品牌網站建設全網營銷推廣、搜索引擎SEO優化等服務。經過多年發展,公司擁有經驗豐富的技術團隊,先后服務、推廣了近千家網站,包括各類中小企業、企事單位、高校等機構單位。

Android中Toolbar隨著ScrollView滑動透明度漸變效果實現

一.思路:監聽ScrollView的滑動事件 不斷的修改Toolbar的透明度

二.注意

1.ScrollView 6.0以前沒有scrollView.setOnScrollChangeListener(l)方法  所以要自定義ScrollView 在onScrollChanged()中監聽

2.ScrollView 6.0(23)以前沒有scrollView.setOnScrollChangeListener()方法  所以要自定義ScrollView 實現.為了Toolbar不遮蓋ScrollView我們給ScrollView設置paddingTop

   但是ScrollView 設置paddintTop以后 Toolbar透明度變為0以后還占據空間 會出現空白,解決方法:

 為ScrollView設置兩個屬性:

 1〉.

android:clipToPadding="false" 

表示控件的繪制范圍是否不在padding里面  false就是表示空間的繪制可以繪制到padding中

 2〉

android:clipChildren="false" 

表示子控件是否不能超出padding區域(比如: false :ScrollView上滑的時候 child 可以滑出padding區域 ;true:ScrollView上滑的時候 child 不能可以滑出padding區域 )

布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" > 
 <com.dice.md.toolbar.transperent.TranslucentScrollView 
  android:id="@+id/scrollview" 
  android:clipToPadding="false" 
  android:clipChildren="true" 
  android:paddingTop="&#63;attr/actionBarSize" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" > 
  <LinearLayout 
   android:orientation="vertical" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_blue_dark" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_green_light" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_orange_light" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_blue_dark" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_green_light" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_orange_light" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_blue_dark" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_green_light" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_orange_light" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_blue_dark" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_green_light" 
    /> 
   <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="400dp" 
    android:background="@android:color/holo_orange_light" 
    /> 
  </LinearLayout> 
 </com.dice.md.toolbar.transperent.TranslucentScrollView> 
 <android.support.v7.widget.Toolbar 
  android:id="@+id/toolbar" 
  android:layout_width="match_parent" 
  android:background="&#63;attr/colorPrimary" 
  android:layout_height="&#63;attr/actionBarSize" > 
 </android.support.v7.widget.Toolbar> 
</RelativeLayout> 

三.步驟

1. 創建回調接口:

public interface TranslucentListener { 
/** 
 * 透明度的回調 
 * @param alpha 
 */ 
public void onTranslucent(float alpha); 
} 

2.自定義ScrollView 在onScrollChange方法中回調TranslucentListener接口的方法 并且回傳alpha的值:

@Override 
protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
 super.onScrollChanged(l, t, oldl, oldt); 
 if (translucentListener!=null) { 
  //translucentListener.onTranslucent(alpha); 
 } 
} 

3.alpha的值得計算:

// alpha = 滑出去的高度/(screenHeight/3); 
float heightPixels = getContext().getResources().getDisplayMetrics().heightPixels; 
float scrollY = getScrollY();//該值 大于0 
float alpha = 1-scrollY/(heightPixels/3);// 0~1 透明度是1~0 
//這里選擇的screenHeight的1/3 是alpha改變的速率 (根據你的需要你可以自己定義)

最后MainActivity中

@Override 
public void onTranslucent(float alpha) { 
 toolbar.setAlpha(alpha); 
} 

上述內容就是在Android開發中利用Toolbar實現隨著ScrollView改變透明度,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注創新互聯行業資訊頻道。

本文名稱:在Android開發中利用Toolbar實現隨著ScrollView改變透明度
當前URL:http://m.newbst.com/article18/gdedgp.html

成都網站建設公司_創新互聯,為您提供網站建設手機網站建設網站策劃網站改版品牌網站建設ChatGPT

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站優化排名