国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android之scrollview滑動使標(biāo)題欄漸變背景色的實(shí)例代碼

2019-10-22 18:11:27
字體:
供稿:網(wǎng)友

之前也是在網(wǎng)上看到這種效果,不過是滾動listview來改變標(biāo)題欄的顏色,感覺那個應(yīng)用的比較少,比如我要滾動scrollview來實(shí)現(xiàn)呢,那么問題就來了,廢話少說,看一下要實(shí)現(xiàn)的效果先(這是在項(xiàng)目應(yīng)用的效果)。

android,標(biāo)題欄漸變,scrollview,滑動漸變

直接上源代碼:

一、核心類(ObservableScrollView.java)

package com.jukopro.titlebarcolor; import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; /**  * 帶滾動監(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,    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);   }  } } 

二、具體使用(MainActivity.java)

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è)置滾動監(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);  }  @Override  public void onScrollChanged(ObservableScrollView scrollView, int x, int y,    int oldx, int oldy) {   // TODO Auto-generated method stub   // Log.i("TAG", "y--->" + y + " height-->" + height);   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背景透明(仿知乎滑動效果)    textView.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26));   } else {    textView.setBackgroundColor(Color.argb((int) 255, 227, 29, 26));   }  } } 

三、XML(activity_main.xml)

<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"  tools:context="${relativePackage}.${activityClass}" >  <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/zuqiu" />    <com.jukopro.titlebarcolor.MyListview     android:id="@+id/listview"     android:layout_width="match_parent"     android:layout_height="wrap_content" >    </com.jukopro.titlebarcolor.MyListview>   </LinearLayout>  </com.jukopro.titlebarcolor.ObservableScrollView>  <TextView   android:id="@+id/textview"   android:layout_width="match_parent"   android:layout_height="48dp"   android:gravity="center"   android:text="我是標(biāo)題"   android:textSize="18sp"   android:textColor="@android:color/white"   android:background="#00000000" /> </RelativeLayout>

還不懂的童鞋可以下載源代碼.

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網(wǎng)的支持。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 万荣县| 木里| 邵阳县| 普陀区| 什邡市| 凤城市| 华宁县| 安新县| 宜黄县| 霍林郭勒市| 社旗县| 无极县| 冷水江市| 大冶市| 封丘县| 丹阳市| 安庆市| 普兰店市| 镇原县| 白玉县| 三门县| 县级市| 开封县| 仪陇县| 克山县| 高阳县| 沂水县| 三亚市| 大名县| 宣恩县| 乐至县| 石景山区| 临漳县| 无极县| 夏邑县| 体育| 景谷| 迁西县| 鹤山市| 乌拉特后旗| 衡阳市|