欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2018年05月14日 13:16:33   作者:歲月LICHENGAN  
這篇文章主要介紹了Android之scrollview滑動使標(biāo)題欄漸變背景色的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

直接上源代碼:

一、核心類(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í)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Android自定義View實現(xiàn)水波紋效果

    Android自定義View實現(xiàn)水波紋效果

    這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)水波紋效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android中DrawerLayout+ViewPager滑動沖突的解決方法

    Android中DrawerLayout+ViewPager滑動沖突的解決方法

    這篇文章主要為大家詳細介紹了Android中DrawerLayout+ViewPager滑動沖突的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 月下載量上千次Android實現(xiàn)二維碼生成器app源碼分享

    月下載量上千次Android實現(xiàn)二維碼生成器app源碼分享

    既然是二維碼生成器那么我們?nèi)绾沃谱鞫S碼呢?這篇文章為大家分享了月下載量上千次Android實現(xiàn)二維碼生成器app源碼,希望大家喜歡
    2015-12-12
  • Android ViewFlipper的簡單使用

    Android ViewFlipper的簡單使用

    這篇文章主要為大家詳細介紹了Android ViewFlipper的簡單使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Android開發(fā)中ViewPager實現(xiàn)多頁面切換效果

    Android開發(fā)中ViewPager實現(xiàn)多頁面切換效果

    ViewPager用于實現(xiàn)多頁面的切換效果,該類存在于Google的兼容包里面,所以在引用時記得在BuilldPath中加入“Android-support-v4.jar”。具體詳情大家可以參考下本文
    2016-11-11
  • Android 多媒體播放API簡單實例

    Android 多媒體播放API簡單實例

    這篇文章主要介紹了Android 多媒體播放API簡單實例的相關(guān)資料,這里附有代碼實例及實現(xiàn)效果圖,需要的朋友可以參考下
    2016-12-12
  • Android使用Canvas繪制圓形進度條效果

    Android使用Canvas繪制圓形進度條效果

    這篇文章主要為大家詳細介紹了Android使用Canvas繪制圓形進度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • android實現(xiàn)簡易計算器

    android實現(xiàn)簡易計算器

    這篇文章主要為大家詳細介紹了android實現(xiàn)簡易計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • android通用xml解析方法

    android通用xml解析方法

    之前做的網(wǎng)絡(luò)相關(guān)的應(yīng)用,里面涉及到了xml的解析,由于急于完成任務(wù)也沒有設(shè)計就直接敲代碼。這幾天給一個朋友做項目的時候也涉及到了xml的解析,而且解析的內(nèi)容比較多,我查看了以前的項目中的相關(guān)代碼,頓時覺得很多代碼都是可以優(yōu)化的。在此寫兩個通用的xml解析方法,當(dāng)然這里所講的通用只是在一定程度上并且需要遵守一些規(guī)范。
    2013-03-03
  • 基于Android自定義控件實現(xiàn)刮刮樂效果

    基于Android自定義控件實現(xiàn)刮刮樂效果

    這篇文章主要介紹了基于Android自定義控件實現(xiàn)刮刮樂效果 的相關(guān)資料,需要的朋友可以參考下
    2015-12-12

最新評論