动态设置gradient的好处是可以设置多个颜色变化,做出更过酷炫的颜色变换。

先上效果图:

其实背景就是一个自定义View,然后在布局文件中引用进去:

public class gradientView extends View{    public gradientView(Context context) {        super(context);    }    public gradientView(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);    }    public gradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        int width = getWidth();        int height = getHeight();        int color1 = getResources().getColor(R.color.orange);        int color2 = getResources().getColor(R.color.splashCenter);        int color3 = getResources().getColor(R.color.colorPrimaryDark);        int color4 = getResources().getColor(R.color.green_light);        int color5 = getResources().getColor(R.color.green);        Paint paint = new Paint();        LinearGradient gradient = new LinearGradient(0,0,0,height,new int[]{color1,color2,color3,color4,color5},null, Shader.TileMode.MIRROR);        paint.setShader(gradient);        canvas.drawRect(0,0,width,height,paint);    }}

在布局文件中引用:

    

网上更多的例子可以看下这篇博客:

https://www.jianshu.com/p/32d17739d378

 

整个项目的下载地址:

https://github.com/buder-cp/base_component_learn

更多相关文章

  1. Android仿淘宝列表和网格布局切换
  2. Android(安卓)一条竖线或横线、画边框
  3. Android仿微信SwitchButton
  4. android中ActionBar的覆盖叠加
  5. Inflater与findViewById()区别
  6. android 4.0以上版本横竖屏切换后不重建不销毁Activity
  7. Android(安卓)6.0状态栏使用灰色文字和图标
  8. Android(安卓)View measure(0,0)的作用
  9. 3.0之后在LinearLayout里增加分割线

随机推荐

  1. android studio 使用adb 命令传递文件到a
  2. Android中shape的使用
  3. 谷歌升级手机Android商城 趋向社交网络应
  4. 图解 Android(安卓)Handler 线程消息机制
  5. Android:Google的阳谋和Apache Harmony
  6. Android(安卓)Broadcast 用法简单讨论
  7. Android开发资料推荐之Google Android开
  8. Android沉浸式状态栏下,如何代码实现andro
  9. 图解 Android(安卓)Handler 线程消息机制
  10. Android开发之Android体系架构介绍