Android背景颜色滑动渐变效果(上下滑动,左右滑动)

今天和朋友聊到这个功能,刚开始的想法是自定义view,如何进行滑动监听,经过一列操作完成效果后,发现了一个贼简单的实现效果,如下(老规矩后面有可运行代码)。

效果图:

在这里插入图片描述

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.设置颜色

Orientation.TOP_BOTTOM为纵向,横向改变参数即可

  GradientDrawable aDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                    new int[]{Color.parseColor("#ffffff"), Color.parseColor("#009966"),Color.parseColor("#00ff00")});
            ll_base.setBackground(aDrawable);

5.获取控件与屏幕高度(宽度)的比例,根据比例设置颜色个数

java代码如下:

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;
     }
}
  • 17
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值