android 透明的標題逐漸顯示
阿新 • • 發佈:2019-02-17
效果圖:
關鍵程式碼如下:
public class QQSpeakActivity extends AppCompatActivity implements GradationScrollView.ScrollViewListener{ private GradationScrollView scrollView; private ListView listView; private TextView textView; private int height; private ImageView ivBanner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_NO_TITLE); StatusBarUtil.setImgTransparent(this); setContentView(R.layout.activity_qqspeak); scrollView = (GradationScrollView) findViewById(R.id.scrollview); listView = (ListView) findViewById(R.id.listview); textView = (TextView) findViewById(R.id.textview); ivBanner = (ImageView) findViewById(R.id.iv_banner); ivBanner.setFocusable(true); ivBanner.setFocusableInTouchMode(true); ivBanner.requestFocus(); initListeners(); initData(); } /** * 獲取頂部圖片高度後,設定滾動監聽 */ private void initListeners() { ViewTreeObserver vto = ivBanner.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { textView.getViewTreeObserver().removeGlobalOnLayoutListener( this); height = ivBanner.getHeight(); scrollView.setScrollViewListener(QQSpeakActivity.this); } }); } private void initData() { ArrayAdapter<String> adapter = new ArrayAdapter<String>(QQSpeakActivity.this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.data)); listView.setAdapter(adapter); } /** * 滑動監聽 * @param scrollView * @param x * @param y * @param oldx * @param oldy */ @Override public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) { // TODO Auto-generated method stub if (y <= 0) { //設定標題的背景顏色 textView.setBackgroundColor(Color.argb((int) 0, 144,151,166)); } else if (y > 0 && y <= height) { //滑動距離小於banner圖的高度時,設定背景和字型顏色顏色透明度漸變 float scale = (float) y / height; float alpha = (255 * scale); textView.setTextColor(Color.argb((int) alpha, 255,255,255)); textView.setBackgroundColor(Color.argb((int) alpha, 144,151,166)); } else { //滑動到banner下面設定普通顏色 textView.setBackgroundColor(Color.argb((int) 255, 144,151,166)); } } }
如果想看程式碼的 點選下載