国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android沉浸式狀態(tài)欄 + actionBar漸變 + scrollView頂部伸縮效果

2019-10-22 18:20:47
字體:
供稿:網(wǎng)友

閑話不多說,直接上圖。

android,沉浸式狀態(tài)欄,actionBar,漸變,scrollView,頂部伸縮

給大家講講我的編程思想吧。

第一部分:沉浸式狀態(tài)欄(API-Level 19, Android4.4 KitKat 之后加入的東西),而且在Api-Level 21版本中新增了一個屬性(下面會說到)。所以,style文件應(yīng)該聲明三份。

values

<style name="TranslucentTheme" parent="@style/AppTheme"></style>

values-19

<style name="TranslucentTheme" parent="@style/AppTheme"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">false</item></style>

values-V21

<style name="TranslucentTheme" parent="@style/AppTheme"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">false</item> <!-- v-21 中新增的屬性 --> <item name="android:statusBarColor">@android:color/transparent</item></style>

至于以上屬性的含義及使用方式,就不多做解釋了。

第二部分:actionBar漸變

因?yàn)橐獙?shí)現(xiàn)actionBar漸變,所以我沒有使用系統(tǒng)的actionBar。而是自定義了一個繼承自LinearLayout的ViewGroup。

直接給各位看代碼

package test.com.widget; import android.content.Context; import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import test.com.R; import test.com.impl.ActionBarClickListener; /**  * 支持漸變的 actionBar  * Created by 暉仔(Milo) on 2016/12/28.  * email:303767416@qq.com  */ public final class TranslucentActionBar extends LinearLayout {  private View layRoot;  private View vStatusBar;  private View layLeft;  private View layRight;  public TextView tvTitle;  private TextView tvLeft;  private TextView tvRight;  private View iconLeft;  private View iconRight;  public TranslucentActionBar(Context context) {   this(context, null);  }  public TranslucentActionBar(Context context, AttributeSet attrs) {   super(context, attrs);   init();  }  public TranslucentActionBar(Context context, AttributeSet attrs, int defStyleAttr) {   super(context, attrs, defStyleAttr);  }  private void init() {   setOrientation(HORIZONTAL);   View contentView = inflate(getContext(), R.layout.actionbar_trans, this);   layRoot = contentView.findViewById(R.id.lay_transroot);   vStatusBar = contentView.findViewById(R.id.v_statusbar);   tvTitle = (TextView) contentView.findViewById(R.id.tv_actionbar_title);   tvLeft = (TextView) contentView.findViewById(R.id.tv_actionbar_left);   tvRight = (TextView) contentView.findViewById(R.id.tv_actionbar_right);   iconLeft = contentView.findViewById(R.id.iv_actionbar_left);   iconRight = contentView.findViewById(R.id.v_actionbar_right);  }  /**   * 設(shè)置狀態(tài)欄高度   *   * @param statusBarHeight   */  public void setStatusBarHeight(int statusBarHeight) {   ViewGroup.LayoutParams params = vStatusBar.getLayoutParams();   params.height = statusBarHeight;   vStatusBar.setLayoutParams(params);  }  /**   * 設(shè)置是否需要漸變   */  public void setNeedTranslucent() {   setNeedTranslucent(true, false);  }  /**   * 設(shè)置是否需要漸變,并且隱藏標(biāo)題   *   * @param translucent   */  public void setNeedTranslucent(boolean translucent, boolean titleInitVisibile) {   if (translucent) {    layRoot.setBackgroundDrawable(null);   }   if (!titleInitVisibile) {    tvTitle.setVisibility(View.GONE);   }  }  /**   * 設(shè)置標(biāo)題   *   * @param strTitle   */  public void setTitle(String strTitle) {   if (!TextUtils.isEmpty(strTitle)) {    tvTitle.setText(strTitle);   } else {    tvTitle.setVisibility(View.GONE);   }  }  /**   * 設(shè)置數(shù)據(jù)   *   * @param strTitle   * @param resIdLeft   * @param strLeft   * @param resIdRight   * @param strRight   * @param listener   */  public void setData(String strTitle, int resIdLeft, String strLeft, int resIdRight, String strRight, final ActionBarClickListener listener) {   if (!TextUtils.isEmpty(strTitle)) {    tvTitle.setText(strTitle);   } else {    tvTitle.setVisibility(View.GONE);   }   if (!TextUtils.isEmpty(strLeft)) {    tvLeft.setText(strLeft);    tvLeft.setVisibility(View.VISIBLE);   } else {    tvLeft.setVisibility(View.GONE);   }   if (!TextUtils.isEmpty(strRight)) {    tvRight.setText(strRight);    tvRight.setVisibility(View.VISIBLE);   } else {    tvRight.setVisibility(View.GONE);   }   if (resIdLeft == 0) {    iconLeft.setVisibility(View.GONE);   } else {    iconLeft.setBackgroundResource(resIdLeft);    iconLeft.setVisibility(View.VISIBLE);   }   if (resIdRight == 0) {    iconRight.setVisibility(View.GONE);   } else {    iconRight.setBackgroundResource(resIdRight);    iconRight.setVisibility(View.VISIBLE);   }   if (listener != null) {    layLeft = findViewById(R.id.lay_actionbar_left);    layRight = findViewById(R.id.lay_actionbar_right);    layLeft.setOnClickListener(new View.OnClickListener() {     @Override     public void onClick(View v) {      listener.onLeftClick();     }    });    layRight.setOnClickListener(new View.OnClickListener() {     @Override     public void onClick(View v) {      listener.onRightClick();     }    });   }  } } 

