HeadZoomLayout

68747470733a2f2f6a69747061636b2e696f2f762f6f6c642d74726176656c65722f486561645a6f6f6d4c61796f75742e737667  

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6275696c642d70617373696e672d677265656e2e737667

一个可以下拉放大头部背景图的布局控件,可轻松实现下拉放大头部背景图和QQ个人信息页面下拉扩展并放大背景图效果

非入侵式,不干扰业务代码、不影响子View的滑动。类似于官方的SwipeRefreshLayout

1.0.5之后迁移androidx

可包裹以下控件使用

RecyclerView

ListView

ScrollView

NestedScrollView

LinearLayout等

效果展示

653ede1587281eb95c24ea1e7ce1cd3d.png

使用

定义布局时需要声明头部视图的id{@link HeadZoomLayout#headViewId},

同时需要将头部中的背景图片(ImageView对象)scaleType设置为centerCrop

如需实现类似于QQ个人信息界面中下拉扩展并放大效果只需保证头部的宽高比大于图片的宽高比即可。

allprojects {

repositories {

//...

maven { url 'https://jitpack.io' }

}

}

dependencies {

implementation 'com.github.old-traveler:HeadZoomLayout:Tag'

}

属性名称

属性说明

默认值

headViewId

头部视图id

0

maxZoomRatio

头部最大放大比例

1.0f

zoomEnable

是否可以放大头部

true

maxDragDistance

值越大,阻尼越大

1000f

dragAccelerationRatio

值越大,阻尼越大

3.0f

useDecelerateInterpolator

回弹动画使用减速插值器

true

maxRecoverTime

最大回弹动画时间

400L

嵌套LinearLayout使用

android:layout_width="match_parent"

android:layout_height="match_parent"

app:headViewId="@id/fl_head">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:id="@+id/fl_head"

android:layout_width="match_parent"

android:layout_height="150dp"

android:background="@color/colorAccent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scaleType="centerCrop"

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

//...

//...

嵌套ScrollView或NestedScrollView

android:layout_width="match_parent"

android:layout_height="match_parent"

app:headViewId="@id/fl_head">

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:id="@+id/fl_head"

android:layout_width="match_parent"

android:layout_height="150dp"

android:background="@color/colorAccent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scaleType="centerCrop"

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

//...

//...

嵌套RecyclerView或ListView

使用动态添加Head的形式并将其id设置HeadZoomLayout中headViewId

or

多样式布局时设置第一个Item的布局id为HeadZoomLayout中的headViewId

app:headViewId="@id/fl_head"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="match_parent"

android:layout_height="match_parent"/>

android:id="@+id/fl_head"

android:layout_width="match_parent"

android:layout_height="150dp"

android:background="@color/colorAccent">

android:layout_width="match_parent"

android:layout_height="match_parent"

android:scaleType="centerCrop"

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

//...

注意事项

必须给代表背景的ImageView设置centerCrop剪裁模式

如需实现qq那种先扩展再放大的效果,需保证图片的高宽比大于ImageView的高宽比

如果使用Glide或者其他会对图片进行剪裁的图片加载库时会造成无法实现qq那种扩展放大效果。可通过ImageView.setImageDrawable或者以setImageBitmap加载来解决

以Glide为例子,通过Drawable加载可解决问题:

Glide.with(context)

.asDrawable()

.load(Constant.BASE_IMAGE_URL + url)

.into(new SimpleTarget() {

@Override

public void onResourceReady(@NonNull Drawable resource,

@Nullable Transition super Drawable> transition) {

imageView.setImageDrawable(resource);

}

});

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