天天看點

直播網站源碼,顯示隐藏标題欄

直播網站源碼,顯示隐藏标題欄的相關代碼

// An highlighted block
public class DivViewActivity extends AppCompatActivity {
    private ImageView iv_detail;

    private ObservableScrollView scrollView;
    private TextView tv_titlebar;
    private RelativeLayout layout_title;
    private int mImageHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_div_view);
        initView();
        //得到ImageView控件的高度
//        iv_detail.getHeight();//就是如果這個視圖樹沒有繪制完執行該方法,那麼是得不到高度

        //擷取視圖樹的監聽,我們得到視圖樹繪制完畢,我們再去得到控件的高度
        ViewTreeObserver viewTreeObserver = iv_detail.getViewTreeObserver();
        //使用視圖觀察者設定監聽,以便擷取所觀察控件的高度
        viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //卸磨殺驢,回調監聽後的,第一節事情就是移除監聽,減少記憶體的消耗
                iv_detail.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //得到控件高度
                mImageHeight = iv_detail.getHeight();
            }
        });

        //使用我們的自定義ScrollView滾動的監聽,滑動超過圖檔的高度,标題顯示出來
        scrollView.setmScrollViewListener(new ObservableScrollView.ScrollViewListener() {
            @Override
            public void onScrollChange(ObservableScrollView scrollView, int l, int t, int oldl, int oldt) {
                //對t參數進行判斷,兩種形态,一種消失沒有 ; 随着滑動顔色越來越深
                Log.d("1801",t+"");
                if (t<0){
                    //設定标題隐藏
                    tv_titlebar.setVisibility(View.GONE);
                    //标題背景透明
                    layout_title.setBackgroundColor(Color.argb(0,0,0,0));
                }
                else if(t>0 && t < mImageHeight ){
                    //讓标題顯示出來
                    tv_titlebar.setVisibility(View.VISIBLE);
                    //擷取ScrollView向下滑動,圖檔消失部分的比例
                    float scale = (float) t / mImageHeight;
                    //根據這個比例,讓标題的顔色慢慢由淺到深
                    float alpha = 255 * scale;
                    //設定标題的内容及顔色
                    tv_titlebar.setText("阿巴阿巴阿巴阿巴阿巴啊阿巴");
                    tv_titlebar.setTextColor(Color.argb((int)alpha,0,0,0));
                    //設定标題布局顔色
                    layout_title.setBackgroundColor(Color.argb((int)alpha,255,255,255));
                }
            }
        });
    }


    private void initView() {
        iv_detail = (ImageView) findViewById(R.id.iv_detail);
        scrollView = (ObservableScrollView) findViewById(R.id.scrollView);
        tv_titlebar = (TextView) findViewById(R.id.tv_titlebar);
        layout_title = (RelativeLayout) findViewById(R.id.layout_title);
    }
}
           

以上就是直播網站源碼,顯示隐藏标題欄的相關代碼, 更多内容歡迎關注之後的文章

繼續閱讀