Android仿支付宝UI功能开发,UI实例-->仿支付宝首页头部伸缩效果

658204b937b8?tdsourcetag=s_pctim_aiomsg

1. 效果图

下面是我们将要实现的效果图:

658204b937b8?tdsourcetag=s_pctim_aiomsg

效果图

2.具体实现

我们可以利用design和v7包中的控件来实现(涉及到的控件有CoordinatorLayout、AppBarLayout、Toolbar、NestedScrollView)。

第一步:创建一个Scrolling Activity

下面是该activity的界面效果图

658204b937b8?tdsourcetag=s_pctim_aiomsg

Scrolling Activity

第二步:编写最终效果图需要用到的xml

在第一步的基础上构造出我们最终想要的效果图。

(1) include_toolbar_open.xml

该布局是头部展开时toolbar对应的效果图:

658204b937b8?tdsourcetag=s_pctim_aiomsg

头部展开时toolbar的效果图

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@color/colorPrimary"

>

android:layout_margin="@dimen/dimen_10dp"

android:layout_toLeftOf="@+id/iv_contact"

android:background="@drawable/shape_search"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingLeft="@dimen/dimen_10dp"

android:paddingRight="@dimen/dimen_10dp">

android:id="@+id/iv_search"

android:layout_width="20dp"

android:layout_height="20dp"

android:layout_centerVertical="true"

android:src="@mipmap/ic_search"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_centerVertical="true"

android:layout_toRightOf="@id/iv_search"

android:hint="搜索商品"

android:background="@null"

android:paddingLeft="@dimen/dimen_10dp"

android:textColorHint="@android:color/white"

android:textSize="14sp"/>

android:id="@+id/iv_plus"

android:layout_width="20dp"

android:layout_height="20dp"

android:layout_alignParentRight="true"

android:layout_centerVertical="true"

android:layout_marginRight="@dimen/dimen_10dp"

android:src="@mipmap/ic_add"/>

android:id="@+id/iv_contact"

android:layout_width="25dp"

android:layout_height="25dp"

android:layout_centerVertical="true"

android:layout_marginRight="@dimen/dimen_10dp"

android:layout_toLeftOf="@id/iv_plus"

android:src="@mipmap/ic_contact"/>

android:id="@+id/bg_toolbar_open"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

(2) include_toolbar_close.xml

该布局是头部收缩时toolbar对应的效果图:

658204b937b8?tdsourcetag=s_pctim_aiomsg

头部收缩时toolbar的效果图

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="50dp"

android:background="@color/colorPrimary">

android:id="@+id/iv_scan"

android:layout_width="25dp"

android:layout_height="25dp"

android:layout_alignParentLeft="true"

android:layout_centerVertical="true"

android:layout_marginLeft="20dp"

android:src="@mipmap/ic_scan"/>

android:id="@+id/iv_pay"

android:layout_width="25dp"

android:layout_height="25dp"

android:layout_centerVertical="true"

android:layout_marginLeft="20dp"

android:layout_toRightOf="@+id/iv_scan"

android:src="@mipmap/ic_payment"/>

android:id="@+id/iv_charge"

android:layout_width="25dp"

android:layout_height="25dp"

android:layout_centerVertical="true"

android:layout_marginLeft="20dp"

android:layout_toRightOf="@+id/iv_pay"

android:src="@mipmap/ic_transfer"/>

android:layout_width="25dp"

android:layout_height="25dp"

android:layout_centerVertical="true"

android:layout_marginLeft="20dp"

android:layout_toRightOf="@+id/iv_charge"

android:src="@mipmap/ic_card"/>

android:id="@+id/iv_plus"

android:layout_width="25dp"

android:layout_height="25dp"

android:layout_alignParentRight="true"

android:layout_centerVertical="true"

android:layout_marginRight="20dp"

android:layout_toRightOf="@+id/iv_scan"

android:src="@mipmap/ic_add"/>

android:id="@+id/bg_toolbar_close"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

(3) include_open.xml

该布局是头部打开时折叠区对应的效果图:

658204b937b8?tdsourcetag=s_pctim_aiomsg

头部打开时折叠区对应的效果图

xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@color/colorPrimary">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:src="@mipmap/ic_scan"/>

android:layout_gravity="center_horizontal"

android:layout_marginTop="5dp"

android:text="扫一扫"

android:textColor="@color/white"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:src="@mipmap/ic_payment"/>

android:layout_gravity="center_horizontal"

android:layout_marginTop="5dp"

android:text="付钱"

android:textColor="@color/white"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:src="@mipmap/ic_transfer"/>

android:layout_gravity="center_horizontal"

android:layout_marginTop="5dp"

