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

Android開發(fā)實(shí)現(xiàn)標(biāo)題隨scrollview滑動(dòng)變色的方法詳解

 更新時(shí)間:2017年11月20日 11:47:40   作者:touch_ping  
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)標(biāo)題隨scrollview滑動(dòng)變色的方法,涉及Android針對(duì)滑動(dòng)事件的響應(yīng)、界面布局、屬性動(dòng)態(tài)變換等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android開發(fā)實(shí)現(xiàn)標(biāo)題隨scrollview滑動(dòng)變色的方法。分享給大家供大家參考,具體如下:

要實(shí)現(xiàn)某個(gè)view的背景透明度跟隨scrollview滑動(dòng)而改變需要重新scrollview的onOverScrolled方法,該方法隨著滑動(dòng)變化(包括手指滑動(dòng)、手指移開慣性滑動(dòng))而響應(yīng),所以最適合做變色處理。

step1:設(shè)定布局

由于我們要實(shí)現(xiàn)的是滑動(dòng)時(shí)標(biāo)題的背景透明度改變,固定頂部的標(biāo)題view不能在srcollview里面跟隨滑動(dòng),所以需要這樣布局:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.****.ScrollChangeScrollView
      android:id="@+id/scrollView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:fillViewport="true">
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
          <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawablePadding="5dp"
            android:drawableTop="@drawable/dicovery_vintner_icon_wine"
            android:gravity="center"
            android:text="葡萄酒"
            android:textColor="@color/hometitlebg" />
      </LinearLayout>
    </com.***.ScrollChangeScrollView>
    <Button
      android:id="@+id/btn_back"
      android:layout_width="match_parent"
      android:layout_height="35dp"
      android:layout_centerVertical="true"
      android:background="@null"
      android:drawableLeft="@drawable/icon_back"
      android:padding="10dp" />
</FrameLayout>

step2:添加需要用到的方法

滑動(dòng)時(shí),某個(gè)view要變色,重新scrollview后,添加方法讓其知道該view需要變色

private View mTitleView;
/**
* 變色標(biāo)題view
* @param view
*/
public void setupTitleView (View view) {
    this.mTitleView = view;
}

滑動(dòng)時(shí)變色需要參考scrollview里面的某個(gè)子view的滑動(dòng)高度,如果該子view上劃完全劃出屏幕,則標(biāo)題view背景透明為0:

private View mByWhichView;
/**
* 跟隨的view
* @param view
*/
public void setupByWhichView(View view) {
    mByWhichView = view;
}

再添加一個(gè)設(shè)置,如果不要背景透明度漸變:

private boolean shouldSlowlyChange;
public void setShouldSlowlyChange(boolean slowlyChange) {
    this.shouldSlowlyChange = slowlyChange;
}

step3:代碼實(shí)現(xiàn)

**
 * 滑動(dòng)時(shí)標(biāo)題變色view
 * Created by george.yang on 16/2/21.
 */
public class ScrollChangeScrollView extends ScrollView {
  private View mByWhichView;
  private View mTitleView;
  private boolean shouldSlowlyChange = true;
  public ScrollChangeScrollView(Context context) {
    super(context);
  }
  public ScrollChangeScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }
  public ScrollChangeScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public ScrollChangeScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
  }
  @Override
  public void scrollTo(int x, int y) {
    //這是為了修復(fù)noScrllListView嵌套在srcollview時(shí)就自動(dòng)滑動(dòng)到noscrolllistview的頂部的bug,不影響使用
    if (x == 0 && y == 0 || y <= 0) {
      super.scrollTo(x, y);
    }
  }
  public void setListener(OnScrollListener listener){
    this.mListener = listener;
  }
  public void setShouldSlowlyChange(boolean slowlyChange) {
    this.shouldSlowlyChange = slowlyChange;
  }
  /**
   * 設(shè)置透明度漸變的標(biāo)題view
   * @param view
   */
  public void setupTitleView (View view) {
    this.mTitleView = view;
  }
  /**
   * 跟隨的view
   * @param view
   */
  public void setupByWhichView(View view) {
    mByWhichView = view;
  }
  @Override
  protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX,
                 boolean clampedY) {
    super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    if (scrollY >= mByWhichView.getTop() + mByWhichView.getMeasuredHeight()) {
      mTitleView.setBackgroundColor(Color.BLACK);
    } else if (scrollY>=0) {
      if (!shouldSlowlyChange) {
        mTitleView.setBackgroundColor(Color.TRANSPARENT);
      } else {
        float persent = scrollY * 1f / (mByWhichView.getTop() + mByWhichView.getMeasuredHeight());
        int alpha = (int) (255 * persent);
        int color = Color.argb(alpha,0,0,0);
        mTitleView.setBackgroundColor(color);
      }
    }
    if (mListener!=null) {
      mListener.onScroll(scrollX, scrollY);
    }
  }
}

效果如下:

滑動(dòng)前

滑動(dòng)變色中

參考的view超出屏幕后

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android基本組件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論