【移動應用開發(fā)技術】Android如何實現(xiàn)沉浸式狀態(tài)欄+actionBar漸變+scrollView頂部伸縮效果_第1頁
【移動應用開發(fā)技術】Android如何實現(xiàn)沉浸式狀態(tài)欄+actionBar漸變+scrollView頂部伸縮效果_第2頁
【移動應用開發(fā)技術】Android如何實現(xiàn)沉浸式狀態(tài)欄+actionBar漸變+scrollView頂部伸縮效果_第3頁
【移動應用開發(fā)技術】Android如何實現(xiàn)沉浸式狀態(tài)欄+actionBar漸變+scrollView頂部伸縮效果_第4頁
【移動應用開發(fā)技術】Android如何實現(xiàn)沉浸式狀態(tài)欄+actionBar漸變+scrollView頂部伸縮效果_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發(fā)技術】Android如何實現(xiàn)沉浸式狀態(tài)欄+actionBar漸變+scrollView頂部伸縮效果

/upload/information/20200623/125/125058.gif<style

name="TranslucentTheme"

parent="@style/AppTheme">

</style><style

name="TranslucentTheme"

parent="@style/AppTheme">

<item

name="android:windowTranslucentStatus">true</item>

<item

name="android:windowTranslucentNavigation">false</item>

</style><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漸變package

.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

.R;

import

.impl.ActionBarClickListener;

/**

*

支持漸變的

actionBar

*

Created

by

暉仔(Milo)

on

2016/12/28.

*

email:303767416@

*/

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

}

/**

*

設置狀態(tài)欄高度

*

*

@param

statusBarHeight

*/

public

void

setStatusBarHeight(int

statusBarHeight)

{

ViewGroup.LayoutParams

params

=

vStatusBar.getLayoutParams();

params.height

=

statusBarHeight;

vStatusBar.setLayoutParams(params);

}

/**

*

設置是否需要漸變

*/

public

void

setNeedTranslucent()

{

setNeedTranslucent(true,

false);

}

/**

*

設置是否需要漸變,并且隱藏標題

*

*

@param

translucent

*/

public

void

setNeedTranslucent(boolean

translucent,

boolean

titleInitVisibile)

{

if

(translucent)

{

layRoot.setBackgroundDrawable(null);

}

if

(!titleInitVisibile)

{

tvTitle.setVisibility(View.GONE);

}

}

/**

*

設置標題

*

*

@param

strTitle

*/

public

void

setTitle(String

strTitle)

{

if

(!TextUtils.isEmpty(strTitle))

{

tvTitle.setText(strTitle);

}

else

{

tvTitle.setVisibility(View.GONE);

}

}

/**

*

設置數(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();

}

});

}

}

}<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/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"

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"

android:layout_centerInParent="true"

android:text="標題"

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"

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>/**

*

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

*

*

@return

*/

public

int

getStatusBarHeight()

{

//獲取status_bar_height資源的ID

int

resourceId

=

getResources().getIdentifier("status_bar_height",

"dimen",

"android");

if

(resourceId

>

0)

{

//根據(jù)資源ID獲取響應的尺寸值

return

getResources().getDimensionPixelSize(resourceId);

}

return

0;

}/**

*

設置狀態(tài)欄高度

*

*

@param

statusBarHeight

*/

public

void

setStatusBarHeight(int

statusBarHeight)

{

ViewGroup.LayoutParams

params

=

vStatusBar.getLayoutParams();

params.height

=

statusBarHeight;

vStatusBar.setLayoutParams(params);

}/**

*

設置是否需要漸變

*/

public

void

setNeedTranslucent()

{

setNeedTranslucent(true,

false);

}

/**

*

設置是否需要漸變,并且隱藏標題

*

*

@param

translucent

*/

public

void

setNeedTranslucent(boolean

translucent,

boolean

titleInitVisibile)

{

if

(translucent)

{

layRoot.setBackgroundDrawable(null);

}

if

(!titleInitVisibile)

{

tvTitle.setVisibility(View.GONE);

}

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

.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

.R;

import

.utils.SizeUtils;

/**

*

Created

by

暉仔(Milo)

on

2017/2/13.

*

email:303767416@

*/

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;

//漸變結束位置

private

int

transEndY

=

300;

//漸變開始默認位置,Y軸,50dp

private

final

int

DFT_TRANSSTARTY

=

50;

//漸變結束默認位置,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;

}

/**

*

設置伸縮視圖

*

*

@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();

}

});

}

}

/**

*

設置漸變視圖

*

*

@param

transView

漸變的視圖

*/

public

void

setTransView(View

transView)

{

setTransView(transView,

getResources().getColor(R.color.colorPrimary),

SizeUtils.dip2px(getContext(),

DFT_TRANSSTARTY),

SizeUtils.dip2px(getContext(),

DFT_TRANSENDY));

}

/**

*

設置漸變視圖

*

*

@param

transView

漸變的視圖

*

@param

transColor

漸變顏色

*

@param

transEndY

漸變結束位置

*/

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當前高度

//

設置動畫

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

}

@Overri

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論