這篇文章主要為大家展示了“Android中scrollview滑動(dòng)使標(biāo)題欄漸變背景色的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Android中scrollview滑動(dòng)使標(biāo)題欄漸變背景色的示例分析”這篇文章吧。
創(chuàng)新互聯(lián)建站成都網(wǎng)站建設(shè)按需規(guī)劃網(wǎng)站,是成都網(wǎng)站營銷推廣公司,為成都衛(wèi)生間隔斷提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計(jì)服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計(jì)、前端HTML5制作、后臺(tái)程序開發(fā)等。成都網(wǎng)站維護(hù)熱線:028-86922220
直接上源代碼:
一、核心類(ObservableScrollView.java)
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, 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è)置滾動(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() { ArrayAdapteradapter = new ArrayAdapter (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背景透明(仿知乎滑動(dòng)效果) textView.setBackgroundColor(Color.argb((int) alpha, 227, 29, 26)); } else { textView.setBackgroundColor(Color.argb((int) 255, 227, 29, 26)); } } }
三、XML(activity_main.xml)
以上是“Android中scrollview滑動(dòng)使標(biāo)題欄漸變背景色的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!