android 布局北京渐变,怎么在Android中利用Scrollview实现一个滑动渐变效果

怎么在Android中利用Scrollview实现一个滑动渐变效果

发布时间:2021-01-29 15:53:33

来源:亿速云

阅读:106

作者:Leah

本篇文章给大家分享的是有关怎么在Android中利用Scrollview实现一个滑动渐变效果,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。public class MyScrollView extends ScrollView {

private TranslucentListener mTranslucentListener;

public void setTranslucentListener(TranslucentListener translucentListener) {

this.mTranslucentListener = translucentListener;

}

public MyScrollView(Context context) {

this(context, null);

}

public MyScrollView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {

this(context, attrs, defStyleAttr, 0);

}

public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

}

@Override

protected void onScrollChanged(int l, int t, int oldl, int oldt) {

super.onScrollChanged(l, t, oldl, oldt);

if (mTranslucentListener != null) {

//ScrollView滑出高度

int scrollY = getScrollY();

//屏幕高度

int screenHeight = getContext().getResources().getDisplayMetrics().heightPixels;

//有效滑动距离为屏幕2分之一

// alpha = 滑动高度/(screenHeight/3f)

if (scrollY <= screenHeight / 2f) {

Log.d(">>>>>>>>>", "ScrollView划出高度:" + scrollY);

Log.d(">>>>>>>>>", "屏幕高度:" + screenHeight);

Log.d(">>>>>>>>>", "渐变值:" + (0 + scrollY / (screenHeight / 4f)));

// 渐变的过程 1~0

mTranslucentListener.onTranslucent(0 + scrollY / (screenHeight /4f));

}

}

}

}

Activity 设置public class ToolbarActivity extends AppCompatActivity implements TranslucentListener {

private Toolbar mToolBar;

private MyScrollView mScrollView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_toobar);

mToolBar = findViewById(R.id.id_toolbar);

mScrollView = findViewById(R.id.id_scrollView);

//初始化渐变为0

mToolBar.setAlpha(0);

//设置渐变回调

mScrollView.setTranslucentListener(this);

}

@Override

public void onTranslucent(float alpha) {

mToolBar.setAlpha(alpha);

}

}

渐变回调接口/**

* @ClassName TranslucentListener

* @Author rex

* @Date 2021/1/27 17:38

*/

public interface TranslucentListener {

/**

* 透明度的回调监听

*

* @param alpha 0~1 透明度

*/

public void onTranslucent(float alpha);

}

布局文件

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

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/id_scrollView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:clipChildren="false"

android:clipToPadding="false"

>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:id="@+id/button1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button0" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button1" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button2" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button3" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button4" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button5" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button5" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button5" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button5" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button5" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button5" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button5" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button5" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button" />

android:layout_width="match_parent"

android:layout_height="match_parent"

android:text="Button" />

android:id="@+id/id_toolbar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@color/colorAccent"

app:title="title"

/>

下滑显示上滑渐变消失/**

* @ClassName MyScrollView

* @Author Rex

* @Date 2021/1/27 17:38

*/

public class MyScrollView extends ScrollView {

private TranslucentListener mTranslucentListener;

public void setTranslucentListener(TranslucentListener translucentListener) {

this.mTranslucentListener = translucentListener;

}

public MyScrollView(Context context) {

this(context, null);

}

public MyScrollView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {

this(context, attrs, defStyleAttr, 0);

}

public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {

super(context, attrs, defStyleAttr, defStyleRes);

}

@Override

protected void onScrollChanged(int l, int t, int oldl, int oldt) {

super.onScrollChanged(l, t, oldl, oldt);

if (mTranslucentListener != null) {

//ScrollView滑出高度

int scrollY = getScrollY();

//屏幕高度

int screenHeight = getContext().getResources().getDisplayMetrics().heightPixels;

//有效滑动距离为屏幕2分之一

// alpha = 滑动高度/(screenHeight/3f)

if (scrollY <= screenHeight / 2f) {

Log.d(">>>>>>>>>", "ScrollView划出高度:" + scrollY);

Log.d(">>>>>>>>>", "屏幕高度:" + screenHeight);

Log.d(">>>>>>>>>", "渐变值:" + (1 - scrollY / (screenHeight / 4f)));

// 渐变的过程 1~0

mTranslucentListener.onTranslucent(1 - scrollY / (screenHeight /4f));

}

}

}

}

注意: 这里只是更改了 mTranslucentListener.onTranslucent 里的 渐变值

Activty 里 把初始化 mToolBar.setAlpha(0); 去掉

XML

android:id="@+id/id_scrollView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:clipChildren="false"

android:clipToPadding="false"

android:paddingTop="?attr/actionBarSize"

>

xml 加入 paddingtop .

注意:

android:clipChildren=“false”

android:clipToPadding="false"

以上就是怎么在Android中利用Scrollview实现一个滑动渐变效果,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值