亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android仿知乎ScrollView滾動改變標題欄透明度

 更新時間:2018年06月28日 09:23:06   作者:止水  
這篇文章主要為大家詳細介紹了android仿知乎ScrollView滾動改變標題欄透明度,具有一定的參考價值,感興趣的小伙伴們可以參考一下

刷知乎的時候看到,專題欄里面 往下滾動標題欄會由透明逐漸變藍色,覺得這個效果不錯,就想自己寫一下

這是自己實現(xiàn)的效果圖:

說下實現(xiàn)思路:

1、先獲取頂部圖片的高度height,這個有3種方式獲取,我用的是監(jiān)聽onGlobalLayout方法的回調(diào)

2、監(jiān)聽scrollview的滾動坐標,原生的沒有這個監(jiān)聽,需要我們自己寫個view繼承scrollview,然后重寫onScrollChanged()方法,創(chuàng)建一個監(jiān)聽,在這個方法里面回調(diào)

3、根據(jù)圖片高度height和滾動的縱坐標y進行判斷,算出比例,透明度范圍0~255,根據(jù)比例設(shè)置改變的透明度,當y>height是不做改變

實現(xiàn)代碼:

import com.example.d_changealphabyscroll.widget.ObservableScrollView;
import com.example.d_changealphabyscroll.widget.ObservableScrollView.ScrollViewListener;
 
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ScrollView;
 
public class MainActivity extends Activity implements ScrollViewListener{
 
 private View layoutHead;
 private ObservableScrollView scrollView;
 private ImageView imageView;
 private WebView webView;
 
 private int height ;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 initView();
 }
 
 
 private void initView() {
 webView = (WebView) findViewById(R.id.webview1);
 scrollView = (ObservableScrollView) findViewById(R.id.scrollview);
 layoutHead = findViewById(R.id.view_head);
 imageView = (ImageView) findViewById(R.id.imageView1);
 layoutHead.setBackgroundColor(Color.argb(0, 0xfd, 0x91, 0x5b));
 
 //初始化webview
 //啟用支持javascript
  WebSettings settings = webView.getSettings();
  settings.setJavaScriptEnabled(true);
  webView.loadUrl("http://www.topit.me/");
  //覆蓋WebView默認使用第三方或系統(tǒng)默認瀏覽器打開網(wǎng)頁的行為,使網(wǎng)頁用WebView打開
  webView.setWebViewClient(new WebViewClient(){
   @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    //返回值是true的時候控制去WebView打開,為false調(diào)用系統(tǒng)瀏覽器或第三方瀏覽器
    view.loadUrl(url);
   return true;
  }
  });
 
 
 //獲取頂部圖片高度后,設(shè)置滾動監(jiān)聽
 ViewTreeObserver vto = imageView.getViewTreeObserver(); 
  vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
   @Override 
   public void onGlobalLayout() {
    imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
    height = imageView.getHeight();
    imageView.getWidth();
    
    scrollView.setScrollViewListener(MainActivity.this);
   } 
  });
  
  
  
 }
 
 
 @Override
 public void onScrollChanged(ObservableScrollView scrollView, int x, int y,
 int oldx, int oldy) {
 
// Log.i("TAG","y--->"+y+" height-->"+height);
 if(y<=height){
 float scale =(float) y /height;
 float alpha = (255 * scale);
// Log.i("TAG","alpha--->"+alpha);
 
 //layout全部透明
// layoutHead.setAlpha(scale);
 
 //只是layout背景透明(仿知乎滑動效果)
 layoutHead.setBackgroundColor(Color.argb((int) alpha, 0xfd, 0x91, 0x5b));
 }
 
 }
}

帶滾動監(jiān)聽的scrollview

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);
 }
 }
 
}

Demo下載地址:點擊打開鏈接

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論