下面是actionbar_trans.xml的代碼

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/lay_transroot"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:background="@color/colorPrimary"  android:orientation="vertical">  <View   android:id="@+id/v_statusbar"   android:layout_width="match_parent"   android:layout_height="1.0dp" />  <RelativeLayout   android:layout_width="match_parent"   android:layout_height="45dp"   android:orientation="vertical">   <RelativeLayout    android:id="@+id/lay_actionbar_left"    android:layout_width="100dp"    android:layout_height="match_parent"    android:orientation="horizontal">    <ImageView     android:id="@+id/iv_actionbar_left"     android:layout_width="20dp"     android:layout_height="20dp"     android:layout_centerVertical="true"     android:layout_marginLeft="10dp"     android:background="@mipmap/ic_left_light"     android:visibility="gone" />    <TextView     android:id="@+id/tv_actionbar_left"     style="@style/text_white"     android:layout_height="match_parent"     android:layout_marginLeft="10dp"     android:layout_toRightOf="@+id/iv_actionbar_left"     android:gravity="center_vertical"     android:maxLength="2"     android:singleLine="true"     android:text="返回"     android:visibility="gone" />   </RelativeLayout>   <TextView    android:id="@+id/tv_actionbar_title"    style="@style/text_white"    android:layout_centerInParent="true"    android:text="標(biāo)題"    android:textSize="16sp" />   <RelativeLayout    android:id="@+id/lay_actionbar_right"    android:layout_width="100dp"    android:layout_height="match_parent"    android:layout_alignParentRight="true"    android:gravity="right"    android:orientation="horizontal">    <View     android:id="@+id/v_actionbar_right"     android:layout_width="20dp"     android:layout_height="20dp"     android:layout_alignParentRight="true"     android:layout_centerVertical="true"     android:layout_marginRight="10dp"     android:visibility="gone" />    <TextView     android:id="@+id/tv_actionbar_right"     style="@style/text_white"     android:layout_height="match_parent"     android:layout_marginRight="10dp"     android:layout_toLeftOf="@+id/v_actionbar_right"     android:gravity="center_vertical|right"     android:singleLine="true"     android:visibility="gone" />   </RelativeLayout>  </RelativeLayout> </LinearLayout> 

這里我即沒有用到 android:fitsSystemWindows="true" 屬性,也沒有用到 StatusBarUtils ,因?yàn)槲野l(fā)現(xiàn)使用的時(shí)候很容易造成兼容問題。

所以,我的做法是聲明了一個高度為0.0dp的 statusbar,背景為透明,然后獲取狀態(tài)欄高度并賦值到它上,來實(shí)現(xiàn)兼容。事實(shí)證明,這樣做的兼容效果最好。

獲取狀態(tài)欄高度代碼:

