GradientDrawable动态改变Shape文件

1.Shape文件的简单使用

<?xml version="1.0" encoding="utf-8"?>
<!-- android:shape 可取值有:
                   rectangle 矩形
                   oval     圆
                   line     线
                   ring     环 -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <!-- 大小 宽高 -->
    <size
        android:width="100dp"
        android:height="100dp" />


    <!-- 颜色渐变
         android:startColor:起始颜色
         android:endColor:终止颜色
         android:cneterColor:中间过渡颜色
         android:angle:旋转角度,逆时针,45的倍数,0度是从左往右,90度是从下往上
         android:type:渐变类型:
                      linear  线性渐变
                      radial 放射性渐变,需设置android:gradientRadius渐变半径
                      sweep  扫描渐变 顺时针扫描 -->
    <gradient
        android:angle="45"
        android:endColor="#ffffff"
        android:gradientRadius="50dp"
        android:startColor="#000000"
        android:type="linear" />

    <!-- 填充 会覆盖渐变色 -->
    <solid android:color="#ffffff" />

    <!-- 圆角 不同位置圆角角度:
                        android:radius              四个角   
                        android:topLeftRadius       左上  
                        android:topRightRadius      右上
                        android:bottomLeftRadius    左下
                        android:bottomRightRadius   右下 -->
    <corners android:radius="5dp" />

    <!-- 描边,边框的属性:
            android:width:边框宽度
            android:color:边框颜色
            android:dashWidth:边框类型,0:实线,大于0:虚线
                    android:dashGap:虚线间隔 -->
    <stroke
        android:width="2dp"
        android:color="#22ccdd"
        android:dashGap="1dp"
        android:dashWidth="1dp" />
</shape>

 
 
引用的话直接在view的布局文件里android:background="@drawable/test_bg"引用即可。

2.代码动态改变shape文件

使用GradientDrawable类可以动态创建shape文件和修改已创建的静态xml文件。

2.1 动态创建shape文件

GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.parseColor("#cccccc"));
gradientDrawable.setStroke(1,Color.parseColor("#ffffff"));
// GradientDrawable的各种属性
myView.setBackground(gradientDrawable);

2.2动态修改已创建的xml文件

GradientDrawable gradientDrawable =(GradientDrawable)myView.getBackground();
gradientDrawable.setColor(Color.parseColor("#cccccc"));
// gradientDrawable.set  各种属性
myView.setBackground(gradientDrawable);

3.例子:渐变背景改变动画

背景图片,左下到右上渐变,起始和终止颜色是(#000000,#ffffff)黑色到白色,动画变为(#ffffff,#000000)白色到黑色。
代码:

public static void gradientChange(int startColor1, int startColor2, int endColor1, int endColor2, final View view,final long duration) {
    final int[] gradientColor = new int[2];
    //设置窗口颜色渐变动画
    ValueAnimator animator = ValueAnimator.ofInt(startColor1, startColor2);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            gradientColor[0] = (int) animation.getAnimatedValue();
        }
    });
    animator.setDuration(duration);
    animator.setEvaluator(new ArgbEvaluator());
    animator.start();
    ValueAnimator animator2 = ValueAnimator.ofInt(endColor1, endColor2);
    animator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
            gradientColor[1] = (int) animation.getAnimatedValue();
            view.setBackground(changeGradientColor(gradientColor[0], gradientColor[1]));
        }
    });
    animator2.setDuration(duration);
    animator2.setEvaluator(new ArgbEvaluator());
    animator2.start();
}
//渐变色设置shape
private static GradientDrawable changeGradientColor(int startColor, int endColor) {
    int colors[] = {startColor, endColor};//分别为开始颜色,结束颜色
    GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.BL_TR, colors);
    gradientDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
    return gradientDrawable;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值