Android实现背景颜色滑动渐变效果的全过程
作者:计蒙不吃鱼 时间:2021-08-28 09:23:51
目录
前言
一、介绍一下GradientDrawable
二、实现
三、源码:
总结
前言
今天和朋友聊到这个功能,刚开始的想法是自定义view,如何进行滑动监听,经过一列操作完成效果后,发现了一个贼简单的实现效果,如下(老规矩后面有可运行代码)。
效果图:
一、介绍一下GradientDrawable
GradientDrawable 支持渐变色的Drawable,与shapeDrawable是类似的,多了支持渐变色。
代码中的GradientDrawable比xml中的shape下gradient属性更加具体,shape下gradient属性只支持三色阶渐变,而GradientDrawable可以有更多的色阶渐变(GradientDrawable在Android中便是shape标签的代码实现)。
二、实现
1、在布局中放入一个ScrollView,然后确保里面的内容能够达到滑动的效果。
2、获取屏幕的高度
//获取屏幕高度
private float getScreenHeight(){
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels; // 屏幕宽度(像素)
int height = metric.heightPixels; // 屏幕高度(像素)
return height;
}
3、获取控件高度(此案例为ScrollView中包裹的第一个子控件)。
4、设置颜色(为了方便颜色自接写出来)
GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ffffff"), Color.parseColor("#009966"),Color.parseColor("#00ff00")});
ll_base.setBackground(aDrawable);
5、获取控件与屏幕高度(宽度)的比例,根据比例设置颜色个数
//得到控件的高度与屏幕高度的比例
private float getScreenHeightScale(int height){
return height/getScreenHeight();
}
三、源码:
public class BaseActivity extends Activity {
private LinearLayout ll_base;
private int heights;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
initView();
}
private void initView() {
ll_base = (LinearLayout) findViewById(R.id.ll_base);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
heights = ll_base.getMeasuredHeight();
float coloramount=getScreenHeightScale(heights);
if (coloramount>=0&&coloramount<1.5f){
GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ffffff"), Color.parseColor("#009966")});
ll_base.setBackground(aDrawable);
}
if (coloramount>=1.5f&&coloramount<3.0f){
GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ffffff"), Color.parseColor("#009966"), Color.parseColor("#00ff00")});
ll_base.setBackground(aDrawable);
}
if (coloramount>=3.0f&&coloramount<4.5f){
GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
new int[]{Color.parseColor("#ffffff"), Color.parseColor("#009966"), Color.parseColor("#00ff00"),Color.parseColor("#000000")});
ll_base.setBackground(aDrawable);
}
// .................
}
//得到控件的高度与屏幕高度的比例
private float getScreenHeightScale(int height){
return height/getScreenHeight();
}
//获取屏幕高度
private float getScreenHeight(){
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
int width = metric.widthPixels; // 屏幕宽度(像素)
int height = metric.heightPixels; // 屏幕高度(像素)
return height;
}
}
来源:https://juejin.cn/post/7001387181596114951
标签:android,背景,渐变
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#判断密码强度的方法
2023-11-29 23:39:53
![](https://img.aspxhome.com/file/2023/7/129737_0s.png)
JAVA与SQL 中的null与NULL解析
2023-06-23 11:51:18
![](https://img.aspxhome.com/file/2023/0/72380_0s.png)
C++编程异常处理中try和throw以及catch语句的用法
2023-04-08 15:29:41
Android仿Iphone屏幕底部弹出半透明PopupWindow效果
2023-08-17 06:14:43
Java实现五子棋游戏
2022-07-08 12:50:27
JavaSE的类和对象你真的了解吗
2021-09-17 21:14:59
![](https://img.aspxhome.com/file/2023/0/70940_0s.jpg)
在WPF中合并两个ObservableCollection集合
2022-11-01 18:42:10
java用户管理注册功能 含前后台代码
2022-08-01 12:05:11
![](https://img.aspxhome.com/file/2023/1/132071_0s.jpg)
Android 7.0开发获取存储设备信息的方法
2022-05-24 01:04:57
详解WPF中的隧道路由和冒泡路由事件
2023-03-01 07:59:49
![](https://img.aspxhome.com/file/2023/8/68068_0s.png)
关于mybatis if else if 条件判断SQL片段表达式取值和拼接问题
2023-02-04 18:51:21
![](https://img.aspxhome.com/file/2023/3/100063_0s.jpg)
WinForm通过操作注册表实现限制软件使用次数的方法
2023-07-27 15:39:57
Android Beam 文件传输失败分析与解决方法
2023-03-16 18:15:27
![](https://img.aspxhome.com/file/2023/4/113294_0s.png)
Java操作excel的三种常见方法实例
2022-12-11 02:29:55
![](https://img.aspxhome.com/file/2023/2/77362_0s.png)
java实现网站微信扫码支付
2023-06-18 18:58:19
C#隐藏手机号、邮箱等敏感信息的实现方法
2023-12-08 17:17:45
![](https://img.aspxhome.com/file/2023/3/125973_0s.png)
Java SE之了解泛型
2022-08-10 08:42:54
![](https://img.aspxhome.com/file/2023/8/84788_0s.jpg)
解决MyEclipse中的Building workspace问题的三个方法
2023-10-25 09:00:51
![](https://img.aspxhome.com/file/2023/2/81252_0s.png)
java基础--自己动手实现一个LRU
2023-06-25 18:21:04
C# 中DateTime 的使用技巧汇总
2022-04-17 07:32:05