/**  * 獲取狀態(tài)欄高度  *  * @return  */ public int getStatusBarHeight() {  //獲取status_bar_height資源的ID  int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");  if (resourceId > 0) {   //根據(jù)資源ID獲取響應(yīng)的尺寸值   return getResources().getDimensionPixelSize(resourceId);  }  return 0; } 

設(shè)置 statusbar高度:

/**  * 設(shè)置狀態(tài)欄高度  *  * @param statusBarHeight  */ public void setStatusBarHeight(int statusBarHeight) {  ViewGroup.LayoutParams params = vStatusBar.getLayoutParams();  params.height = statusBarHeight;  vStatusBar.setLayoutParams(params); } 

開啟漸變:

/**  * 設(shè)置是否需要漸變  */  public void setNeedTranslucent() {   setNeedTranslucent(true, false);  }  /**  * 設(shè)置是否需要漸變,并且隱藏標(biāo)題  *  * @param translucent  */  public void setNeedTranslucent(boolean translucent, boolean titleInitVisibile) {   if (translucent) {    layRoot.setBackgroundDrawable(null);   }   if (!titleInitVisibile) {    tvTitle.setVisibility(View.GONE);   }  } 

第三步:實(shí)現(xiàn)ScrollView頂部伸縮

到了這里,必須得說一下,因?yàn)槭莻€人項(xiàng)目中用到,所以并沒有把功能做的很強(qiáng)大,本人都是以最簡單、有效的方式實(shí)現(xiàn)的。所以,代碼并不像gitHub上那些被下載很多次的開源項(xiàng)目一樣,有很高的擴(kuò)展性。
時(shí)間關(guān)系,我直接貼代碼吧,代碼里我都寫了注釋的。