android:text="收钱"

android:textColor="@color/white"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:layout_weight="1"

android:layout_width="0dp"

android:layout_height="match_parent"

android:gravity="center"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:src="@mipmap/ic_card"/>

android:layout_gravity="center_horizontal"

android:layout_marginTop="5dp"

android:text="卡包"

android:textColor="@color/white"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/bg_content"

android:layout_width="match_parent"

android:layout_height="100dp"/>

(3) content_zfbhome_telescopic.xml

内容区

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:layout_behavior="@string/appbar_scrolling_view_behavior"

tools:context=".activity.ZFBHomeTelescopicActivity"

tools:showIn="@layout/activity_zfbhome_telescopic">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="@dimen/text_margin"

android:text="@string/large_text" />

(3) activity_zfbhome_telescopic.xml

最终的在activity布局里的效果图:

658204b937b8?tdsourcetag=s_pctim_aiomsg

最终效果图

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

tools:context=".activity.ZFBHomeTelescopicActivity">

android:id="@+id/app_bar"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/toolbar_layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

app:contentScrim="?attr/colorPrimary"

app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"

>

layout="@layout/include_open"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="50dp"

app:layout_collapseMode="parallax"

app:layout_collapseParallaxMultiplier="0.7"/>

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="50dp"

app:contentInsetLeft="0dp"

app:contentInsetStart="0dp"

app:layout_collapseMode="pin" >

layout="@layout/include_toolbar_open"/>

layout="@layout/include_toolbar_close"/>

注:布局中涉及到的一些属性具体的作用就不做阐述了,可以自行查阅。

第三步:主Activity(ZFBHomeTelescopicActivity.java)

我们需要对AppBarLayout设置偏移监听,需要实现一些背景色缩放效果和收缩展开时toolbar对应布局的显示。

public class ZFBHomeTelescopicActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {

@BindView(R.id.bg_content)

View bgContent; //大布局背景遮罩层

@BindView(R.id.iv_search)

ImageView ivSearch; //

@BindView(R.id.iv_plus)

ImageView ivPlus;

@BindView(R.id.iv_contact)

ImageView ivContact;

@BindView(R.id.iv_scan)

ImageView ivScan;

@BindView(R.id.iv_pay)

ImageView ivPay;

@BindView(R.id.iv_charge)

ImageView ivCharge;

@BindView(R.id.bg_toolbar_close)

View bgToolbarClose; //收缩状态下toolbar的遮罩层

@BindView(R.id.toolbar)

Toolbar toolbar;

@BindView(R.id.toolbar_layout)

CollapsingToolbarLayout toolbarLayout;

@BindView(R.id.app_bar)

AppBarLayout appBar;

@BindView(R.id.bg_toolbar_open) //展开状态下toolbar的遮罩层

View bgToolbarOpen;

private View toolbarOpen; //展开状态下toolbar显示的内容

private View toolbarClose; //收缩状态下toolbar显示的内容

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_zfbhome_telescopic);

ButterKnife.bind(this);

toolbarOpen = findViewById(R.id.include_toolbar_open);

toolbarClose = findViewById(R.id.include_toolbar_close);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

appBar.addOnOffsetChangedListener(this);

}

@Override

public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

//垂直方向偏移

int offset = Math.abs(verticalOffset);

//最大偏移距离

int scrollRange = appBarLayout.getTotalScrollRange();

//当滑动没超过一半时,展开状态下toolbar显示内容,根据收缩位置,改变透明值

if (offset <= scrollRange / 2){

toolbarOpen.setVisibility(View.VISIBLE);

toolbarClose.setVisibility(View.GONE);

//根据偏移百分比,计算透明值

float scale2 = (float) offset / (scrollRange / 2);

int alpha2 = (int) (255 * scale2);

bgToolbarOpen.setBackgroundColor(Color.argb(alpha2,25,131,209));

}else {//当滑动超过一半,收缩状态下toolbar显示内容,根据收缩位置,改变透明值

toolbarClose.setVisibility(View.VISIBLE);

toolbarOpen.setVisibility(View.GONE);

float scale3 = (float) (scrollRange - offset) / (scrollRange / 2);

int alpha3 = (int) (255 * scale3);

bgToolbarClose.setBackgroundColor(Color.argb(alpha3,25,131,209));

}

//根据偏移值百分比计算扫一扫布局的透明度值

float scale = (float) offset / scrollRange;

int alpha = (int) (255 * scale);

bgContent.setBackgroundColor(Color.argb(alpha,25,131,209));

}

@Override

protected void onDestroy() {

super.onDestroy();

appBar.removeOnOffsetChangedListener(this);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值