package test.com.widget;  import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.content.Context; import android.graphics.Color; import android.support.annotation.ColorInt; import android.support.v4.graphics.ColorUtils; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.ScrollView; import test.com.R; import test.com.utils.SizeUtils; /**  * Created by 暉仔(Milo) on 2017/2/13.  * email:303767416@qq.com  */ public class TranslucentScrollView extends ScrollView {  static final String TAG = "TranslucentScrollView";  //伸縮視圖  private View zoomView;  //伸縮視圖初始高度  private int zoomViewInitHeight = 0;  // 記錄首次按下位置  private float mFirstPosition = 0;  // 是否正在放大  private Boolean mScaling = false;  //漸變的視圖  private View transView;  //漸變顏色  private int transColor = Color.WHITE;  //漸變開始位置  private int transStartY = 50;  //漸變結(jié)束位置  private int transEndY = 300;  //漸變開始默認(rèn)位置,Y軸,50dp  private final int DFT_TRANSSTARTY = 50;  //漸變結(jié)束默認(rèn)位置,Y軸,300dp  private final int DFT_TRANSENDY = 300;  private TranslucentScrollView.TranslucentChangedListener translucentChangedListener;  public interface TranslucentChangedListener {   /**    * 透明度變化,取值范圍0-255    *    * @param transAlpha    */   void onTranslucentChanged(int transAlpha);  }  public TranslucentScrollView(Context context) {   super(context);  }  public TranslucentScrollView(Context context, AttributeSet attrs) {   super(context, attrs);  }  public TranslucentScrollView(Context context, AttributeSet attrs, int defStyleAttr) {   super(context, attrs, defStyleAttr);  }  public void setTranslucentChangedListener(TranslucentScrollView.TranslucentChangedListener translucentChangedListener) {   this.translucentChangedListener = translucentChangedListener;  }  /**   * 設(shè)置伸縮視圖   *   * @param zoomView   */  public void setPullZoomView(View zoomView) {   this.zoomView = zoomView;   zoomViewInitHeight = zoomView.getLayoutParams().height;   if (zoomViewInitHeight == LayoutParams.MATCH_PARENT || zoomViewInitHeight == WindowManager.LayoutParams.WRAP_CONTENT) {    zoomView.post(new Runnable() {     @Override     public void run() {      zoomViewInitHeight = TranslucentScrollView.this.zoomView.getHeight();     }    });   }  }  /**   * 設(shè)置漸變視圖   *   * @param transView 漸變的視圖   */  public void setTransView(View transView) {   setTransView(transView, getResources().getColor(R.color.colorPrimary), SizeUtils.dip2px(getContext(), DFT_TRANSSTARTY), SizeUtils.dip2px(getContext(), DFT_TRANSENDY));  }  /**   * 設(shè)置漸變視圖   *   * @param transView 漸變的視圖   * @param transColor 漸變顏色   * @param transEndY 漸變結(jié)束位置   */  public void setTransView(View transView, @ColorInt int transColor, int transStartY, int transEndY) {   this.transView = transView;   //初始視圖-透明   this.transView.setBackgroundColor(ColorUtils.setAlphaComponent(transColor, 0));   this.transStartY = transStartY;   this.transEndY = transEndY;   this.transColor = transColor;   if (transStartY > transEndY) {    throw new IllegalArgumentException("transStartY 不得大于 transEndY .. ");   }  }  /**   * 獲取透明度   *   * @return   */  private int getTransAlpha() {   float scrollY = getScrollY();   if (transStartY != 0) {    if (scrollY <= transStartY) {     return 0;    } else if (scrollY >= transEndY) {     return 255;    } else {     return (int) ((scrollY - transStartY) / (transEndY - transStartY) * 255);    }   } else {    if (scrollY >= transEndY) {     return 255;    }    return (int) ((transEndY - scrollY) / transEndY * 255);   }  }  /**   * 重置ZoomView   */  private void resetZoomView() {   final ViewGroup.LayoutParams lp = zoomView.getLayoutParams();   final float h = zoomView.getLayoutParams().height;// ZoomView當(dāng)前高度   // 設(shè)置動畫   ValueAnimator anim = ObjectAnimator.ofFloat(0.0F, 1.0F).setDuration(200);   anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {    @Override    public void onAnimationUpdate(ValueAnimator animation) {     float cVal = (Float) animation.getAnimatedValue();     lp.height = (int) (h - (h - zoomViewInitHeight) * cVal);     zoomView.setLayoutParams(lp);    }   });   anim.start();  }  @Override  protected void onScrollChanged(int l, int t, int oldl, int oldt) {   super.onScrollChanged(l, t, oldl, oldt);   int transAlpha = getTransAlpha();   if (transView != null) {    Log.d(TAG, "[onScrollChanged .. in ], 透明度 == " + transAlpha);    transView.setBackgroundColor(ColorUtils.setAlphaComponent(transColor, transAlpha));   }   if (translucentChangedListener != null) {    translucentChangedListener.onTranslucentChanged(transAlpha);   }  }  @Override  public boolean onTouchEvent(MotionEvent event) {   if (zoomView != null) {    ViewGroup.LayoutParams params = zoomView.getLayoutParams();    switch (event.getAction()) {     case MotionEvent.ACTION_UP:      //手指離開后恢復(fù)圖片      mScaling = false;      resetZoomView();      break;     case MotionEvent.ACTION_MOVE:      if (!mScaling) {       if (getScrollY() == 0) {        mFirstPosition = event.getY();       } else {        break;       }      }      int distance = (int) ((event.getY() - mFirstPosition) * 0.6);      if (distance < 0) {       break;      }      mScaling = true;      params.height = zoomViewInitHeight + distance;      Log.d(TAG, "params.height == " + params.height + ", zoomViewInitHeight == " + zoomViewInitHeight + ", distance == " + distance);      zoomView.setLayoutParams(params);      return true;    }   }   return super.onTouchEvent(event);  } } 

總結(jié)

以上所述是小編給大家介紹的Android沉浸式狀態(tài)欄 + actionBar漸變 + scrollView頂部伸縮,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 济宁市| 库尔勒市| 万全县| 顺义区| 桦甸市| 化隆| 彭水| 温泉县| 格尔木市| 上杭县| 绿春县| 元阳县| 北海市| 龙门县| 威信县| 梓潼县| 拜城县| 洱源县| 紫金县| 额济纳旗| 临清市| 陵水| 安吉县| 长葛市| 拉孜县| 泰和县| 章丘市| 榆林市| 龙井市| 达孜县| 屏边| 页游| 尼勒克县| 定襄县| 新宁县| 文水县| 扶绥县| 长兴县| 威海市| 宕昌县| 普洱|